mirror of
https://gitdab.com/cadence/out-of-your-element.git
synced 2025-09-09 20:03:04 +02:00
glue
This commit is contained in:
parent
6e55e6d1b3
commit
3578ca28b5
5 changed files with 94 additions and 28 deletions
|
@ -1,6 +1,7 @@
|
|||
// @ts-check
|
||||
|
||||
const assert = require("assert").strict
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
const passthrough = require("../../passthrough")
|
||||
const {discord, db} = passthrough
|
||||
|
||||
|
@ -46,5 +47,17 @@ async function withWebhook(channelID, callback) {
|
|||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} channelID
|
||||
* @param {DiscordTypes.RESTPostAPIWebhookWithTokenJSONBody & {files?: {name: string, file: Buffer}[]}[]} data
|
||||
*/
|
||||
async function sendMessageWithWebhook(channelID, data) {
|
||||
const result = await withWebhook(channelID, async webhook => {
|
||||
return discord.snow.webhook.executeWebhook(webhook.id, webhook.token, data, {wait: true, disableEveryone: true})
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
module.exports.ensureWebhook = ensureWebhook
|
||||
module.exports.withWebhook = withWebhook
|
||||
module.exports.sendMessageWithWebhook = sendMessageWithWebhook
|
43
m2d/actions/send-event.js
Normal file
43
m2d/actions/send-event.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
// @ts-check
|
||||
|
||||
const assert = require("assert").strict
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
const passthrough = require("../../passthrough")
|
||||
const {sync, discord, db} = passthrough
|
||||
|
||||
/** @type {import("./channel-webhook")} */
|
||||
const channelWebhook = sync.require("./channel-webhook")
|
||||
/** @type {import("../converters/event-to-message")} */
|
||||
const eventToMessage = sync.require("../converters/event-to-message")
|
||||
|
||||
/** @param {import("../../types").Event.Outer<any>} event */
|
||||
async function sendEvent(event) {
|
||||
// TODO: matrix equivalents...
|
||||
const roomID = await createRoom.ensureRoom(message.channel_id)
|
||||
// TODO: no need to sync the member to the other side... right?
|
||||
let senderMxid = null
|
||||
if (!message.webhook_id) {
|
||||
assert(message.member)
|
||||
senderMxid = await registerUser.ensureSimJoined(message.author, roomID)
|
||||
await registerUser.syncUser(message.author, message.member, message.guild_id, roomID)
|
||||
}
|
||||
|
||||
const messages = eventToMessage.eventToMessage(event)
|
||||
assert(Array.isArray(messages))
|
||||
|
||||
/** @type {DiscordTypes.APIMessage[]} */
|
||||
const messageResponses = []
|
||||
let eventPart = 0 // 0 is primary, 1 is supporting
|
||||
for (const message of messages) {
|
||||
const messageResponse = await channelWebhook.sendMessageWithWebhook(channelID, message)
|
||||
// TODO: are you sure about that? many to many? and we don't need to store which side it originated from?
|
||||
db.prepare("INSERT INTO event_message (event_id, message_id, part) VALUES (?, ?, ?)").run(event.event_id, messageResponse.id, eventPart)
|
||||
|
||||
eventPart = 1 // TODO: use more intelligent algorithm to determine whether primary or supporting
|
||||
messageResponses.push(messageResponse)
|
||||
}
|
||||
|
||||
return messageResponses
|
||||
}
|
||||
|
||||
module.exports.sendEvent = sendEvent
|
|
@ -1,23 +0,0 @@
|
|||
// @ts-check
|
||||
|
||||
const assert = require("assert").strict
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
const passthrough = require("../../passthrough")
|
||||
const {sync, discord, db} = passthrough
|
||||
|
||||
/** @type {import("./register-webhook")} */
|
||||
const registerWebhook = sync.require("./register-webhook")
|
||||
|
||||
/**
|
||||
* @param {string} channelID
|
||||
* @param {DiscordTypes.RESTPostAPIWebhookWithTokenJSONBody & {files?: {name: string, file: Buffer}[]}[]} data
|
||||
*/
|
||||
// param {DiscordTypes.RESTPostAPIWebhookWithTokenQuery & {wait: true, disableEveryone?: boolean}} options
|
||||
async function sendMessage(channelID, data) {
|
||||
const result = await registerWebhook.withWebhook(channelID, async webhook => {
|
||||
return discord.snow.webhook.executeWebhook(webhook.id, webhook.token, data, {wait: true, disableEveryone: true})
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
module.exports.sendMessage = sendMessage
|
Loading…
Add table
Add a link
Reference in a new issue