Added error handler

merge-requests/1/merge
Bsian 2019-10-16 00:10:37 +01:00
parent 17760f41c4
commit 371b3328de
No known key found for this signature in database
GPG Key ID: 097FB9A291026091
5 changed files with 83 additions and 51 deletions

View File

@ -1,6 +1,7 @@
import { promisify } from 'util'; import { promisify } from 'util';
import childProcess from 'child_process'; import childProcess from 'child_process';
import nodemailer from 'nodemailer'; import nodemailer from 'nodemailer';
import { Message } from 'eris';
import Client from './Client'; import Client from './Client';
import Command from './class/Command'; import Command from './class/Command';
@ -39,4 +40,11 @@ export default class Util {
} }
return undefined; return undefined;
} }
public async handleError(error: Error, message?: Message, command?: Command): Promise<Message> {
const stack = await this.client.createMessage('595788220764127272', `\`\`\`js\n${error.stack}\n\`\`\``);
if (command) this.client.commands.get(command.name).enabled = false;
if (message) message.channel.createMessage(`***${this.client.stores.emojis.error} An unexpected error has occured - please contact a member of the Engineering Team.${command ? ' This command has been disabled.' : ''}***`);
return stack;
}
} }

View File

@ -2,8 +2,11 @@ import fs from 'fs-extra';
import { Message } from 'eris'; import { Message } from 'eris';
import Client from '../Client'; import Client from '../Client';
import Command from '../class/Command'; import Command from '../class/Command';
import Util from '../Util';
export default class Lock extends Command { export default class Lock extends Command {
util: Util = new Util(this.client)
constructor(client: Client) { constructor(client: Client) {
super(client); super(client);
this.name = 'lock'; this.name = 'lock';
@ -13,9 +16,12 @@ export default class Lock extends Command {
} }
public async run(message: Message, args: string[]) { // eslint-disable-line public async run(message: Message, args: string[]) { // eslint-disable-line
let account = await this.client.db.Account.findOne({ account: args[0] }); try {
if (!account) account = await this.client.db.Account.findOne({ userID: args[0].replace(/[<@!>]/gi, '') }); const account = await this.client.db.Account.findOne({ $or: [{ account: args[0] }, { userID: args[0].replace(/[<@!>]/gi, '') }] });
if (!account) return message.channel.createMessage(`***${this.client.stores.emojis.error} Cannot find user.***`); if (!account) return message.channel.createMessage(`***${this.client.stores.emojis.error} Cannot find user.***`);
const edit = await message.channel.createMessage(`***${this.client.stores.emojis.loading} Locking account...***`); const edit = await message.channel.createMessage(`***${this.client.stores.emojis.loading} Locking account...***`);
} catch (error) {
return this.util.handleError(error, message, this);
}
} }
} }

View File

@ -3,8 +3,11 @@ import { createPaginationEmbed } from 'eris-pagination';
import Client from '../Client'; import Client from '../Client';
import Command from '../class/Command'; import Command from '../class/Command';
import RichEmbed from '../class/RichEmbed'; import RichEmbed from '../class/RichEmbed';
import Util from '../Util';
export default class Modlogs extends Command { export default class Modlogs extends Command {
util: Util = new Util(this.client)
constructor(client: Client) { constructor(client: Client) {
super(client); super(client);
this.name = 'modlogs'; this.name = 'modlogs';
@ -15,6 +18,7 @@ export default class Modlogs extends Command {
} }
public async run(message: Message, args: string[]) { public async run(message: Message, args: string[]) {
try {
const msg: Message = await message.channel.createMessage(`***${this.client.stores.emojis.loading} Locating modlogs...***`); const msg: Message = await message.channel.createMessage(`***${this.client.stores.emojis.loading} Locating modlogs...***`);
const query = await this.client.db.Moderation.find({ $or: [{ account: args.join(' ') }, { userID: args.filter((a) => a)[0].replace(/[<@!>]/g, '') }] }); const query = await this.client.db.Moderation.find({ $or: [{ account: args.join(' ') }, { userID: args.filter((a) => a)[0].replace(/[<@!>]/g, '') }] });
if (!query.length) return msg.edit(`***${this.client.stores.emojis.error} Cannot locate modlogs for ${args.join(' ')}***`); if (!query.length) return msg.edit(`***${this.client.stores.emojis.error} Cannot locate modlogs for ${args.join(' ')}***`);
@ -48,5 +52,8 @@ export default class Modlogs extends Command {
createPaginationEmbed(message, this.client, embeds, {}, msg); createPaginationEmbed(message, this.client, embeds, {}, msg);
return msg; return msg;
} catch (error) {
return this.util.handleError(error, message, this);
}
} }
} }

View File

@ -1,8 +1,11 @@
import { Message } from 'eris'; import { Message } from 'eris';
import Client from '../Client'; import Client from '../Client';
import Command from '../class/Command'; import Command from '../class/Command';
import Util from '../Util';
export default class Ping extends Command { export default class Ping extends Command {
util: Util = new Util(this.client)
constructor(client: Client) { constructor(client: Client) {
super(client); super(client);
this.name = 'ping'; this.name = 'ping';
@ -11,8 +14,12 @@ export default class Ping extends Command {
} }
public async run(message: Message) { public async run(message: Message) {
try {
const clientStart: number = Date.now(); const clientStart: number = Date.now();
const msg: Message = await message.channel.createMessage('🏓 Pong!'); const msg: Message = await message.channel.createMessage('🏓 Pong!');
msg.edit(`🏓 Pong!\nClient: \`${Date.now() - clientStart}ms\`\nResponse: \`${msg.createdAt - message.createdAt}ms\``); msg.edit(`🏓 Pong!\nClient: \`${Date.now() - clientStart}ms\`\nResponse: \`${msg.createdAt - message.createdAt}ms\``);
} catch (error) {
this.util.handleError(error, message, this);
}
} }
} }

View File

@ -14,6 +14,7 @@ export default class {
util: Util = new Util(this.client) util: Util = new Util(this.client)
async run(message: Message) { async run(message: Message) {
try {
const noPrefix: string[] = message.content.slice(prefix.length).trim().split(/ +/g); const noPrefix: string[] = message.content.slice(prefix.length).trim().split(/ +/g);
const command: string = noPrefix[0].toLowerCase(); const command: string = noPrefix[0].toLowerCase();
const resolved: Command = this.util.resolveCommand(command); const resolved: Command = this.util.resolveCommand(command);
@ -30,5 +31,8 @@ export default class {
if (!resolved.enabled) { message.channel.createMessage(`***${this.client.stores.emojis.error} This command has been disabled***`); return; } if (!resolved.enabled) { message.channel.createMessage(`***${this.client.stores.emojis.error} This command has been disabled***`); return; }
const args: string[] = noPrefix.slice(1); const args: string[] = noPrefix.slice(1);
resolved.run(message, args); resolved.run(message, args);
} catch (error) {
this.util.handleError(error, message);
}
} }
} }