Skip to content

Commit

Permalink
refactor: better error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Battlesquid committed Jul 11, 2024
1 parent f72be43 commit 5f28f1d
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 95 deletions.
24 changes: 22 additions & 2 deletions src/commands/hall_of_fame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ export class HallOfFameCommand extends AugmentedSubcommand {

const { settings, error } = await this.getSettings(inter.guildId);
if (error) {
logger.error("An error occurred while retreiving your settings.", error);
inter.reply({
content: "An error occurred while retrieving your settings.",
ephemeral: true
});
logger.error("An error occurred while retrieving your settings.", error);
return;
}

Expand All @@ -88,6 +92,10 @@ export class HallOfFameCommand extends AugmentedSubcommand {
});
inter.reply(`Enabled hall of fame on ${channel}.`);
} catch (error) {
inter.reply({
content: "An error occurred while enabling hall of fame.",
ephemeral: true
});
logger.error("An error occurred while enabling hall of fame.", error);
}
}
Expand All @@ -98,6 +106,10 @@ export class HallOfFameCommand extends AugmentedSubcommand {
const { settings, error } = await this.getSettings(inter.guildId);

if (error) {
inter.reply({
content: "An error occurred while retreiving your settings.",
ephemeral: true
});
logger.error("An error occurred while retreiving your settings.", error);
return;
}
Expand All @@ -112,6 +124,10 @@ export class HallOfFameCommand extends AugmentedSubcommand {
.where(eq(hallOfFameSettings.gid, inter.guildId));
inter.reply(`Disabled hall of fame on ${channel}`);
} catch (error) {
inter.reply({
content: "An error occurred while disabling hall of fame.",
ephemeral: true
});
logger.error("An error occurred while disabling hall of fame.", error);
}
}
Expand All @@ -125,11 +141,15 @@ export class HallOfFameCommand extends AugmentedSubcommand {

const { settings, error } = await this.getSettings(inter.guildId);
if (error) {
inter.reply({
content: "An error occurred while retreiving your settings.",
ephemeral: true
});
logger.error("An error occurred while retreiving your settings.", error);
return;
}
if (isNullish(settings)) {
const mention = slashCommandMention(hall_of_fame.commands.chat.base.name, hall_of_fame.commands.chat.subcommands.enable.name, "1126901836243275806");
const mention = slashCommandMention(hall_of_fame.commands.chat.base.name, hall_of_fame.commands.chat.subcommands.enable.name, this.hints().getMessageId());
inter.reply({
content: `You must create a hall of fame first. Create one with ${mention}.`,
ephemeral: true
Expand Down
4 changes: 4 additions & 0 deletions src/commands/imageboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class ImageBoardCommand extends AugmentedSubcommand {
const channel = inter.options.getChannel<ChannelType.GuildText>("channel", true);
const { settings, error } = await this.getSettings(inter.guildId);
if (error) {
inter.reply({
content: "An error occurred while retrieving your settings.",
ephemeral: true
});
logger.error("An error occurred while retrieving your settings.", error);
return;
}
Expand Down
12 changes: 10 additions & 2 deletions src/commands/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export class LogsCommand extends AugmentedSubcommand {
});
inter.reply(`Enabled ${type} logging on ${channel}.`);
} catch (error) {
logger.error("An error occurred, please try again later", error);
inter.reply({
content: "An error occurred while saving your settings.",
ephemeral: true
});
logger.error("An error occurred while saving your settings.", error);
}
}

Expand All @@ -66,7 +70,11 @@ export class LogsCommand extends AugmentedSubcommand {
.where(eq(logSettings.gid, inter.guildId));
inter.reply(`${capitalize(type)} logging disabled.`);
} catch (error) {
logger.error("An error occurred.", error);
inter.reply({
content: "An error occurred while saving your settings.",
ephemeral: true
});
logger.error("An error occurred while saving your settings.", error);
}
}
}
40 changes: 24 additions & 16 deletions src/commands/reactroles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ export class ReactRolesCommand extends AugmentedSubcommand {
});
inter.reply(`New react roles created in ${channel}.`);
} catch (error) {
logger.error("An error occurred while sending ", error);
inter.reply({
content: `An error occurred while creating '${title}'.`,
ephemeral: true
});
logger.error(`An error occurred while creating '${title}'.`, error);
}
}

Expand Down Expand Up @@ -130,7 +134,11 @@ export class ReactRolesCommand extends AugmentedSubcommand {
});
inter.reply(`'${title}' updated successfully.`);
} catch (error) {
logger.error(`An error occurred while trying to edit '${title}'.`, error);
inter.reply({
content: `An error occurred while editing '${title}'.`,
ephemeral: true
});
logger.error(`An error occurred while editing '${title}'.`, error);
}
}

