Skip to content

Commit

Permalink
Merge pull request #511 from aternosorg/catch-logging
Browse files Browse the repository at this point in the history
Try catch sending messages to log channels, only log a warning
  • Loading branch information
JulianVennen authored Oct 25, 2022
2 parents c2e7b66 + 04ec9a7 commit 72e0fb9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/commands/CommandManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,14 @@ export class CommandManager {
* @return {Promise<void>}
*/
async handleCommandError(interaction, error) {
await logger.error(error);
const name = [
interaction.commandName,
interaction.options.getSubcommandGroup(false),
interaction.options.getSubcommand(false)
].filter(v => !!v).join(' ');
await logger.error(`Failed to execute command '${name}': ${error.name}`, error);
let message = 'An error occurred while executing this command. ';
if (error.code === RESTJSONErrorCodes.MissingPermissions) {
if ([RESTJSONErrorCodes.MissingPermissions, RESTJSONErrorCodes.MissingAccess].includes(error.code)) {
message = 'I\'m missing some permissions to execute this command. ';
}

Expand Down
13 changes: 12 additions & 1 deletion src/discord/GuildWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import RateLimiter from './RateLimiter.js';
import bot from '../bot/Bot.js';
import GuildSettings from '../settings/GuildSettings.js';
import database from '../bot/Database.js';
import logger from '../bot/Logger.js';

export default class GuildWrapper {

Expand Down Expand Up @@ -162,7 +163,17 @@ export default class GuildWrapper {

const channel = await this.fetchChannel(channelId);
if (channel && channel instanceof BaseGuildTextChannel) {
return channel.send(options);
try {
return channel.send(options);
}
catch (e) {
if ([RESTJSONErrorCodes.MissingPermissions, RESTJSONErrorCodes.MissingAccess].includes(e.code)) {
await logger.warn('Failed to send message to ' +
`${channel.name} (${channelId}) in ${this.guild.name} (${this.guild.id}): ${e.name}`, e);
} else {
throw e;
}
}
}
return null;
}
Expand Down

0 comments on commit 72e0fb9

Please sign in to comment.