m->d message editing deletion

This commit is contained in:
Cadence Ember 2023-08-30 13:14:23 +12:00
parent 621a22df01
commit 5bef9ebfe2
2 changed files with 21 additions and 2 deletions

View File

@ -71,7 +71,20 @@ async function editMessageWithWebhook(channelID, messageID, data, threadID) {
return result
}
/**
* @param {string} channelID
* @param {string} messageID
* @param {string} [threadID]
*/
async function deleteMessageWithWebhook(channelID, messageID, threadID) {
const result = await withWebhook(channelID, async webhook => {
return discord.snow.webhook.deleteWebhookMessage(webhook.id, webhook.token, messageID, threadID)
})
return result
}
module.exports.ensureWebhook = ensureWebhook
module.exports.withWebhook = withWebhook
module.exports.sendMessageWithWebhook = sendMessageWithWebhook
module.exports.editMessageWithWebhook = editMessageWithWebhook
module.exports.deleteMessageWithWebhook = deleteMessageWithWebhook

View File

@ -31,20 +31,26 @@ async function sendEvent(event) {
const {messagesToEdit, messagesToSend, messagesToDelete} = await eventToMessage.eventToMessage(event, guild, {api})
let eventPart = 0 // 0 is primary, 1 is supporting
/** @type {DiscordTypes.APIMessage[]} */
const messageResponses = []
let eventPart = 0 // 0 is primary, 1 is supporting
for (const data of messagesToEdit) {
const messageResponse = await channelWebhook.editMessageWithWebhook(channelID, data.id, data.message, threadID)
eventPart = 1
messageResponses.push(messageResponse)
}
for (const id of messagesToDelete) {
await channelWebhook.deleteMessageWithWebhook(channelID, id, threadID)
}
for (const message of messagesToSend) {
const messageResponse = await channelWebhook.sendMessageWithWebhook(channelID, message, threadID)
db.prepare("REPLACE INTO message_channel (message_id, channel_id) VALUES (?, ?)").run(messageResponse.id, channelID)
db.prepare("INSERT INTO event_message (event_id, event_type, event_subtype, message_id, part, source) VALUES (?, ?, ?, ?, ?, 0)").run(event.event_id, event.type, event.content.msgtype || null, messageResponse.id, eventPart) // source 0 = matrix
eventPart = 1 // TODO: use more intelligent algorithm to determine whether primary or supporting?
eventPart = 1
messageResponses.push(messageResponse)
}