From 37adac1dd0d0cf6ebd4fe13c7235f0f71437972b Mon Sep 17 00:00:00 2001 From: Isaac Date: Mon, 17 May 2021 21:16:58 +0100 Subject: [PATCH] "help" command --- src/commands/add.js | 8 +++++++- src/commands/blacklist.js | 8 +++++++- src/commands/close.js | 8 +++++++- src/commands/help.js | 36 ++++++++++++++++++++++++++++++++- src/commands/new.js | 8 +++++++- src/commands/panel.js | 8 +++++++- src/commands/remove.js | 8 +++++++- src/commands/settings.js | 2 +- src/commands/stats.js | 9 +++++++-- src/commands/topic.js | 8 +++++++- src/locales/en-GB.json | 12 +++++++++-- src/modules/commands/command.js | 23 +++++++++++---------- src/modules/commands/manager.js | 5 ++++- src/modules/plugins/manager.js | 5 ++++- 14 files changed, 122 insertions(+), 26 deletions(-) diff --git a/src/commands/add.js b/src/commands/add.js index 68e8400..7ad6337 100644 --- a/src/commands/add.js +++ b/src/commands/add.js @@ -1,5 +1,6 @@ const Command = require('../modules/commands/command'); -const { MessageEmbed } = require('discord.js'); +// eslint-disable-next-line no-unused-vars +const { MessageEmbed, Message } = require('discord.js'); module.exports = class AddCommand extends Command { constructor(client) { @@ -27,6 +28,11 @@ module.exports = class AddCommand extends Command { }); } + /** + * @param {Message} message + * @param {string} args + * @returns {Promise} + */ async execute(message, args) { let settings = await message.guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); diff --git a/src/commands/blacklist.js b/src/commands/blacklist.js index dbf4b68..3481628 100644 --- a/src/commands/blacklist.js +++ b/src/commands/blacklist.js @@ -1,5 +1,6 @@ const Command = require('../modules/commands/command'); -const { MessageEmbed } = require('discord.js'); +// eslint-disable-next-line no-unused-vars +const { MessageEmbed, Message } = require('discord.js'); module.exports = class BlacklistCommand extends Command { constructor(client) { @@ -24,6 +25,11 @@ module.exports = class BlacklistCommand extends Command { }); } + /** + * @param {Message} message + * @param {string} args + * @returns {Promise} + */ async execute(message, args) { let settings = await message.guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); diff --git a/src/commands/close.js b/src/commands/close.js index 4a936e6..5a20d9e 100644 --- a/src/commands/close.js +++ b/src/commands/close.js @@ -1,5 +1,6 @@ const Command = require('../modules/commands/command'); -const { MessageEmbed, MessageMentions } = require('discord.js'); +// eslint-disable-next-line no-unused-vars +const { MessageEmbed, MessageMentions, Message } = require('discord.js'); const { Op } = require('sequelize'); const toTime = require('to-time-monthsfork'); const { footer } = require('../utils/discord'); @@ -48,6 +49,11 @@ module.exports = class CloseCommand extends Command { }); } + /** + * @param {Message} message + * @param {*} args + * @returns {Promise} + */ async execute(message, args) { const arg_ticket = this.args[0].name; const arg_reason = this.args[1].name; diff --git a/src/commands/help.js b/src/commands/help.js index b6e7269..f796117 100644 --- a/src/commands/help.js +++ b/src/commands/help.js @@ -1,5 +1,6 @@ const Command = require('../modules/commands/command'); -const { MessageEmbed } = require('discord.js'); +// eslint-disable-next-line no-unused-vars +const { MessageEmbed, Message } = require('discord.js'); module.exports = class HelpCommand extends Command { constructor(client) { @@ -24,8 +25,41 @@ module.exports = class HelpCommand extends Command { }); } + /** + * @param {Message} message + * @param {string} args + * @returns {Promise} + */ async execute(message, args) { let settings = await message.guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); + + const cmd = this.manager.commands.find(command => command.aliases.includes(args.toLowerCase())); + + if (cmd) { + return await cmd.sendUsage(message.channel, args); + } else { + let commands = this.manager.commands.filter(async command => { + if (command.permissions.length >= 1) return !message.member.hasPermission(command.permissions); + else if (command.staff_only) return await message.member.isStaff(); + }); + let list = commands.map(command => { + // let description = command.description; + let description = command.description.length > 50 + ? command.description.substring(0, 50) + '...' + : command.description; + return `**\`${settings.command_prefix}${command.name}\` ·** ${description}`; + }); + return await message.channel.send( + new MessageEmbed() + .setColor(settings.colour) + .setTitle(i18n('commands.help.response.list.title')) + .setDescription(i18n('commands.help.response.list.description', { + prefix: settings.command_prefix, + })) + .addField(i18n('commands.help.response.list.fields.commands'), list.join('\n')) + .setFooter(settings.footer, message.guild.iconURL()) + ); + } } }; \ No newline at end of file diff --git a/src/commands/new.js b/src/commands/new.js index edcd6e5..4055ce3 100644 --- a/src/commands/new.js +++ b/src/commands/new.js @@ -1,5 +1,6 @@ const Command = require('../modules/commands/command'); -const { MessageEmbed } = require('discord.js'); +// eslint-disable-next-line no-unused-vars +const { MessageEmbed, Message } = require('discord.js'); const { footer } = require('../utils/discord'); const { letters } = require('../utils/emoji'); const { wait } = require('../utils'); @@ -28,6 +29,11 @@ module.exports = class NewCommand extends Command { }); } + /** + * @param {Message} message + * @param {string} args + * @returns {Promise} + */ async execute(message, args) { let settings = await message.guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); diff --git a/src/commands/panel.js b/src/commands/panel.js index 984cb24..4f2dc3e 100644 --- a/src/commands/panel.js +++ b/src/commands/panel.js @@ -1,5 +1,6 @@ const Command = require('../modules/commands/command'); -const { MessageEmbed } = require('discord.js'); +// eslint-disable-next-line no-unused-vars +const { MessageEmbed, Message } = require('discord.js'); const { some, wait } = require('../utils'); const { emojify } = require('node-emoji'); @@ -56,6 +57,11 @@ module.exports = class PanelCommand extends Command { }); } + /** + * @param {Message} message + * @param {*} args + * @returns {Promise} + */ async execute(message, args) { // localised command and arg names are a pain const arg_title = this.args[0].name; diff --git a/src/commands/remove.js b/src/commands/remove.js index e85448f..2263d10 100644 --- a/src/commands/remove.js +++ b/src/commands/remove.js @@ -1,5 +1,6 @@ const Command = require('../modules/commands/command'); -const { MessageEmbed } = require('discord.js'); +// eslint-disable-next-line no-unused-vars +const { MessageEmbed, Message } = require('discord.js'); module.exports = class RemoveCommand extends Command { constructor(client) { @@ -27,6 +28,11 @@ module.exports = class RemoveCommand extends Command { }); } + /** + * @param {Message} message + * @param {string} args + * @returns {Promise} + */ async execute(message, args) { let settings = await message.guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); diff --git a/src/commands/settings.js b/src/commands/settings.js index adfd1b8..b271567 100644 --- a/src/commands/settings.js +++ b/src/commands/settings.js @@ -26,7 +26,7 @@ module.exports = class SettingsCommand extends Command { /** * @param {Message} message - * @param {*} args + * @param {string} args * @returns {Promise} */ async execute(message) { diff --git a/src/commands/stats.js b/src/commands/stats.js index 88e1e4d..6b57268 100644 --- a/src/commands/stats.js +++ b/src/commands/stats.js @@ -1,6 +1,7 @@ const Command = require('../modules/commands/command'); const Keyv = require('keyv'); -const { MessageEmbed } = require('discord.js'); +// eslint-disable-next-line no-unused-vars +const { MessageEmbed, Message } = require('discord.js'); module.exports = class StatsCommand extends Command { constructor(client) { @@ -20,6 +21,11 @@ module.exports = class StatsCommand extends Command { }); } + /** + * @param {Message} message + * @param {string} args + * @returns {Promise} + */ async execute(message) { let settings = await message.guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); @@ -34,7 +40,6 @@ module.exports = class StatsCommand extends Command { guild: message.guild.id } }); - stats = { // maths tickets: tickets.count, messages: settings.log_messages diff --git a/src/commands/topic.js b/src/commands/topic.js index 36a1f15..a5484d7 100644 --- a/src/commands/topic.js +++ b/src/commands/topic.js @@ -1,5 +1,6 @@ const Command = require('../modules/commands/command'); -const { MessageEmbed } = require('discord.js'); +// eslint-disable-next-line no-unused-vars +const { MessageEmbed, Message } = require('discord.js'); module.exports = class TopicCommand extends Command { constructor(client) { @@ -21,6 +22,11 @@ module.exports = class TopicCommand extends Command { }); } + /** + * @param {Message} message + * @param {string} args + * @returns {Promise} + */ async execute(message, args) { let settings = await message.guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); diff --git a/src/locales/en-GB.json b/src/locales/en-GB.json index b608d42..7f6ecb6 100644 --- a/src/locales/en-GB.json +++ b/src/locales/en-GB.json @@ -181,7 +181,7 @@ "commands": "commands" }, "args": { - "member": { + "command": { "description": "The command to display information about", "example": "new", "name": "command" @@ -189,7 +189,15 @@ }, "description": "List commands you have access to, or find out more about a command", "name": "help", - "response": {} + "response": { + "list": { + "description": "The commands you have access to are listed below. For more information about a command, type `{prefix}help [command]`.To create a ticket, type `{prefix}new [topic]`.", + "fields": { + "commands": "Commands" + }, + "title": "❔ Help" + } + } }, "new": { "aliases": { diff --git a/src/modules/commands/command.js b/src/modules/commands/command.js index c106498..2e6afcd 100644 --- a/src/modules/commands/command.js +++ b/src/modules/commands/command.js @@ -1,4 +1,5 @@ -const { MessageEmbed } = require('discord.js'); +// eslint-disable-next-line no-unused-vars +const { MessageEmbed, Message } = require('discord.js'); /** * A command @@ -66,7 +67,7 @@ module.exports = class Command { * Array of permissions needed for a user to use this command * @type {string[]} */ - this.permissions = data.permissions; + this.permissions = data.permissions ?? []; /** * Should the command handler process named arguments? @@ -115,12 +116,12 @@ module.exports = class Command { /** * Send a message with the command usage * @param {TextChannel} channel - The channel to send the message to - * @param {string} [cmd_name] - The command alias + * @param {string} [alias] - The command alias * @returns {Message} */ - async sendUsage(channel, cmd_name) { + async sendUsage(channel, alias) { let settings = await channel.guild.settings; - if (!cmd_name) cmd_name = this.name; + if (!alias) alias = this.name; const prefix = settings.command_prefix; const i18n = this.client.i18n.getLocale(settings.locale); @@ -137,18 +138,18 @@ module.exports = class Command { embed; if (this.process_args) { - usage = `${prefix + cmd_name} ${this.args.map(arg => arg.required ? `<${arg.name}>` : `[${arg.name}]`).join(' ')}`; - example = `${prefix + cmd_name} \n${this.args.map(arg => `--${arg.name} ${arg.example || ''}`).join('\n')}`; + usage = `${prefix + alias} ${this.args.map(arg => arg.required ? `<${arg.name}>` : `[${arg.name}]`).join(' ')}`; + example = `${prefix + alias} \n${this.args.map(arg => `--${arg.name} ${arg.example || ''}`).join('\n')}`; embed = new MessageEmbed() .setColor(settings.error_colour) - .setTitle(i18n('cmd_usage.title', cmd_name)) + .setTitle(i18n('cmd_usage.title', alias)) .setDescription(i18n('cmd_usage.named_args') + i18n('cmd_usage.description', usage, example)); } else { - usage = `${prefix + cmd_name} ${this.args.map(arg => arg.required ? `<${arg.name}>` : `[${arg.name}]`).join(' ')}`; - example = `${prefix + cmd_name} ${this.args.map(arg => `${arg.example || ''}`).join(' ')}`; + usage = `${prefix + alias} ${this.args.map(arg => arg.required ? `<${arg.name}>` : `[${arg.name}]`).join(' ')}`; + example = `${prefix + alias} ${this.args.map(arg => `${arg.example || ''}`).join(' ')}`; embed = new MessageEmbed() .setColor(settings.error_colour) - .setTitle(i18n('cmd_usage.title', cmd_name)) + .setTitle(i18n('cmd_usage.title', alias)) .setDescription(i18n('cmd_usage.description', usage, example)); } diff --git a/src/modules/commands/manager.js b/src/modules/commands/manager.js index 4583899..7138642 100644 --- a/src/modules/commands/manager.js +++ b/src/modules/commands/manager.js @@ -19,7 +19,10 @@ module.exports = class CommandManager { /** The Discord Client */ this.client = client; - /** A discord.js Collection (Map) of loaded commands */ + /** + * A discord.js Collection (Map) of loaded commands + * @type {Collection} + */ this.commands = new Collection(); } diff --git a/src/modules/plugins/manager.js b/src/modules/plugins/manager.js index ab2953d..36cc26d 100644 --- a/src/modules/plugins/manager.js +++ b/src/modules/plugins/manager.js @@ -15,7 +15,10 @@ module.exports = class PluginManager { /** The Discord Client */ this.client = client; - /** A discord.js Collection (Map) of loaded plugins */ + /** + * A discord.js Collection (Map) of loaded plugins + * @type {Collection} + */ this.plugins = new Collection(); /** Array of official plugins to be used to check if a plugin is official */