Fix all rooms being affected by speedbump

This commit is contained in:
Cadence Ember 2024-01-22 22:05:59 +13:00
parent 11864f80cf
commit 6a06dc14ce
3 changed files with 13 additions and 10 deletions

View file

@ -11,7 +11,7 @@ const speedbump = sync.require("./speedbump")
* @param {import("discord-api-types/v10").GatewayMessageDeleteDispatchData} data * @param {import("discord-api-types/v10").GatewayMessageDeleteDispatchData} data
*/ */
async function deleteMessage(data) { async function deleteMessage(data) {
const row = select("channel_room", ["room_id", "speedbump_id", "speedbump_checked"], {channel_id: data.channel_id}).get() const row = select("channel_room", ["room_id", "speedbump_checked"], {channel_id: data.channel_id}).get()
if (!row) return if (!row) return
const eventsToRedact = select("event_message", "event_id", {message_id: data.id}).pluck().all() const eventsToRedact = select("event_message", "event_id", {message_id: data.id}).pluck().all()
@ -22,7 +22,7 @@ async function deleteMessage(data) {
await api.redactEvent(row.room_id, eventID) await api.redactEvent(row.room_id, eventID)
} }
speedbump.updateCache(data.channel_id, row.speedbump_id, row.speedbump_checked) speedbump.updateCache(data.channel_id, row.speedbump_checked)
} }
/** /**

View file

@ -15,12 +15,11 @@ const KNOWN_BOTS = new Set([
/** /**
* Fetch new speedbump data for the channel and put it in the database as cache * Fetch new speedbump data for the channel and put it in the database as cache
* @param {string} channelID * @param {string} channelID
* @param {string?} speedbumpID * @param {number?} lastChecked
* @param {number?} speedbumpChecked
*/ */
async function updateCache(channelID, speedbumpID, speedbumpChecked) { async function updateCache(channelID, lastChecked) {
const now = Math.floor(Date.now() / 1000) const now = Math.floor(Date.now() / 1000)
if (speedbumpChecked && now - speedbumpChecked < SPEEDBUMP_UPDATE_FREQUENCY) return if (lastChecked && now - lastChecked < SPEEDBUMP_UPDATE_FREQUENCY) return
const webhooks = await discord.snow.webhook.getChannelWebhooks(channelID) const webhooks = await discord.snow.webhook.getChannelWebhooks(channelID)
const found = webhooks.find(b => KNOWN_BOTS.has(b.application_id)) const found = webhooks.find(b => KNOWN_BOTS.has(b.application_id))
const foundApplication = found?.application_id const foundApplication = found?.application_id

View file

@ -241,11 +241,13 @@ module.exports = {
const guild = client.guilds.get(channel.guild_id) const guild = client.guilds.get(channel.guild_id)
assert(guild) assert(guild)
const row = select("channel_room", ["speedbump_id", "speedbump_webhook_id"], {channel_id: message.channel_id}).get()
if (message.webhook_id) { if (message.webhook_id) {
const row = select("webhook", "webhook_id", {webhook_id: message.webhook_id}).pluck().get() const row = select("webhook", "webhook_id", {webhook_id: message.webhook_id}).pluck().get()
if (row) return // The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it. if (row) return // The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it.
} else if (row) { }
const row = select("channel_room", ["speedbump_id", "speedbump_webhook_id"], {channel_id: message.channel_id}).get()
if (row && row.speedbump_id) {
const affected = await speedbump.doSpeedbump(message.id) const affected = await speedbump.doSpeedbump(message.id)
if (affected) return if (affected) return
} }
@ -260,11 +262,13 @@ module.exports = {
* @param {DiscordTypes.GatewayMessageUpdateDispatchData} data * @param {DiscordTypes.GatewayMessageUpdateDispatchData} data
*/ */
async onMessageUpdate(client, data) { async onMessageUpdate(client, data) {
const row = select("channel_room", ["speedbump_id", "speedbump_webhook_id"], {channel_id: data.channel_id}).get()
if (data.webhook_id) { if (data.webhook_id) {
const row = select("webhook", "webhook_id", {webhook_id: data.webhook_id}).pluck().get() const row = select("webhook", "webhook_id", {webhook_id: data.webhook_id}).pluck().get()
if (row) return // The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it. if (row) return // The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it.
} else if (row) { }
const row = select("channel_room", ["speedbump_id", "speedbump_webhook_id"], {channel_id: data.channel_id}).get()
if (row) {
// Edits need to go through the speedbump as well. If the message is delayed but the edit isn't, we don't have anything to edit from. // Edits need to go through the speedbump as well. If the message is delayed but the edit isn't, we don't have anything to edit from.
const affected = await speedbump.doSpeedbump(data.id) const affected = await speedbump.doSpeedbump(data.id)
if (affected) return if (affected) return