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

@ -14,11 +14,6 @@ module.exports = class NewCommand extends Command {
],
process_args: false,
args: [
{
name: i18n('commands.new.args.category.name'),
description: i18n('commands.new.args.topic.description'),
required: true,
},
{
name: i18n('commands.new.args.topic.name'),
description: i18n('commands.new.args.topic.description'),
@ -33,11 +28,26 @@ module.exports = class NewCommand extends Command {
let settings = await message.guild.settings;
const i18n = this.client.i18n.get(settings.locale);
await message.channel.send(
new MessageEmbed()
.setColor(settings.colour)
.setTitle(i18n('bot.version', require('../../package.json').version))
);
let { count: cat_count, rows: categories } = await this.client.db.models.Category.findAndCountAll({
where: {
guild: message.guild.id
}
});
switch (cat_count) {
case 0:
return await message.channel.send(
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('commands.new.response.no_categories.title'))
.setDescription(i18n('commands.new.response.no_categories.description'))
);
case 1:
break;
default:
}
// this.client.tickets.create(message.guild.id, message.member.id, '825861413687787560', args.topic);
}

View File

@ -1,6 +1,14 @@
{
"bot": {
"version": "Discord Tickets v%s by eartharoid"
"version": "[Discord Tickets](%s) v%s by [eartharoid](%s)"
},
"cmd_usage": {
"title": "`%s` command usage",
"description": "**Usage:** %s"
},
"cmd_usage_named_args": {
"title": "`%s` command usage",
"description": "This command uses named arguments:\n`%s argName: value anotherArg: \"A string value\";`\n\n**Required arguments are prefixed with `❗`.**"
},
"commands": {
"new": {
@ -18,8 +26,14 @@
"description": "The topic of the ticket"
}
},
"description": "Create a new support ticket",
"name": "new"
"description": "Create a new ticket",
"name": "new",
"response": {
"no_categories": {
"title": "❌ Can't create ticket",
"description": "A server administrator must create at least one ticket category before a new ticket can be opened."
}
}
},
"settings": {
"aliases": {
@ -32,6 +46,16 @@
}
}
},
"no_perm": "❌ You do not have the permissions required to use this command:\n%s",
"staff_only": "❌ You must be a member of staff to use this command."
"command_execution_error": {
"title": "⚠️",
"description": "An unexpected error occurred during command execution.\nPlease ask a server administrator to check the console output / logs for details."
},
"missing_perms": {
"title": "❌",
"description": "You do not have the permissions required to use this command:\n%s"
},
"staff_only": {
"title": "❌",
"description": "You must be a member of staff to use this command."
}
}

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'))
);
}
}