diff --git a/backend/src/Queue.ts b/backend/src/Queue.ts index 2ca63d2f..a8505411 100644 --- a/backend/src/Queue.ts +++ b/backend/src/Queue.ts @@ -29,10 +29,14 @@ export class Queue { } public add(fn: TQueueFunction): Promise { - const promise = new Promise((resolve) => { + const promise = new Promise((resolve, reject) => { this.queue.push(async () => { - const result = await fn(); - resolve(result); + try { + const result = await fn(); + resolve(result); + } catch (err) { + reject(err); + } }); if (!this.running) this.next(); diff --git a/backend/src/plugins/Cases/functions/postToCaseLogChannel.ts b/backend/src/plugins/Cases/functions/postToCaseLogChannel.ts index 6c1342d7..594f0852 100644 --- a/backend/src/plugins/Cases/functions/postToCaseLogChannel.ts +++ b/backend/src/plugins/Cases/functions/postToCaseLogChannel.ts @@ -24,7 +24,7 @@ export async function postToCaseLogChannel( // This doesn't use `!isText() || isThread()` because TypeScript had some issues inferring types from it if (!caseLogChannel || !(caseLogChannel instanceof TextChannel || caseLogChannel instanceof NewsChannel)) return null; - let result; + let result: InternalPosterMessageResult | null = null; try { if (file != null) { content.files = file; diff --git a/backend/src/plugins/InternalPoster/functions/sendMessage.ts b/backend/src/plugins/InternalPoster/functions/sendMessage.ts index a5ce27d9..aecd7735 100644 --- a/backend/src/plugins/InternalPoster/functions/sendMessage.ts +++ b/backend/src/plugins/InternalPoster/functions/sendMessage.ts @@ -10,6 +10,16 @@ export type InternalPosterMessageResult = { channelId: string; }; +async function sendDirectly( + channel: TextChannel | NewsChannel, + content: MessageOptions, +): Promise { + return channel.send(content).then((message) => ({ + id: message.id, + channelId: message.channelId, + })); +} + /** * Sends a message using a webhook or direct API requests, preferring webhooks when possible. */ @@ -17,7 +27,7 @@ export async function sendMessage( pluginData: GuildPluginData, channel: TextChannel | NewsChannel, content: MessageOptions, -): Promise { +): Promise { return pluginData.state.queue.add(async () => { if (!pluginData.state.webhookClientCache.has(channel.id)) { const webhookInfo = await getOrCreateWebhookForChannel(pluginData, channel); @@ -53,19 +63,13 @@ export async function sendMessage( pluginData.state.webhookClientCache.delete(channel.id); // Fallback to regular message for this log message - return channel.send(content).then((message) => ({ - id: message.id, - channelId: message.channelId, - })); + return sendDirectly(channel, content); } throw err; }); } - return channel.send(content).then((message) => ({ - id: message.id, - channelId: message.channelId, - })); + return sendDirectly(channel, content); }); }