106 lines
4.8 KiB
TypeScript
106 lines
4.8 KiB
TypeScript
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() {
|
|
return {
|
|
SUCCESS: emotes.success,
|
|
LOADING: emotes.loading,
|
|
ERROR: emotes.error,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Resolves a command
|
|
* @param query Command input
|
|
* @param message Only used to check for errors
|
|
*/
|
|
public resolveCommand(query: string | string[]): Promise<{cmd: Command, args: string[] }> {
|
|
try {
|
|
// let resolvedCommand: Command;
|
|
// eslint-disable-next-line no-param-reassign
|
|
if (typeof query === 'string') query = query.split(' ');
|
|
const commands = this.client.commands.toArray();
|
|
const resolvedCommand = commands.find((c) => c.name === query[0].toLowerCase() || c.aliases.includes(query[0].toLowerCase()));
|
|
|
|
if (!resolvedCommand) return Promise.resolve(null);
|
|
query.shift();
|
|
return Promise.resolve({ cmd: resolvedCommand, args: query });
|
|
} catch (error) {
|
|
return Promise.reject(error);
|
|
}
|
|
}
|
|
|
|
public resolveMember(message: Message, search: string, guild: Guild): Member | undefined {
|
|
try {
|
|
let member = guild.members.find((mem) => `${mem.user.username}#${mem.user.discriminator}` === search || mem.user.id === search || mem.user.username === search || mem.user.username.startsWith(search) || mem.user.username.toLowerCase() === search.toLowerCase() || mem.user.username.toLowerCase().startsWith(search.toLowerCase()));
|
|
// Nickname
|
|
// eslint-disable-next-line no-mixed-operators
|
|
if (!member) {
|
|
member = guild.members.find((mem) => mem.nick && (mem.nick === search || mem.nick.startsWith(search) || mem.nick.toLowerCase() === search.toLowerCase() || mem.nick.toLowerCase().startsWith(search.toLowerCase())));
|
|
}
|
|
if (search.split(' ').length > 1 && !member) {
|
|
let nSearch = search.split(' ');
|
|
while (!member && nSearch.length > 0) {
|
|
nSearch = nSearch.slice(0, nSearch.length - 1);
|
|
member = guild.members.find((mem) => `${mem.user.username}#${mem.user.discriminator}` === search || mem.user.id === search || mem.user.username === search || mem.user.username.startsWith(search) || mem.user.username.toLowerCase() === search.toLowerCase() || mem.user.username.toLowerCase().startsWith(search.toLowerCase()));
|
|
// Nickname
|
|
// eslint-disable-next-line no-mixed-operators
|
|
if (!member) {
|
|
member = guild.members.find((mem) => mem.nick && (mem.nick === search || mem.nick.startsWith(search) || mem.nick.toLowerCase() === search.toLowerCase() || mem.nick.toLowerCase().startsWith(search.toLowerCase())));
|
|
}
|
|
}
|
|
}
|
|
if (!member) return undefined;
|
|
return member;
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
public async handleError(error: Error, message?: Message, command?: Command): Promise<void> {
|
|
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);
|
|
}
|
|
}
|
|
}
|