2023-04-30 12:57:30 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2023-05-05 05:29:08 +00:00
|
|
|
const fetch = require("node-fetch").default
|
2023-05-08 12:58:46 +00:00
|
|
|
const reg = require("../../matrix/read-registration.js")
|
|
|
|
|
|
|
|
const passthrough = require("../../passthrough")
|
|
|
|
const { discord, sync, db } = passthrough
|
|
|
|
/** @type {import("../converters/message-to-event")} */
|
|
|
|
const messageToEvent = sync.require("../converters/message-to-event")
|
|
|
|
/** @type {import("../../matrix/api")} */
|
|
|
|
const api = sync.require("../../matrix/api")
|
|
|
|
/** @type {import("./register-user")} */
|
|
|
|
const registerUser = sync.require("./register-user")
|
2023-05-08 20:03:57 +00:00
|
|
|
/** @type {import("../actions/create-room")} */
|
|
|
|
const createRoom = sync.require("../actions/create-room")
|
2023-04-30 12:57:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message
|
|
|
|
*/
|
2023-05-08 12:58:46 +00:00
|
|
|
async function sendMessage(message) {
|
2023-05-05 05:29:08 +00:00
|
|
|
const event = messageToEvent.messageToEvent(message)
|
2023-05-08 20:03:57 +00:00
|
|
|
const roomID = await createRoom.ensureRoom(message.channel_id)
|
2023-05-08 12:58:46 +00:00
|
|
|
let senderMxid = null
|
|
|
|
if (!message.webhook_id) {
|
|
|
|
senderMxid = await registerUser.ensureSimJoined(message.author, roomID)
|
|
|
|
}
|
|
|
|
const eventID = api.sendEvent(roomID, "m.room.message", event, senderMxid)
|
|
|
|
return eventID
|
2023-04-30 12:57:30 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 20:25:00 +00:00
|
|
|
module.exports.sendMessage = sendMessage
|