From bc72e484f89f498388bef47754a6263949e2248e Mon Sep 17 00:00:00 2001 From: Matthew R Date: Tue, 14 Apr 2020 21:33:34 -0400 Subject: [PATCH] error handling funcs --- src/class/Client.ts | 2 ++ src/class/Util.ts | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/class/Client.ts b/src/class/Client.ts index 07228ea..7e12f5b 100644 --- a/src/class/Client.ts +++ b/src/class/Client.ts @@ -27,6 +27,7 @@ export default class Client extends eris.Client { // eslint-disable-next-line const event = new (require(`${__dirname}/../events/${file}`).default)(this); this.on(eventName, (...args) => event.run(...args)); + this.util.signale.success(`Successfully loaded event: ${eventName}`); delete require.cache[require.resolve(`${__dirname}/../events/${file}`)]; }); } @@ -37,6 +38,7 @@ export default class Client extends eris.Client { // eslint-disable-next-line new-cap const command: Command = new (require(`${__dirname}/../commands/${file}`).default)(this); this.commands.add(command.name, command); + this.util.signale.success(`Successfully loaded command: ${command.name}`); }); } } diff --git a/src/class/Util.ts b/src/class/Util.ts index 27ae07c..798e0e4 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -1,12 +1,21 @@ -import { Member, Message, Guild } from 'eris'; -import { Client, Command } from '.'; +import signale from 'signale'; +import { Member, Message, Guild, PrivateChannel, GroupChannel } from 'eris'; +import { Client, Command, RichEmbed } from '.'; import { statusMessages as emotes } from '../configs/emotes.json'; export default class Util { public client: Client; + public signale: signale.Signale; + constructor(client: Client) { this.client = client; + this.signale = signale; + this.signale.config({ + displayDate: true, + displayTimestamp: true, + displayFilename: true, + }); } get emojis() { @@ -51,4 +60,33 @@ export default class Util { return undefined; } } + + public async handleError(error: Error, message?: Message, command?: Command): Promise { + try { + this.signale.error(error); + const info = { content: `\`\`\`js\n${error.stack}\n\`\`\``, embed: null }; + if (message) { + const embed = new RichEmbed(); + embed.setColor('FF0000'); + embed.setAuthor(`Error caused by ${message.author.username}#${message.author.discriminator}`, message.author.avatarURL); + embed.setTitle('Message content'); + embed.setDescription(message.content); + embed.addField('User', `${message.author.mention} (\`${message.author.id}\`)`, true); + embed.addField('Channel', message.channel.mention, true); + let guild: string; + if (message.channel instanceof PrivateChannel || message.channel instanceof GroupChannel) guild = '@me'; + else guild = message.channel.guild.id; + embed.addField('Message link', `[Click here](https://discordapp.com/channels/${guild}/${message.channel.id}/${message.id})`, true); + embed.setTimestamp(new Date(message.timestamp)); + info.embed = embed; + } + await this.client.createMessage('595788220764127272', info); + const msg = message.content.slice(this.client.config.prefix.length).trim().split(/ +/g); + // eslint-disable-next-line no-param-reassign + if (command) this.resolveCommand(msg).then((c) => { c.cmd.enabled = false; }); + if (message) message.channel.createMessage(`***${this.emojis.ERROR} An unexpected error has occured - please contact a Faculty Marshal.${command ? ' This command has been disabled.' : ''}***`); + } catch (err) { + this.signale.error(err); + } + } }