From 6673d3c29472affc72e1a5cf11e4372632652eec Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Mon, 24 Jul 2023 03:06:33 +0200 Subject: [PATCH] Fix crash when replying to request where bot cannot reply --- src/execution.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/execution.ts b/src/execution.ts index 2be19cd..057d80e 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -1,4 +1,4 @@ -import DiscordApi from "discord.js"; +import DiscordApi, { GuildTextBasedChannel } from "discord.js"; import { database, openai } from "./index"; import Moderation from "./moderation"; @@ -115,6 +115,15 @@ function requestReply( } } +/** + * Checks if the request can be replied in a channel + * @param request the request to check + * @returns if the request can be replied to + */ +function canReplyToRequest(request: apiRequest) { + return !(request.guild?.members.me?.permissionsIn(request.channel as GuildTextBasedChannel).has("SendMessages") ?? true); +} + /** * Check and queues up the request and runs it if there is nothing in queue. * @param request the message to check and queue @@ -130,6 +139,8 @@ export async function queueRequest(request: apiRequest) { return; } + if (!canReplyToRequest(request)) return; + const userLimit = await getUserLimit(getAuthor(request), request.createdAt); if (userLimit !== false && userLimit.remaining <= 0) { @@ -208,6 +219,9 @@ async function executeFromQueue(channel: string) { const message = channelQueue.at(0) as RequestMessage; let functionRanCounter = 0; + // ignore if we can't even send anything to reply + if (!canReplyToRequest(message)) return; + try { let messages: DiscordApi.Collection = await message.channel.messages.fetch({ limit: config.limits.messages, cache: false });