Skip to content

Commit

Permalink
Added logging when errors happen
Browse files Browse the repository at this point in the history
  • Loading branch information
KlukCZ committed Aug 27, 2021
1 parent a693fdd commit 87ef633
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/events/message/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ export default class extends Event {
options.args.forEach(arg => (used += ` ${arg}`));
command.devOnly || command.subDevOnly ? null : await loggingManager.sendCommandLog(this.client, context.guild, context.author, context.channel.id, used);

await command.run(context, options.args);
try {
await command.run(context, options.args);
} catch (err) {
await this.client.bulbutils.logError(err, context);
}
}

private async resolveCommand(options: ResolveCommandOptions): Promise<Command | Message | undefined> {
Expand Down
6 changes: 5 additions & 1 deletion src/structures/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ export default class {

this.client.events.set(event.name, event);
event.emitter[event.type](name, async (...args: any) => {
await event.run(...args);
try {
await event.run(...args);
} catch (err) {
await this.client.bulbutils.logError(err);
}
});
}
this.client.log.client("[CLIENT - EVENTS] Successfully registered all events");
Expand Down
23 changes: 21 additions & 2 deletions src/utils/BulbBotUtils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ContextMenuInteraction, GuildMember, Snowflake, User } from "discord.js";
import { ContextMenuInteraction, GuildMember, MessageEmbed, Snowflake, TextChannel, User } from "discord.js";
import * as Emotes from "../emotes.json";
import moment, { Duration, Moment } from "moment";
import CommandContext from "../structures/CommandContext";
import BulbBotClient from "../structures/BulbBotClient";
import { UserHandle } from "./types/UserHandle";
import i18next, { TOptions } from "i18next";
import { translatorEmojis, translatorConfig } from "../Config";
import { translatorEmojis, translatorConfig, error } from "../Config";
import TranslateString from "./types/TranslateString";
import DatabaseManager from "./managers/DatabaseManager";

Expand Down Expand Up @@ -507,4 +507,23 @@ export default class {

return Emotes.actions.WARN;
}

public async logError(err: Error, context?: CommandContext): Promise<void> {
if (process.env.ENVIRONMENT === "dev") throw err;
const embed = new MessageEmbed()
.setColor("RED")
.setTitle(`New Error | ${err.name}`)
.addField("Name", err.name, true)
.addField("Message", err.message, true)
.addField("String", `${err.name}: ${err.message}`, true)
.setDescription(`**Stack trace:** \n\`\`\`${err.stack}\`\`\``);

if (context) {
embed.addField("Guild ID", <string>context?.guild?.id, true);
embed.addField("User", <string>context.author.id, true);
embed.addField("Message Content", <string>context.content, true);
}

await (<TextChannel>this.client.channels.cache.get(error)).send({ embeds: [embed] });
}
}

0 comments on commit 87ef633

Please sign in to comment.