diff --git a/Makefile b/Makefile index b44d381..6bb691c 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ clean: @-rm -rf build build: - tsc -p ./tsconfig.json + -tsc -p ./tsconfig.json run: cd build && node main diff --git a/package.json b/package.json index bf1c286..31d1f91 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "dependencies": { "axios": "^0.19.2", "eris": "bsian03/eris#bsian", + "eris-pagination": "bsian03/eris-pagination#dev", "moment": "^2.24.0", "mongoose": "^5.9.9", "signale": "^1.4.0", diff --git a/src/class/Util.ts b/src/class/Util.ts index 10a0702..c0798c5 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -118,6 +118,16 @@ export default class Util { return arrayString; } + public splitFields(fields: { name: string, value: string, inline?: boolean }[]): { name: string, value: string, inline?: boolean }[][] { + let index = 0; + const array: {name: string, value: string, inline?: boolean}[][] = [[]]; + while (fields.length) { + if (array[index].length >= 25) { index += 1; array[index] = []; } + array[index].push(fields[0]); fields.shift(); + } + return array; + } + public decimalToHex(int: number): string { const hex = int.toString(16); return '#000000'.substring(0, 7 - hex.length) + hex; diff --git a/src/commands/help.ts b/src/commands/help.ts new file mode 100644 index 0000000..4c468d2 --- /dev/null +++ b/src/commands/help.ts @@ -0,0 +1,116 @@ +import { createPaginationEmbed } from 'eris-pagination'; +import { Message } from 'eris'; +import { Client, Command, RichEmbed } from '../class'; + +export default class Help extends Command { + constructor(client: Client) { + super(client); + this.name = 'help'; + this.description = 'Information about commands.'; + this.usage = 'help [command]'; + this.permissions = 0; + this.enabled = true; + } + + // eslint-disable-next-line consistent-return + public async run(message: Message, args: string[]) { + try { + if (args.length > 0) { + const command = this.client.commands.get(args[0].toLowerCase()); + if (!command) return this.error(message.channel, 'The command you provided doesn\'t exist.'); + const embed = new RichEmbed(); + embed.setTitle(`${this.client.config.prefix}${command.name}`); + embed.addField('Description', command.description ?? '-'); + embed.addField('Usage', command.usage ?? '-'); + if (command.aliases.length > 0) { + embed.addField('Aliases', command.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', ')); + } + let description: string = ''; + if (!command.enabled) { + description += 'This command is disabled.'; + } + if (command.guildOnly) { + description += 'This command can only be ran in a guild.'; + } + embed.setDescription(description); + switch (command.permissions) { + case 0: + break; + case 1: + embed.addField('Permissions', 'Associates+'); + break; + case 2: + embed.addField('Permissions', 'Core Team+'); + break; + case 3: + embed.addField('Permissions', 'Moderators, Supervisor, & Board of Directors'); + break; + case 4: + embed.addField('Permissions', 'Technicians, Supervisor, & Board of Directors'); + break; + case 5: + embed.addField('Permissions', 'Moderators, Technicians, Supervisor, & Board of Directors'); + break; + case 6: + embed.addField('Permissions', 'Supervisor+'); + break; + case 7: + embed.addField('Permissions', 'Board of Directors'); + break; + default: + break; + } + embed.setFooter(this.client.user.username, this.client.user.avatarURL); + embed.setTimestamp(); + return message.channel.createMessage({ embed }); + } + const cmdList: Command[] = []; + this.client.commands.forEach((c) => cmdList.push(c)); + const commands = this.client.commands.map((c) => { + const aliases = c.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', '); + let perm: string; + switch (c.permissions) { + case 0: + break; + case 1: + perm = 'Associates+'; + break; + case 2: + perm = 'Core Team+'; + break; + case 3: + perm = 'Moderators, Supervisor, & Board of Directors'; + break; + case 4: + perm = 'Technicians, Supervisor, & Board of Directors'; + break; + case 5: + perm = 'Moderators, Technicians, Supervisor, & Board of Directors'; + break; + case 6: + perm = 'Supervisor+'; + break; + case 7: + perm = 'Board of Directors'; + break; + default: + break; + } + return { name: `${this.client.config.prefix}${c.name}`, value: `**Description:** ${c.description}\n**Aliases:** ${aliases}\n**Usage:** ${c.usage}\n**Permissions:** ${perm}`, inline: false }; + }); + const splitCommands = this.client.util.splitFields(commands); + const cmdPages: RichEmbed[] = []; + splitCommands.forEach((splitCmd) => { + const embed = new RichEmbed(); + embed.setTimestamp(); embed.setFooter(this.client.user.username, this.client.user.avatarURL); + embed.setDescription(`Command list for ${this.client.user.username}`); + splitCmd.forEach((c) => embed.addField(c.name, c.value, c.inline)); + return cmdPages.push(embed); + }); + if (cmdPages.length === 1) return message.channel.createMessage({ embed: cmdPages[0] }); + return createPaginationEmbed(message, cmdPages); + } catch (err) { + this.client.util.handleError(err, message, this); + } + } +}