Skip to content

Commit

Permalink
Catch some errors in auto responses
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Jun 17, 2024
1 parent c01608b commit 53094d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/events/discord/messageCreate/AutoResponseEventListener.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import MessageCreateEventListener from './MessageCreateEventListener.js';
import AutoResponse from '../../../database/AutoResponse.js';
import {ThreadChannel} from 'discord.js';
import {RESTJSONErrorCodes, ThreadChannel} from 'discord.js';
import {MESSAGE_LENGTH_LIMIT} from '../../../util/apiLimits.js';
import logger from '../../../bot/Logger.js';

export default class AutoResponseEventListener extends MessageCreateEventListener {

Expand All @@ -20,7 +22,16 @@ export default class AutoResponseEventListener extends MessageCreateEventListene

if (triggered.length) {
const response = triggered[Math.floor(Math.random() * triggered.length)];
await message.reply({content: response.response});
try {
await message.reply({content: response.response.substring(0, MESSAGE_LENGTH_LIMIT)});
} catch (e) {
if (e.code === RESTJSONErrorCodes.MissingPermissions) {
const channel = /** @type {import('discord.js').GuildTextBasedChannel} */ message.channel;
await logger.warn(`Missing permissions to respond to message in channel ${channel?.name} (${message.channelId}) of guild ${message.guild?.name} (${message.guildId})`, e);
return;
}
throw e;
}
}
}
}
3 changes: 2 additions & 1 deletion src/util/apiLimits.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ export const BULK_DELETE_MAX_AGE = 14 * 24 * 60 * 60 * 1000;
*/
export const FETCH_BAN_PAGE_SIZE = 1000;

export const MESSAGE_FILE_LIMIT = 10;
export const MESSAGE_FILE_LIMIT = 10;
export const MESSAGE_LENGTH_LIMIT = 2000;

0 comments on commit 53094d8

Please sign in to comment.