Fix crash when replying to request where bot cannot reply

This commit is contained in:
Wroclaw 2023-07-24 03:06:33 +02:00
parent 13d8f73356
commit 6673d3c294

View file

@ -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<string, DiscordApi.Message> = await message.channel.messages.fetch({ limit: config.limits.messages, cache: false });