Command handling, arguments, and "new" command stuff

This commit is contained in:
Isaac
2021-04-01 00:17:00 +01:00
parent 1e3721b5da
commit 3def7e290d
3 changed files with 104 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
// eslint-disable-next-line no-unused-vars
const { Collection, Client, Message } = require('discord.js');
const { Collection, Client, Message, MessageEmbed } = require('discord.js');
// eslint-disable-next-line no-unused-vars
const Command = require('./command');
@@ -82,20 +82,54 @@ module.exports = class CommandManager {
if (!cmd);
let args = raw_args;
if (cmd.process_args) {
args = {};
let data = [...raw_args.matchAll(/(\w+)\??\s?:\s?(["`'](.*)["`'];|[\w<>@!#]+)/gmi)];
let data = [ ...raw_args.matchAll(/(\w+)\??\s?:\s?(["`'](.*)["`'];|[\w<>@!#]+)/gmi) ];
data.forEach(arg => args[arg[1]] = arg[3] || arg[2]);
for (let arg of cmd.args) {
if (!args[arg]) {
let embed = new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('cmd_usage_named_args.title', cmd_name))
.setDescription(i18n('cmd_usage_named_args.description', settings.command_prefix + cmd_name));
cmd.args.forEach(a => {
let required = a.required ? '`❗` ' : '';
embed.addField(required + a.name, a.description);
});
return message.channel.send(embed);
}
}
} else {
const args_num = raw_args.split(' ').filter(arg => arg.length !== 0).length;
const required_args = cmd.args.reduce((acc, arg) => arg.required ? acc + 1 : acc, 0);
if (args_num < required_args) {
let usage = cmd.args.map(arg => arg.required ? `<${arg.name}>` : `[${arg.name}]`).join(' ');
let embed = new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('cmd_usage.title', cmd_name))
.setDescription(i18n('cmd_usage.description', `\`${settings.command_prefix + cmd_name} ${usage}\``));
cmd.args.forEach(a => {
let required = a.required ? '`❗` ' : '';
embed.addField(required + a.name, a.description);
});
return message.channel.send(embed);
}
}
const no_perm = cmd.permissions instanceof Array && !message.member.hasPermission(cmd.permissions);
if (no_perm) {
const missing_perms = cmd.permissions instanceof Array && !message.member.hasPermission(cmd.permissions);
if (missing_perms) {
let perms = cmd.permissions.map(p => `\`${p}\``).join(', ');
return message.channel.send(i18n('no_perm', perms));
return await message.channel.send(
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('missing_perms.title'))
.setDescription(i18n('missing_perms.description', perms))
);
}
if (cmd.staff_only) {
let staff_roles = new Set(); // eslint-disable-line no-undef
let staff_roles = new Set();
let guild_categories = await this.client.db.models.Category.findAll({
where: {
guild: message.guild.id
@@ -107,16 +141,28 @@ module.exports = class CommandManager {
staff_roles = staff_roles.filter(r => message.member.roles.cache.has(r));
const not_staff = staff_roles.length === 0;
if (not_staff) {
return message.channel.send(i18n('staff_only'));
return await message.channel.send(
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('staff_only.title'))
.setDescription(i18n('staff_only.description'))
);
}
}
try {
this.client.log.commands(`Executing "${cmd_name}" command (invoked by ${message.author.tag})`);
this.client.log.commands(`Executing "${cmd.name}" command (invoked by ${message.author.tag})`);
await cmd.execute(message, args, raw_args); // execute the command
} catch (e) {
this.client.log.warn(`An error occurred whilst executing the ${cmd_name} command`);
this.client.log.warn(`An error occurred whilst executing the ${cmd.name} command`);
this.client.log.error(e);
// await message.channel.send(i18n('command_execution_error'));
await message.channel.send(
new MessageEmbed()
.setColor('ORANGE')
.setTitle(i18n('command_execution_error.title'))
.setDescription(i18n('command_execution_error.description'))
);
}
}