feat: #206 convert to interactions (buttons and application commands) (#238)

* feat: make command handler slash command-ready

Only `help` and `settings` commands work so far

* feat(commands): finish new settings command

* fix(settings): convert `roles` and `ping` to an array

* fix(commands): make `add` a slash command

* fix(commands): make `blacklist` a slash command

* fix(commands): remove URLs from `help` command

* Add weblate badge and overview image

* Update sponsors

* sqlite things

* imrpovements

* update eslint rules

* (⚠ untested) close command

* fix: default locale for getting command option names

* Update README.md

* Update README.md

* Update README.md

* update new command to slash commands and fix close command

* fixes and improvements

* fix: closing a ticket when the creator has left

* Revert "fix: closing a ticket when the creator has left"

This reverts commit afc40ae17077782e344fd8cee03a089966c2347e.

* fix: closing a ticket when the creator has left

* fix: localisation issues in settings command

* fix: delete category channel

* New button and select panels + updated message panels

Includes new options for panel embed message image and thumbnail

Co-Authored-By: Puneet Gopinath <baalkrshna@gmail.com>
Co-Authored-By: thevisuales <6569806+thevisuales@users.noreply.github.com>

* Finish converting to buttons, added close button

Co-Authored-By: Puneet Gopinath <baalkrshna@gmail.com>
Co-Authored-By: thevisuales <6569806+thevisuales@users.noreply.github.com>

* fully convert to slash commands

* re-add "... has created a ticket" message

* locales

* fix add and remove commands

* fix remove command

* fix stats command

* eslint

Co-authored-by: Puneet Gopinath <baalkrshna@gmail.com>
Co-authored-by: thevisuales <6569806+thevisuales@users.noreply.github.com>
This commit is contained in:
Isaac
2021-09-24 15:32:36 +01:00
committed by GitHub
parent 39e2cef38d
commit 95f76716ed
38 changed files with 2481 additions and 2268 deletions

View File

