out-of-your-element/m2d/event-dispatcher.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-07-02 13:06:05 +00:00
// @ts-check
2023-07-03 05:20:24 +00:00
/**
* Grab Matrix events we care about, check them, and bridge them.
*/
2023-06-28 20:08:17 +00:00
const assert = require("assert").strict
const {sync, as} = require("../passthrough")
2023-07-03 05:20:24 +00:00
const reg = require("../matrix/read-registration")
/** @type {import("./actions/send-event")} */
const sendEvent = sync.require("./actions/send-event")
const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
/**
* Determine whether an event is the bridged representation of a discord message.
* Such messages shouldn't be bridged again.
* @param {import("../types").Event.Outer<any>} event
*/
function eventOriginatedFromDiscord(event) {
if (
// If it's from a user in the bridge's namespace...
userRegex.some(x => event.sender.match(x))
// ...not counting the appservice's own user...
&& !event.sender.startsWith(`@${reg.sender_localpart}:`)
) {
// ...then it originated from discord
return true
}
2023-06-28 20:08:17 +00:00
2023-07-03 05:20:24 +00:00
return false
}
2023-06-28 20:08:17 +00:00
sync.addTemporaryListener(as, "type:m.room.message", event => {
console.log(event)
2023-07-03 05:20:24 +00:00
if (eventOriginatedFromDiscord(event)) return
const messageResponses = sendEvent.sendEvent(event)
2023-06-28 20:08:17 +00:00
})