Expand Down Expand Up @@ -173,6 +181,10 @@ export class ReactRolesCommand extends AugmentedSubcommand {
await msg.react(emoji);
inter.reply(`Users can now react to '${title}' with ${emoji} to get the ${role} role`);
} catch (error) {
inter.reply({
content: `An error occurred while editing '${title}'.`,
ephemeral: true
});
logger.error(`An error occurred while editing '${title}'.`, error);
}
}
Expand Down Expand Up @@ -205,13 +217,11 @@ export class ReactRolesCommand extends AugmentedSubcommand {
try {
await msg.edit({ embeds: [builder] });
} catch (error) {
logger.error(
{
interaction: `An error occurred while removing ${role} from '${title}'.`,
logger: `An error occurred while removing ${role.name} from '${title}'.`
},
error
);
inter.reply({
content: `An error occurred while removing ${role} from '${title}'.`,
ephemeral: true
});
logger.error(`An error occurred while removing ${role.name} from '${title}'.`, error);
return;
}

Expand All @@ -221,13 +231,11 @@ export class ReactRolesCommand extends AugmentedSubcommand {
await msg.reactions.cache.get(emoji)?.remove();
inter.reply(`Removed ${role} from '${title}'.`);
} catch (error) {
logger.error(
{
interaction: `An error occurred while removing ${role} from '${title}'.`,
logger: `An error occurred while removing ${role.name} from '${title}'.`
},
error
);
inter.reply({
content: `An error occurred while removing ${role} from '${title}'.`,
ephemeral: true
});
logger.error(`An error occurred while removing ${role.name} from '${title}'.`, error);
return;
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/commands/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export class TimeoutCommand extends AugmentedCommand {
millis = ms(duration);
durationStr = ms(millis);
} catch (e) {
inter.reply({
content: "An invalid duration was provided.",
ephemeral: true
});
logger.error("An invalid duration was provided.", e);
return;
}
Expand All @@ -74,7 +78,7 @@ export class TimeoutCommand extends AugmentedCommand {
await member.disableCommunicationUntil(Date.now() + millis, reason);
inter.reply(`Timed out ${member} for ${durationStr} (${reason}).`);
} catch (error) {
this.container.logger.error(error);
logger.error("An error occurred while timing out user, please try again later.", error);
inter.reply({
content: "An error occurred while timing out user, please try again later.",
ephemeral: true
Expand All @@ -93,7 +97,7 @@ export class TimeoutCommand extends AugmentedCommand {
try {
channel = await inter.guild.channels.fetch(settings.moderation);
} catch (error) {
this.container.logger.error(error);
logger.error("Unable to retreive moderation logging channel.", error);
inter.followUp({
content: "Unable to retreive moderation logging channel.",
ephemeral: true
Expand All @@ -109,7 +113,7 @@ export class TimeoutCommand extends AugmentedCommand {
try {
await this.db.update(logSettings).set({ moderation: null }).where(eq(logSettings.gid, inter.guildId));
} catch (error) {
this.container.logger.error(error);
logger.error("An error occurred while deleting the old moderation log channel", error);
inter.followUp({
content: "An error occurred while deleting the old moderation log channel",
ephemeral: true
Expand All @@ -129,7 +133,7 @@ export class TimeoutCommand extends AugmentedCommand {
try {
await channel.send({ embeds: [embed] });
} catch (error) {
this.container.logger.error(error);
logger.error("An error occurred while recording the timeout log.", error);
inter.followUp({
content: "An error occurred while recording the timeout log.",
ephemeral: true
Expand Down
77 changes: 56 additions & 21 deletions src/commands/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ export class VerifyCommand extends AugmentedSubcommand {

const { settings, error } = await this.getSettings(inter.guildId);
if (error) {
logger.error("An error occurred while retrieving settings.", error);
inter.reply({
content: "An error occurred while retrieving your settings.",
ephemeral: true
});
logger.error("An error occurred while retrieving your settings.", error);
return;
}
if (settings) {
Expand All @@ -108,6 +112,10 @@ export class VerifyCommand extends AugmentedSubcommand {
]);
inter.reply(`Verification enabled.${successText}`);
} catch (error) {
inter.reply({
content: "An error occurred while enabling verification.",
ephemeral: true
});
logger.error("An error occurred while enabling verification.", error);
}
}
Expand All @@ -117,7 +125,11 @@ export class VerifyCommand extends AugmentedSubcommand {

const { settings, error } = await this.getSettings(inter.guildId);
if (error) {
logger.error("An error occurred while retrieving settings.", error);
inter.reply({
content: "An error occurred while retrieving your settings.",
ephemeral: true
});
logger.error("An error occurred while retrieving your settings.", error);
return;
}
if (isNullish(settings)) {
Expand All @@ -129,14 +141,19 @@ export class VerifyCommand extends AugmentedSubcommand {
await this.db.delete(verifyEntry).where(eq(verifyEntry.gid, inter.guildId));
inter.reply("Verification disabled.");
} catch (error) {
inter.reply({
content: "An error occurred while disabling verification.",
ephemeral: true
});
logger.error("An error occurred while disabling verification.", error);
}
}

public async chatInputRequest(inter: Subcommand.ChatInputCommandInteraction<"cached">) {
const logger = this.getCommandLogger(inter);
const { settings, error } = await this.getSettings(inter.guildId);
if (error) {
console.error(error);
logger.error("An error occurred while retrieving your settings.", error);
inter.reply({
content: "An error occurred while processing your request, please try again later.",
ephemeral: true
Expand Down Expand Up @@ -179,6 +196,10 @@ export class VerifyCommand extends AugmentedSubcommand {
const logger = this.getCommandLogger(inter);
const { settings, error } = await this.getSettings(inter.guildId);
if (error) {
inter.reply({
content: "An error occurred while retrieving your settings.",
ephemeral: true
});
logger.error("An error occurred while retrieving your settings.", error);
return;
}
Expand Down Expand Up @@ -232,7 +253,11 @@ export class VerifyCommand extends AugmentedSubcommand {
}
const { settings, error } = await this.getSettings(inter.guildId);
if (error) {
logger.error("An error occurred while retrieving settings.", error);
inter.reply({
content: "An error occurred while retrieving your settings.",
ephemeral: true
});
logger.error("An error occurred while retrieving your settings.", error);
return;
}
if (isNullish(settings)) {
Expand All @@ -254,13 +279,11 @@ export class VerifyCommand extends AugmentedSubcommand {
.where(eq(verifySettings.gid, inter.guildId));
inter.reply(`Added ${role} to verification roles.`);
} catch (error) {
logger.error(
{
interaction: `An error occurred while trying to add ${role} to verification roles.`,
logger: `An error occurred while trying to add ${role.name} to verification roles.`
},
error
);
inter.reply({
content: `An error occurred while trying to add ${role} to verification roles.`,
ephemeral: true
});
logger.error(`An error occurred while trying to add ${role.name} to verification roles.`, error);
}
}
public async chatInputRemoveRole(inter: Subcommand.ChatInputCommandInteraction<"cached">) {
Expand All @@ -275,7 +298,11 @@ export class VerifyCommand extends AugmentedSubcommand {

const { settings, error } = await this.getSettings(inter.guildId);
if (error) {
logger.error("An error occurred while retrieving settings.", error);
inter.reply({
content: "An error occurred while retrieving your settings.",
ephemeral: true
});
logger.error("An error occurred while retrieving your settings.", error);
return;
}
if (isNullish(settings)) {
Expand Down Expand Up @@ -316,13 +343,11 @@ export class VerifyCommand extends AugmentedSubcommand {
.where(eq(verifySettings.gid, inter.guildId));
inter.reply(`Removed ${role} from verification roles${replaceText}.`);
} catch (error) {
logger.error(
{
interaction: `An error occurred while trying to remove ${role} from verification roles.`,
logger: `An error occurred while trying to remove ${role.name} from verification roles.`
},
error
);
inter.reply({
content: `An error occurred while trying to remove ${role} to verification roles.`,
ephemeral: true
});
logger.error(`An error occurred while trying to remove ${role.name} to verification roles.`, error);
}
}

Expand All @@ -338,7 +363,11 @@ export class VerifyCommand extends AugmentedSubcommand {
const logger = this.getCommandLogger(inter);
const { settings, error } = await this.getSettings(inter.guildId);
if (error) {
logger.error("An error occurred while retrieving settings.", error);
inter.reply({
content: "An error occurred while retrieving your settings.",
ephemeral: true
});
logger.error("An error occurred while retrieving your settings.", error);
return;
}
if (isNullish(settings)) {
Expand Down Expand Up @@ -372,16 +401,22 @@ export class VerifyCommand extends AugmentedSubcommand {
.where(eq(verifySettings.gid, inter.guildId));
inter.reply("Successfully updated verification settings.");
} catch (error) {
inter.reply({
content: "An error occurred while editing your verification settings.",
ephemeral: true
});
logger.error("An error occurred while editing your verification settings.", error);
}
}

public async chatInputRescan(inter: Subcommand.ChatInputCommandInteraction<"cached">) {
await inter.deferReply();
const logger = this.getCommandLogger(inter);
await inter.deferReply({ ephemeral: true });

const { settings, error: settingsError } = await this.getSettings(inter.guildId);
if (settingsError) {
await inter.editReply("An error occurred while retrieving settings.");
logger.error("An error occurred while retrieving settings.", settingsError);
return;
}
if (isNullish(settings)) {
Expand Down
Loading

0 comments on commit 5f28f1d

Please sign in to comment.