@@ -1,6 +1,6 @@
const {
Message, // eslint-disable-line no-unused-vars
MessageEmbed
Interaction // eslint-disable-line no-unused-vars
} = require('discord.js');
/**
@@ -9,11 +9,13 @@ const {
module.exports = class Command {
/**
*
* @typedef CommandArgument
* @property {string} name - The argument's name
* @property {string} description - The argument's description
* @property {string} example - An example value
* @property {boolean?} required - Is this arg required? Defaults to `false`
* @typedef CommandOption
* @property {string} name - The option's name
* @property {number} type - The option's type (use `Command.option_types`)
* @property {string} description - The option's description
* @property {CommandOption[]} [options] - The option's options
* @property {(string|number)[]} [choices] - The option's choices
* @property {boolean} [required] - Is this arg required? Defaults to `false`
*/
/**
* Create a new Command
@@ -23,8 +25,7 @@ module.exports = class Command {
* @param {string} data.description - The description of the command (1-100)
* @param {boolean} [data.staff_only] - Only allow staff to use this command?
* @param {string[]} [data.permissions] - Array of permissions needed for a user to use this command
* @param {boolean} [data.process_args] - Should the command handler process named arguments?
* @param {CommandArgument[]} [data.args] - The command's arguments (see [docs](https://github.com/75lb/command-line-args/blob/master/doc/option-definition.md) if using processed args)
* @param {CommandOption[]} [data.options] - The command's options
*/
constructor(client, data) {
@@ -44,14 +45,6 @@ module.exports = class Command {
*/
this.name = data.name;
/**
* The command's aliases
* @type {string[]}
*/
this.aliases = data.aliases ?? [];
if (!this.aliases.includes(this.name)) this.aliases.unshift(this.name);
/**
* The command description
* @type {string}
@@ -71,18 +64,11 @@ module.exports = class Command {
*/
this.permissions = data.permissions ?? [];
/**
* Should the command handler process named arguments?
* @type {boolean}
* @default false
*/
this.process_args = data.process_args === true;
/**
* The command options
* @type {CommandArgument[]}
* @type {CommandOption[]}
*/
this.args = data.args ?? [];
this.options = data.options ?? [];
/**
* True if command is internal, false if it is from a plugin
@@ -100,64 +86,40 @@ module.exports = class Command {
try {
this.manager.register(this); // register the command
} catch (e) {
return this.client.log.error(e);
} catch (error) {
return this.client.log.error(error);
}
}
/**
* The code to be executed when a command is invoked
* @abstract
* @param {Message} message - The message that invoked this command
* @param {(object|string)} [args] - Named command arguments, or the message content with the prefix and command removed
* @param {Interaction} interaction - The message that invoked this command
*/
async execute(message, args) { } // eslint-disable-line no-unused-vars
async execute(interaction) { } // eslint-disable-line no-unused-vars
/**
* Send a message with the command usage
* @param {TextChannel} channel - The channel to send the message to
* @param {string} [alias] - The command alias
* @returns {Promise<Message>}
*/
async sendUsage(channel, alias) {
const settings = await this.client.utils.getSettings(channel.guild);
if (!alias) alias = this.name;
const prefix = settings.command_prefix;
const i18n = this.client.i18n.getLocale(settings.locale);
const addArgs = (embed, arg) => {
const required = arg.required ? '`❗` ' : '';
let description = `» ${i18n('cmd_usage.args.description', arg.description)}`;
if (arg.example) description += `\n» ${i18n('cmd_usage.args.example', arg.example)}`;
embed.addField(required + arg.name, description);
async build(guild) {
return {
defaultPermission: !this.staff_only,
description: this.description,
name: this.name,
options: typeof this.options === 'function' ? await this.options(guild) : this.options
};
}
let usage,
example,
embed;
if (this.process_args) {
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', alias))
.setDescription(i18n('cmd_usage.named_args') + i18n('cmd_usage.description', usage, example));
} else {
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', alias))
.setDescription(i18n('cmd_usage.description', usage, example));
}
this.args.forEach(arg => addArgs(embed, arg));
return await channel.send({ embeds: [embed] });
static get option_types() {
return {
SUB_COMMAND: 1,
SUB_COMMAND_GROUP: 2,
STRING: 3, // eslint-disable-line sort-keys
INTEGER: 4, // eslint-disable-line sort-keys
BOOLEAN: 5, // eslint-disable-line sort-keys
USER: 6,
CHANNEL: 7, // eslint-disable-line sort-keys
ROLE: 8,
MENTIONABLE: 9, // eslint-disable-line sort-keys
NUMBER: 10
};
}
};

View File

@@ -2,16 +2,13 @@
const {
Client, // eslint-disable-line no-unused-vars
Collection,
Message, // eslint-disable-line no-unused-vars
Interaction, // eslint-disable-line no-unused-vars
MessageEmbed
} = require('discord.js');
const fs = require('fs');
const { path } = require('../../utils/fs');
const { parseArgsStringToArgv: argv } = require('string-argv');
const parseArgs = require('command-line-args');
/**
* Manages the loading and execution of commands
*/
@@ -48,81 +45,127 @@ module.exports = class CommandManager {
}
/** Register a command */
register(cmd) {
const exists = this.commands.has(cmd.name);
const is_internal = (exists && cmd.internal) || (exists && this.commands.get(cmd.name).internal);
register(command) {
const exists = this.commands.has(command.name);
const is_internal = (exists && command.internal) || (exists && this.commands.get(command.name).internal);
if (is_internal) {
const plugin = this.client.plugins.plugins.find(p => p.commands.includes(cmd.name));
if (plugin) this.client.log.commands(`The "${plugin.name}" plugin has overridden the internal "${cmd.name}" command`);
else this.client.log.commands(`An unknown plugin has overridden the internal "${cmd.name}" command`);
if(cmd.internal) return;
const plugin = this.client.plugins.plugins.find(p => p.commands.includes(command.name));
if (plugin) this.client.log.commands(`The "${plugin.name}" plugin has overridden the internal "${command.name}" command`);
else this.client.log.commands(`An unknown plugin has overridden the internal "${command.name}" command`);
if(command.internal) return;
} else if (exists) {
throw new Error(`A non-internal command with the name "${cmd.name}" already exists`);
throw new Error(`A non-internal command with the name "${command.name}" already exists`);
}
this.commands.set(cmd.name, cmd);
this.client.log.commands(`Loaded "${cmd.name}" command`);
this.commands.set(command.name, command);
this.client.log.commands(`Loaded "${command.name}" command`);
}
async publish(guild) {
if (!guild) {
return this.client.guilds.cache.forEach(guild => {
this.publish(guild);
});
}
try {
const commands = await Promise.all(this.client.commands.commands.map(async command => await command.build(guild)));
await this.client.application.commands.set(commands, guild.id);
await this.updatePermissions(guild);
this.client.log.success(`Published ${this.client.commands.commands.size} commands to "${guild.name}"`);
} catch (error) {
this.client.log.warn('An error occurred whilst publishing the commands');
this.client.log.error(error);
}
}
async updatePermissions(guild) {
guild.commands.fetch().then(async commands => {
const permissions = [];
const settings = await this.client.utils.getSettings(guild.id);
const blacklist = [];
settings.blacklist.users?.forEach(userId => {
blacklist.push({
id: userId,
permission: false,
type: 'USER'
});
});
settings.blacklist.roles?.forEach(roleId => {
blacklist.push({
id: roleId,
permission: false,
type: 'ROLE'
});
});
const categories = await this.client.db.models.Category.findAll({ where: { guild: guild.id } });
const staff_roles = new Set(categories.map(category => category.roles).flat());
commands.forEach(async g_cmd => {
const cmd_permissions = [...blacklist];
const command = this.client.commands.commands.get(g_cmd.name);
if (command.staff_only) {
cmd_permissions.push({
id: guild.roles.everyone.id,
permission: false,
type: 'ROLE'
});
staff_roles.forEach(roleId => {
cmd_permissions.push({
id: roleId,
permission: true,
type: 'ROLE'
});
});
}
permissions.push({
id: g_cmd.id,
permissions: cmd_permissions
});
});
this.client.log.debug(`Command permissions for "${guild.name}"`, require('util').inspect(permissions, {
colors: true,
depth: 10
}));
try {
await guild.commands.permissions.set({ fullPermissions: permissions });
} catch (error) {
this.client.log.warn('An error occurred whilst updating command permissions');
this.client.log.error(error);
}
});
}
/**
* Execute a command
* @param {Message} message - Command message
* @param {Interaction} interaction - Command message
*/
async handle(message) {
if (message.author.bot) return; // ignore self and other bots
const settings = await this.client.utils.getSettings(message.guild);
async handle(interaction) {
if (!interaction.guild) return this.client.log.debug('Ignoring non-guild command interaction');
const settings = await this.client.utils.getSettings(interaction.guild.id);
const i18n = this.client.i18n.getLocale(settings.locale);
const prefix = settings.command_prefix;
const escaped_prefix = prefix.toLowerCase().replace(/(?=\W)/g, '\\'); // (lazy) escape every character so it can be used in a RexExp
const client_mention = `<@!?${this.client.user.id}>`;
let cmd_name = message.content.match(new RegExp(`^(${escaped_prefix}|${client_mention}\\s?)(\\S+)`, 'mi')); // capture prefix and command
if (!cmd_name) return; // stop here if the message is not a command
const command = this.commands.get(interaction.commandName);
if (!command) return;
const raw_args = message.content.replace(cmd_name[0], '').trim(); // remove the prefix and command
cmd_name = cmd_name[2].toLowerCase(); // set cmd_name to the actual command alias, effectively removing the prefix
const cmd = this.commands.find(cmd => cmd.aliases.includes(cmd_name));
if (!cmd) return;
let is_blacklisted = false;
if (settings.blacklist.includes(message.author.id)) {
is_blacklisted = true;
this.client.log.info(`Ignoring blacklisted member ${message.author.tag}`);
} else {
settings.blacklist.forEach(element => {
if (message.guild.roles.cache.has(element) && message.member.roles.cache.has(element)) {
is_blacklisted = true;
this.client.log.info(`Ignoring member ${message.author.tag} with blacklisted role`);
}
});
}
if (is_blacklisted) {
try {
return message.react('❌');
} catch (error) {
return this.client.log.warn('Failed to react to a message');
}
}
const bot_permissions = message.guild.me.permissionsIn(message.channel);
const bot_permissions = interaction.guild.me.permissionsIn(interaction.channel);
const required_bot_permissions = [
'ADD_REACTIONS',
'ATTACH_FILES',
'EMBED_LINKS',
'MANAGE_CHANNELS',
'MANAGE_MESSAGES',
'READ_MESSAGE_HISTORY',
'SEND_MESSAGES'
'MANAGE_MESSAGES'
];
if (!bot_permissions.has(required_bot_permissions)) {
const perms = required_bot_permissions.map(p => `\`${p}\``).join(', ');
if (bot_permissions.has(['EMBED_LINKS', 'SEND_MESSAGES'])) {
await message.channel.send({
if (bot_permissions.has('EMBED_LINKS')) {
await interaction.reply({
embeds: [
new MessageEmbed()
.setColor('ORANGE')
@@ -130,76 +173,33 @@ module.exports = class CommandManager {
.setDescription(i18n('bot.missing_permissions.description', perms))
]
});
} else if (bot_permissions.has('SEND_MESSAGES')) {
await message.channel.send({ content: '⚠️ ' + i18n('bot.missing_permissions.description', perms) });
} else if (bot_permissions.has('ADD_REACTIONS')) {
await message.react('⚠️');
} else {
this.client.log.warn('Unable to respond to command due to missing permissions');
await interaction.reply({ content: i18n('bot.missing_permissions.description', perms) });
}
return;
}
const missing_permissions = cmd.permissions instanceof Array && !message.member.permissions.has(cmd.permissions);
const missing_permissions = command.permissions instanceof Array && !interaction.member.permissions.has(command.permissions);
if (missing_permissions) {
const perms = cmd.permissions.map(p => `\`${p}\``).join(', ');
return await message.channel.send({
const perms = command.permissions.map(p => `\`${p}\``).join(', ');
return await interaction.reply({
embeds: [
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('missing_permissions.title'))
.setDescription(i18n('missing_permissions.description', perms))
]
],
ephemeral: true
});
}
if (cmd.staff_only && await this.client.utils.isStaff(message.member) === false) {
return await message.channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('staff_only.title'))
.setDescription(i18n('staff_only.description'))
]
});
}
let args = raw_args;
if (cmd.process_args) {
try {
args = parseArgs(cmd.args, { argv: argv(raw_args) });
} catch (error) {
const help_cmd = `${settings.command_prefix}${i18n('commands.help.name')} ${cmd_name}`;
return await message.channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.error_colour)
.setTitle(i18n('cmd_usage.invalid_named_args.title'))
.setDescription(i18n('cmd_usage.invalid_named_args.description', error.message, help_cmd))
]
});
}
for (const arg of cmd.args) {
if (arg.required && args[arg.name] === undefined) {
return await cmd.sendUsage(message.channel, cmd_name); // send usage if any required arg is missing
}
}
} else {
const args_num = raw_args.split(/\s/g).filter(arg => arg.length !== 0).length; // count the number of single-word args were given
const required_args = cmd.args.reduce((acc, arg) => arg.required ? acc + 1 : acc, 0); // count how many of the args are required
if (args_num < required_args) {
return await cmd.sendUsage(message.channel, cmd_name);
}
}
try {
this.client.log.commands(`Executing "${cmd.name}" command (invoked by ${message.author.tag})`);
await cmd.execute(message, args); // execute the command
this.client.log.commands(`Executing "${command.name}" command (invoked by ${interaction.user.tag})`);
await command.execute(interaction); // 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 ${command.name} command`);
this.client.log.error(e);
await message.channel.send({
await interaction.reply({
embeds: [
new MessageEmbed()
.setColor('ORANGE')

View File

@@ -1,7 +1,11 @@
/* eslint-disable max-lines */
const EventEmitter = require('events');
const TicketArchives = require('./archives');
const { MessageEmbed } = require('discord.js');
const {
MessageActionRow,
MessageButton,
MessageEmbed
} = require('discord.js');
/** Manages tickets */
module.exports = class TicketManager extends EventEmitter {
@@ -70,23 +74,13 @@ module.exports = class TicketManager extends EventEmitter {
});
(async () => {
const settings = await this.client.utils.getSettings(guild);
const settings = await this.client.utils.getSettings(guild.id);
const i18n = this.client.i18n.getLocale(settings.locale);
topic = t_row.topic
? this.client.cryptr.decrypt(t_row.topic)
: '';
if (cat_row.ping instanceof Array && cat_row.ping.length > 0) {
const mentions = cat_row.ping.map(id => id === 'everyone'
? '@everyone'
: id === 'here'
? '@here'
: `<@&${id}>`);
await t_channel.send({ content: mentions.join(', ') });
}
if (cat_row.image) {
await t_channel.send({ content: cat_row.image });
}
@@ -102,8 +96,39 @@ module.exports = class TicketManager extends EventEmitter {
if (topic) embed.addField(i18n('ticket.opening_message.fields.topic'), topic);
const components = new MessageActionRow();
if (cat_row.claiming) {
components.addComponents(
new MessageButton()
.setCustomId('ticket.claim')
.setLabel(i18n('ticket.claim'))
.setEmoji('🙌')
.setStyle('SECONDARY')
);
}
if (settings.close_button) {
components.addComponents(
new MessageButton()
.setCustomId('ticket.close')
.setLabel(i18n('ticket.close'))
.setEmoji('✖️')
.setStyle('DANGER')
);
}
const mentions = cat_row.ping instanceof Array && cat_row.ping.length > 0
? cat_row.ping.map(id => id === 'everyone'
? '@everyone'
: id === 'here'
? '@here'
: `<@&${id}>`)
.join(', ')
: '';
const sent = await t_channel.send({
content: creator.user.toString(),
components: [components],
content: i18n('ticket.opening_message.content', mentions, creator.user.toString()),
embeds: [embed]
});
await sent.pin({ reason: 'Ticket opening message' });
@@ -118,10 +143,6 @@ module.exports = class TicketManager extends EventEmitter {
.catch(() => this.client.log.warn('Failed to delete system pin message'));
}
if (cat_row.claiming) {
await sent.react('🙌');
}
let questions;
if (cat_row.opening_questions) {
questions = cat_row.opening_questions
@@ -134,7 +155,7 @@ module.exports = class TicketManager extends EventEmitter {
embeds: [
new MessageEmbed()
.setColor(settings.colour)
.setTitle('⚠️ ' + i18n('commands.new.request_topic.title'))
.setTitle(i18n('commands.new.request_topic.title'))
.setDescription(i18n('commands.new.request_topic.description'))
.setFooter(this.client.utils.footer(settings.footer, i18n('collector_expires_in', 120)), guild.iconURL())
]
@@ -213,7 +234,7 @@ module.exports = class TicketManager extends EventEmitter {
this.emit('beforeClose', ticket_id);
const guild = this.client.guilds.cache.get(t_row.guild);
const settings = await this.client.utils.getSettings(guild);
const settings = await this.client.utils.getSettings(guild.id);
const i18n = this.client.i18n.getLocale(settings.locale);
const channel = await this.client.channels.fetch(t_row.id);
@@ -273,98 +294,102 @@ module.exports = class TicketManager extends EventEmitter {
};
if (channel) {
const creator = await guild.members.fetch(t_row.creator);
guild.members.fetch(t_row.creator)
.then(async creator => {
const cat_row = await this.client.db.models.Category.findOne({ where: { id: t_row.category } });
if (creator && cat_row.survey) {
const survey = await this.client.db.models.Survey.findOne({
where: {
guild: t_row.guild,
name: cat_row.survey
}
});
const cat_row = await this.client.db.models.Category.findOne({ where: { id: t_row.category } });
if (creator && cat_row.survey) {
const survey = await this.client.db.models.Survey.findOne({
where: {
guild: t_row.guild,
name: cat_row.survey
}
});
if (survey) {
const r_collector_message = await channel.send({
content: creator.toString(),
embeds: [
new MessageEmbed()
.setColor(settings.colour)
.setTitle(i18n('ticket.survey.start.title'))
.setDescription(i18n('ticket.survey.start.description', creator.toString(), survey.questions.length))
.setFooter(i18n('collector_expires_in', 60))
]
});
await r_collector_message.react('✅');
const filter = (reaction, user) => user.id === creator.user.id && reaction.emoji.name === '✅';
const r_collector = r_collector_message.createReactionCollector({
filter,
time: 60000
});
r_collector.on('collect', async () => {
r_collector.stop();
const filter = message => message.author.id === creator.id;
let answers = [];
let number = 1;
for (const question of survey.questions) {
await channel.send({
if (survey) {
const r_collector_message = await channel.send({
content: creator.toString(),
embeds: [
new MessageEmbed()
.setColor(settings.colour)
.setTitle(`${number++}/${survey.questions.length}`)
.setDescription(question)
.setTitle(i18n('ticket.survey.start.title'))
.setDescription(i18n('ticket.survey.start.description', creator.toString(), survey.questions.length))
.setFooter(i18n('collector_expires_in', 60))
]
});
try {
const collected = await channel.awaitMessages({
errors: ['time'],
filter,
max: 1,
time: 60000
await r_collector_message.react('✅');
const filter = (reaction, user) => user.id === creator.user.id && reaction.emoji.name === '✅';
const r_collector = r_collector_message.createReactionCollector({
filter,
time: 60000
});
r_collector.on('collect', async () => {
r_collector.stop();
const filter = message => message.author.id === creator.id;
let answers = [];
let number = 1;
for (const question of survey.questions) {
await channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.colour)
.setTitle(`${number++}/${survey.questions.length}`)
.setDescription(question)
.setFooter(i18n('collector_expires_in', 60))
]
});
try {
const collected = await channel.awaitMessages({
errors: ['time'],
filter,
max: 1,
time: 60000
});
answers.push(collected.first().content);
} catch (collected) {
return await close();
}
}
await channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.success_colour)
.setTitle(i18n('ticket.survey.complete.title'))
.setDescription(i18n('ticket.survey.complete.description'))
.setFooter(settings.footer, guild.iconURL())
]
});
answers.push(collected.first().content);
} catch (collected) {
return await close();
}
answers = answers.map(a => this.client.cryptr.encrypt(a));
await this.client.db.models.SurveyResponse.create({
answers,
survey: survey.id,
ticket: t_row.id
});
await close();
});
r_collector.on('end', async collected => {
if (collected.size === 0) {
await close();
}
});
}
await channel.send({
embeds: [
new MessageEmbed()
.setColor(settings.success_colour)
.setTitle(i18n('ticket.survey.complete.title'))
.setDescription(i18n('ticket.survey.complete.description'))
.setFooter(settings.footer, guild.iconURL())
]
});
answers = answers.map(a => this.client.cryptr.encrypt(a));
await this.client.db.models.SurveyResponse.create({
answers,
survey: survey.id,
ticket: t_row.id
});
} else {
await close();
});
r_collector.on('end', async collected => {
if (collected.size === 0) {
await close();
}
});
}
} else {
await close();
}
}
})
.catch(async () => {
this.client.log.debug('Skipping survey as member has left');
await close();
});
}
this.emit('close', ticket_id);