Added conventional (non-slash) commands support for settings command

Settings server plugin postponed.
This commit is contained in:
Isaac 2021-03-21 22:52:32 +00:00
parent 57ca04288b
commit 04397b3261
6 changed files with 91 additions and 35 deletions

21
src/commands/_settings.js Normal file
View File

@ -0,0 +1,21 @@
const Command = require('../modules/commands/command');
module.exports = class SettingsCommand extends Command {
constructor(client) {
const i18n = client.i18n.get(client.config.locale);
super(client, {
internal: true,
slash: false,
name: i18n('commands.settings.name'),
description: i18n('commands.settings.description'),
});
}
async execute({ guild, member, channel, args }, message) {
let settings = await guild.settings;
const i18n = this.client.i18n.get(settings.locale);
message.channel.send('Settings!');
}
};

View File

@ -4,6 +4,7 @@ module.exports = {
let settings = await message.guild?.settings;
// message collection for ticket archiving
if (settings?.log_messages) {
if (message.system) return;
@ -29,6 +30,9 @@ module.exports = {
});
}
}
// non-slash commands
if (message.content.match(/^tickets\/(\S+)/mi))
client.commands.handle(message, false);
}
};

View File

@ -17,6 +17,10 @@
"description": "The topic of the ticket"
}
}
},
"settings": {
"name": "settings",
"description": "Configure Discord Tickets"
}
},
"no_perm": "❌ You do not have the permissions required to use this command:\n%s",

View File

@ -1,10 +1,16 @@
/* eslint-disable no-unused-vars */
const { Client, GuildMember, Guild, Channel } = require('discord.js');
const {
Client,
GuildMember,
Guild,
Channel,
Message
} = require('discord.js');
const fs = require('fs');
const { join } = require('path');
const { path } = require('../../utils/fs');
const { createMessage, flags } = require('../../utils/discord');
const {
createMessage,
flags
} = require('../../utils/discord');
/**
* A command
@ -34,6 +40,7 @@ module.exports = class Command {
* @param {Object} data - Command data
* @param {string} data.name - The name of the command (3-32)
* @param {string} data.description - The description of the command (1-100)
* @param {boolean} [data.slash] - Register as a slash command? **Defaults to `true`**
* @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.global] - Create a global command?
@ -63,6 +70,12 @@ module.exports = class Command {
*/
this.description = data.description;
/**
* Register as a slash command?
* @type {boolean}
*/
this.slash = data.slash === false ? false : true;
/**
* Only allow staff to use this command?
* @type {boolean}
@ -110,7 +123,7 @@ module.exports = class Command {
return this.client.log.error(e);
}
if (this.global)
if (this.slash && this.global)
this.client.api.applications(this.client.user.id).commands.post({ data }); // post command to Discord
let internal = this.internal ? 'internal ' : '';
@ -146,9 +159,9 @@ module.exports = class Command {
* @param {Channel} data.channel- The channel object
* @param {Guild} data.guild- The guild object
* @param {GuildMember} data.member - The member object
* @param {Interaction} interaction - Interaction object
* @param {(Interaction|Message)} interaction_or_message - Interaction object
*/
async execute(data, interaction) { }
async execute(data, interaction_or_message) { }
/**
* Defer the response to respond later

View File

@ -1,5 +1,5 @@
// eslint-disable-next-line no-unused-vars
const { Collection, Client } = require('discord.js');
const { Collection, Client, Message } = require('discord.js');
// eslint-disable-next-line no-unused-vars
const Command = require('./command');
@ -139,48 +139,64 @@ module.exports = class CommandManager {
/**
* Execute a command
* @param {Interaction} interaction - Command interaction
* @param {(Interaction|Message)} interaction - Command interaction, or message
*/
async handle(interaction) {
const cmd_name = interaction.data.name;
async handle(interaction, slash) {
slash = slash === false ? false : true;
let cmd_name,
args = {},
data = {},
guild_id,
channel_id,
member_id;
if (!this.commands.has(cmd_name))
if (slash) {
cmd_name = interaction.data.name;
guild_id = interaction.guild_id;
channel_id = interaction.channel_id;
member_id = interaction.member.user.id;
if (interaction.data.options)
interaction.data.options.forEach(({ name, value }) => args[name] = value);
} else {
cmd_name = interaction.content.match(/^tickets\/(\S+)/mi);
if (cmd_name) cmd_name = cmd_name[1];
guild_id = interaction.guild.id;
channel_id = interaction.channel.id;
member_id = interaction.author.id;
}
if (cmd_name === null || !this.commands.has(cmd_name))
throw new Error(`Received "${cmd_name}" command invocation, but the command manager does not have a "${cmd_name}" command`);
let args = {};
if (interaction.data.options)
interaction.data.options.forEach(({ name, value }) => args[name] = value);
let data = { args };
data.guild = await this.client.guilds.fetch(interaction.guild_id);
data.channel = await this.client.channels.fetch(interaction.channel_id),
data.member = await data.guild.members.fetch(interaction.member.user.id);
data.args = args;
data.guild = await this.client.guilds.fetch(guild_id);
data.channel = await this.client.channels.fetch(channel_id),
data.member = await data.guild.members.fetch(member_id);
const cmd = this.commands.get(cmd_name);
let settings = await data.guild.settings;
if (!settings)
settings = await data.guild.createSettings();
if (!settings) settings = await data.guild.createSettings();
const i18n = this.client.i18n.get(settings.locale);
// if (cmd.staff_only) {
// return await cmd.sendResponse(interaction, msg, true);
// }
// if (cmd.staff_only) {}
const no_perm = cmd.permissions instanceof Array
&& !data.member.hasPermission(cmd.permissions);
if (no_perm) {
let perms = cmd.permissions.map(p => `\`${p}\``).join(', ');
let msg = i18n('no_perm', perms);
return await cmd.sendResponse(interaction, msg, true);
if (slash) return await cmd.sendResponse(interaction, msg, true);
else return await interaction.channel.send(msg);
}
try {
await cmd.acknowledge(interaction, true); // respond to discord
if (slash) await cmd.acknowledge(interaction, true); // respond to discord
this.client.log.commands(`Executing "${cmd_name}" command (invoked by ${data.member.user.tag})`);
/* let res = */await cmd.execute(data, interaction); // run the command
// if (typeof res === 'object' || typeof res === 'string')
// cmd.sendResponse(interaction, res, res.secret);
await cmd.execute(data, interaction); // run the command
} catch (e) {
this.client.log.warn(`(COMMANDS) An error occurred whilst executed the ${cmd_name} command`);
this.client.log.error(e);

View File

@ -36,9 +36,7 @@ module.exports = {
keep_for: 30
},
max_listeners: 10,
plugins: [
'dsctickets.settings-server',
],
plugins: [],
presence: {
presences: [
{