mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2024-11-09 14:23:08 +02:00
Command handling, arguments, and "new" command stuff
This commit is contained in:
parent
1e3721b5da
commit
3def7e290d
@ -14,11 +14,6 @@ module.exports = class NewCommand extends Command {
|
|||||||
],
|
],
|
||||||
process_args: false,
|
process_args: false,
|
||||||
args: [
|
args: [
|
||||||
{
|
|
||||||
name: i18n('commands.new.args.category.name'),
|
|
||||||
description: i18n('commands.new.args.topic.description'),
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: i18n('commands.new.args.topic.name'),
|
name: i18n('commands.new.args.topic.name'),
|
||||||
description: i18n('commands.new.args.topic.description'),
|
description: i18n('commands.new.args.topic.description'),
|
||||||
@ -33,11 +28,26 @@ module.exports = class NewCommand extends Command {
|
|||||||
let settings = await message.guild.settings;
|
let settings = await message.guild.settings;
|
||||||
const i18n = this.client.i18n.get(settings.locale);
|
const i18n = this.client.i18n.get(settings.locale);
|
||||||
|
|
||||||
await message.channel.send(
|
let { count: cat_count, rows: categories } = await this.client.db.models.Category.findAndCountAll({
|
||||||
new MessageEmbed()
|
where: {
|
||||||
.setColor(settings.colour)
|
guild: message.guild.id
|
||||||
.setTitle(i18n('bot.version', require('../../package.json').version))
|
}
|
||||||
);
|
});
|
||||||
|
|
||||||
|
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);
|
// this.client.tickets.create(message.guild.id, message.member.id, '825861413687787560', args.topic);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,14 @@
|
|||||||
{
|
{
|
||||||
"bot": {
|
"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": {
|
"commands": {
|
||||||
"new": {
|
"new": {
|
||||||
@ -18,8 +26,14 @@
|
|||||||
"description": "The topic of the ticket"
|
"description": "The topic of the ticket"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"description": "Create a new support ticket",
|
"description": "Create a new ticket",
|
||||||
"name": "new"
|
"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": {
|
"settings": {
|
||||||
"aliases": {
|
"aliases": {
|
||||||
@ -32,6 +46,16 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"no_perm": "❌ You do not have the permissions required to use this command:\n%s",
|
"command_execution_error": {
|
||||||
"staff_only": "❌ You must be a member of staff to use this command."
|
"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."
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
// eslint-disable-next-line no-unused-vars
|
// 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
|
// eslint-disable-next-line no-unused-vars
|
||||||
const Command = require('./command');
|
const Command = require('./command');
|
||||||
|
|
||||||
@ -82,20 +82,54 @@ module.exports = class CommandManager {
|
|||||||
if (!cmd);
|
if (!cmd);
|
||||||
|
|
||||||
let args = raw_args;
|
let args = raw_args;
|
||||||
|
|
||||||
if (cmd.process_args) {
|
if (cmd.process_args) {
|
||||||
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]);
|
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);
|
const missing_perms = cmd.permissions instanceof Array && !message.member.hasPermission(cmd.permissions);
|
||||||
if (no_perm) {
|
if (missing_perms) {
|
||||||
let perms = cmd.permissions.map(p => `\`${p}\``).join(', ');
|
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) {
|
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({
|
let guild_categories = await this.client.db.models.Category.findAll({
|
||||||
where: {
|
where: {
|
||||||
guild: message.guild.id
|
guild: message.guild.id
|
||||||
@ -107,16 +141,28 @@ module.exports = class CommandManager {
|
|||||||
staff_roles = staff_roles.filter(r => message.member.roles.cache.has(r));
|
staff_roles = staff_roles.filter(r => message.member.roles.cache.has(r));
|
||||||
const not_staff = staff_roles.length === 0;
|
const not_staff = staff_roles.length === 0;
|
||||||
if (not_staff) {
|
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 {
|
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
|
await cmd.execute(message, args, raw_args); // execute the command
|
||||||
} catch (e) {
|
} 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);
|
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'))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user