Skip to content

Commit

Permalink
Merge pull request #319 from aternosorg/help
Browse files Browse the repository at this point in the history
refactor help command, remove legacy commands
  • Loading branch information
JulianVennen authored Jul 5, 2021
2 parents a756303 + d6bb4f3 commit 616b8b3
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 226 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "modbot",
"version": "0.5.0",
"version": "1.0.0",
"description": "Discord Bot for the Aternos Discord server",
"main": "index.js",
"scripts": {
Expand Down
13 changes: 11 additions & 2 deletions src/Command.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ class Command {
.addFields(
/** @type {any} */ { name: 'Usage', value: `\`${prefix}${cmd} ${this.usage}\``, inline: true},
/** @type {any} */ { name: 'Description', value: this.description, inline: true},
/** @type {any} */ { name: 'Required Permissions', value: this.userPerms.length !== 0 ? `\`${this.userPerms.join('`, `')}\`` : 'none' }
/** @type {any} */ { name: 'Required Permissions', value: this.userPerms.length !== 0 ? `\`${this.userPerms.join('`, `')}\`` : 'none', inline: true }
)
.setColor(util.color.green)
.setColor(util.color.red)
.setTimestamp();
if (this.comment) {
embed.addFields(
Expand Down Expand Up @@ -251,6 +251,15 @@ class Command {
await message.reactions.removeAll();
}
}

/**
* get an overview of this command
* @return {string}
*/
static getOverview() {
return `**${this.names.join(', ')}**\n`+
`${this.description}\n`;
}
}

module.exports = Command;
105 changes: 0 additions & 105 deletions src/commands/legacy/help.js

This file was deleted.

File renamed without changes.
55 changes: 55 additions & 0 deletions src/commands/utility/HelpCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const Command = require('../../Command');
const {MessageEmbed} = require('discord.js');
const util = require('../../util');

class HelpCommand extends Command {

static description = 'View command information';

static usage = '[<category|command>]';

static names = ['help'];

async execute() {

const commandManager = require('../../features/message/CommandManager');

const categories = commandManager.getCategories();
const commands = commandManager.getCommands();

if (!this.args.length) {
const embed = new MessageEmbed()
.setColor(util.color.red)
.setFooter(`View a command or category using ${this.prefix}help <category|command>`);
for (const [key, commands] of categories.entries()) {
if (commands.length === 0) continue;
embed.addField(util.toTitleCase(key), commands.map(c => c.names).flat().sort().join(', '));
}
return this.message.channel.send(embed);
}

const name = this.args.shift().toLowerCase();
const category = categories.get(name);
const command = commands.get(name);
if (!category && !command) return this.sendUsage();

if (category) {
let description = '';

for (const command of category) {
description += command.getOverview() + '\n';
}

return this.message.channel.send(new MessageEmbed()
.setTitle(`ModBot ${util.toTitleCase(name)} Commands:`)
.setColor(util.color.red)
.setDescription(description)
.setFooter(`View a command using ${this.prefix}help <command>`)
);
}

if (command) return this.message.channel.send(await command.getUsage(this.message, name, this.guildConfig));
}
}

module.exports = HelpCommand;
45 changes: 35 additions & 10 deletions src/features/message/CommandManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ const defaultPrefix = require('../../../config.json').prefix;
const Discord = require('discord.js');
const util = require('../../util');
const GuildConfig = require('../../GuildConfig');
const {Collection} = Discord;
const {APIErrors} = Discord.Constants;

const monitor = require('../../Monitor').getInstance();

class CommandManager {

/**
* loaded commands
* @type {Object}
* command categories
* @type {module:"discord.js".Collection<String, Class<Command>[]>}
*/
static #categories = new Collection();

/**
* loaded commands (name => class)
* @type {module:"discord.js".Collection<String, Class<Command>>}
* @private
*/
static #commands = this._loadCommands();
Expand All @@ -21,35 +28,53 @@ class CommandManager {
* @private
*/
static _loadCommands() {
const commands = {};
const commands = new Collection();
for (const folder of fs.readdirSync(`${__dirname}/../../commands`)) {

const category = [];

const dirPath = `${__dirname}/../../commands/${folder}`;
if (!fs.lstatSync(dirPath).isDirectory() || folder === 'legacy') continue;
if (!fs.lstatSync(dirPath).isDirectory()) continue;
for (const file of fs.readdirSync(dirPath)) {
const path = `${dirPath}/${file}`;
if (!file.endsWith('.js') || !fs.lstatSync(path).isFile()) {
continue;
}
try {
const command = require(path);
category.push(command);
for (const name of command.names) {
if (commands[name]) {
if (commands.has(name)) {
console.error(`Two command registered the name '${name}':`);
console.error(`- ${commands[name].path}`);
console.error(`- ${commands.get(name).path}`);
console.error(`- ${folder}/${file}`);
}
command.path = `${folder}/${file}`;
commands[name] = command;
commands.set(name, command);
}
} catch (e) {
monitor.error(`Failed to load command '${folder}/${file}'`, e);
console.error(`Failed to load command '${folder}/${file}'`, e);
}
}

this.#categories.set(folder, category);
}
return commands;
}

/**
* get command categories
* @return {module:"discord.js".Collection<String, Class<Command>[]>}
*/
static getCategories() {
return this.#categories;
}

/**
* get all commands (name => class)
* @return {module:"discord.js".Collection<String, Class<Command>>}
*/
static getCommands() {
return this.#commands;
}
Expand All @@ -64,7 +89,7 @@ class CommandManager {
*/
static async event(options, message) {
const {isCommand, name, prefix} = await this.getCommandName(message);
const Command = this.#commands[name];
const Command = this.#commands.get(name);
if (!isCommand || Command === undefined) return;

try {
Expand Down Expand Up @@ -129,7 +154,7 @@ class CommandManager {
static async isCommand(message) {
const {isCommand, name} = await this.getCommandName(message);
if (!isCommand) return false;
return this.#commands[name] !== undefined;
return this.#commands.has(name);
}
}

Expand Down
83 changes: 0 additions & 83 deletions src/features/message/legacyCommands.js

This file was deleted.

Loading

0 comments on commit 616b8b3

Please sign in to comment.