cloudservices/src/commands/help.ts

68 lines
4.1 KiB
TypeScript
Raw Normal View History

2019-10-15 14:03:40 -04:00
import { Message } from 'eris';
2019-10-19 08:31:06 -04:00
import { createPaginationEmbed } from 'eris-pagination';
2019-10-20 19:43:32 -04:00
import { Client } from '..';
2019-10-18 18:16:32 -04:00
import { Command, RichEmbed } from '../class';
export default class Help extends Command {
constructor(client: Client) {
super(client);
this.name = 'help';
this.description = 'Display a list of commands';
2019-10-20 19:43:32 -04:00
this.usage = `${this.client.config.prefix}help | ${this.client.config.prefix}help ping`;
2019-10-19 08:31:06 -04:00
this.aliases = ['commands'];
2019-10-18 18:16:32 -04:00
this.enabled = true;
}
2019-10-20 19:43:32 -04:00
// eslint-disable-next-line consistent-return
2019-10-18 18:16:32 -04:00
public async run(message: Message, args?: string[]) {
2019-10-19 09:32:10 -04:00
try {
2019-10-31 18:14:34 -04:00
if (!args[0]) {
2019-10-20 19:43:32 -04:00
const cmdList: Command[] = [];
this.client.commands.forEach((c) => cmdList.push(c));
2019-10-31 18:14:34 -04:00
const commands = this.client.commands.map((c: Command) => {
2019-10-20 19:43:32 -04:00
const aliases = c.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', ');
const perms: string[] = [];
let allowedRoles = c.permissions && c.permissions.roles && c.permissions.roles.map((r) => `<@&${r}>`).join(', ');
if (allowedRoles) { allowedRoles = `**Roles:** ${allowedRoles}`; perms.push(allowedRoles); }
let allowedUsers = c.permissions && c.permissions.users && c.permissions.users.map((u) => `<@${u}>`).join(', ');
if (allowedUsers) { allowedUsers = `**Users:** ${allowedUsers}`; perms.push(allowedUsers); }
const displayedPerms = perms.length ? `**Permissions:**\n${perms.join('\n')}` : '';
2019-10-28 16:21:04 -04:00
return { name: `${this.client.config.prefix}${c.name}`, value: `**Description:** ${c.description}\n**Aliases:** ${aliases}\n**Usage:** ${c.usage}\n${displayedPerms}`, inline: false };
2019-10-20 19:43:32 -04:00
});
2019-10-18 18:16:32 -04:00
2019-10-20 19:43:32 -04:00
const splitCommands = this.client.util.splitFields(commands);
const cmdPages: RichEmbed[] = [];
splitCommands.forEach((splitCmd) => {
const embed = new RichEmbed();
embed.setTimestamp(); embed.setFooter(`Requested by ${message.author.username}#${message.author.discriminator}`, message.author.avatarURL);
embed.setAuthor(`${this.client.user.username}#${this.client.user.discriminator}`, 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);
});
// @ts-ignore
if (cmdPages.length === 1) return message.channel.createMessage({ embed: cmdPages[0] });
return createPaginationEmbed(message, this.client, cmdPages);
}
2019-10-31 20:17:57 -04:00
const { cmd } = await this.client.util.resolveCommand(args[0], args.slice(1), message);
2019-10-31 18:14:34 -04:00
if (!cmd) return message.channel.createMessage(`${this.client.stores.emojis.error} **Command not found!**`);
2019-10-20 19:43:32 -04:00
const perms: string[] = [];
2019-10-31 18:14:34 -04:00
let allowedRoles = cmd.permissions && cmd.permissions.roles && cmd.permissions.roles.map((r) => `<@&${r}>`).join(', ');
2019-10-20 19:43:32 -04:00
if (allowedRoles) { allowedRoles = `**Roles:** ${allowedRoles}`; perms.push(allowedRoles); }
2019-10-31 18:14:34 -04:00
let allowedUsers = cmd.permissions && cmd.permissions.users && cmd.permissions.users.map((u) => `<@${u}>`).join(', ');
2019-10-20 19:43:32 -04:00
if (allowedUsers) { allowedUsers = `**Users:** ${allowedUsers}`; perms.push(allowedUsers); }
const displayedPerms = perms.length ? `**Permissions:**\n${perms.join('\n')}` : '';
2019-10-31 18:14:34 -04:00
const aliases = cmd.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', ');
2019-10-20 19:43:32 -04:00
const embed = new RichEmbed();
embed.setTimestamp(); embed.setFooter(`Requested by ${message.author.username}#${message.author.discriminator}`, message.author.avatarURL);
2019-10-31 20:22:11 -04:00
embed.setTitle(`${this.client.config.prefix}${cmd.parentName}`); embed.setAuthor(`${this.client.user.username}#${this.client.user.discriminator}`, this.client.user.avatarURL);
2019-10-31 18:14:34 -04:00
const description = `**Description**: ${cmd.description}\n**Usage:** ${cmd.usage}\n**Aliases:** ${aliases}\n${displayedPerms}`;
2019-10-20 19:43:32 -04:00
embed.setDescription(description);
2019-10-19 08:31:06 -04:00
// @ts-ignore
2019-10-20 19:43:32 -04:00
message.channel.createMessage({ embed });
2019-10-19 09:32:10 -04:00
} catch (error) {
2019-10-20 19:43:32 -04:00
this.client.util.handleError(error, message, this);
2019-10-19 09:32:10 -04:00
}
2019-10-18 18:16:32 -04:00
}
}