fix(cmd): help command rewrite

This commit is contained in:
Rauf 2020-01-13 23:42:02 -05:00
parent bf24b9f46b
commit 685b1c1d39
2 changed files with 59 additions and 8 deletions

View File

@ -1,11 +1,11 @@
import { Client } from 'discord.js';
import { readdir } from 'fs'; import { readdir } from 'fs';
import { promisify } from 'util'; import { promisify } from 'util';
import { Event } from './Event'; import { Event } from './Event';
import { PluginClient } from '../PluginClient';
const readDir = promisify(readdir); const readDir = promisify(readdir);
export async function EventLoader(lifeguard: Client) { export async function EventLoader(lifeguard: PluginClient) {
const eventFiles = await readDir('./build/src/events'); const eventFiles = await readDir('./build/src/events');
for await (const file of eventFiles) { for await (const file of eventFiles) {

View File

@ -1,14 +1,65 @@
import { Command } from '../Command'; import { Command } from '../Command';
import { MessageEmbed, Collection } from 'discord.js';
import { Plugin } from '../Plugin';
function convertPlugins(plugins: Collection<string, Plugin>) {
return plugins
.map((plugin, key) => ({
name: key,
cmds: [...plugin.values()]
.filter(cmd => !cmd.options.hidden)
.map(cmd => cmd.name)
.sort((a, b) => a.localeCompare(b)),
}))
.sort((a, b) => a.name.localeCompare(b.name));
}
export const command = new Command( export const command = new Command(
'help', 'help',
(lifeguard, msg, args) => { (lifeguard, msg, args) => {
const cmds = Array.from(lifeguard.plugins.values()) if (!args.length) {
.map(plugin => Array.from(plugin.values())) const plugins = convertPlugins(lifeguard.plugins);
.reduce((acc, val) => acc.concat(val), [])
.filter(cmd => !cmd.options.hidden) const embed = new MessageEmbed()
.sort((a, b) => a.name.localeCompare(b.name)); .setTitle('Lifeguard Help')
msg.channel.send(cmds.map(c => c.name).join('\n')); .setColor(0x7289da)
.setFooter(
`Executed By ${msg.author.tag}`,
msg.author.avatarURL() ?? msg.author.defaultAvatarURL
)
.setTimestamp();
for (const plugin of plugins) {
embed.addField(plugin.name, plugin.cmds.join('\n'));
}
msg.channel.send(embed);
} else {
const plugin = lifeguard.plugins.find(plugin => plugin.has(args[0]));
const cmd = plugin?.get(args[0]);
if (cmd) {
const embed = new MessageEmbed()
.setTitle(cmd.name)
.setColor(0x7289da)
.setFooter(
`Executed By ${msg.author.tag}`,
msg.author.avatarURL() ?? msg.author.defaultAvatarURL
)
.setTimestamp();
const options = Object.entries(cmd.options);
options.map(([key, val]) => {
if (key === 'usage') {
embed.addField(key, val.join('\n'));
return;
}
embed.addField(key, `${val}`);
});
msg.channel.send(embed);
}
}
}, },
{ {
level: 0, level: 0,