diff --git a/src/commands/_settings.js b/src/commands/_settings.js new file mode 100644 index 0000000..63a7640 --- /dev/null +++ b/src/commands/_settings.js @@ -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!'); + } +}; \ No newline at end of file diff --git a/src/listeners/message.js b/src/listeners/message.js index ec6c2d6..4020ed8 100644 --- a/src/listeners/message.js +++ b/src/listeners/message.js @@ -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); } }; \ No newline at end of file diff --git a/src/locales/en-GB.json b/src/locales/en-GB.json index 00db934..8f53bdb 100644 --- a/src/locales/en-GB.json +++ b/src/locales/en-GB.json @@ -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", diff --git a/src/modules/commands/command.js b/src/modules/commands/command.js index aa5253d..274e52e 100644 --- a/src/modules/commands/command.js +++ b/src/modules/commands/command.js @@ -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 diff --git a/src/modules/commands/manager.js b/src/modules/commands/manager.js index 871a4f6..2370c9e 100644 --- a/src/modules/commands/manager.js +++ b/src/modules/commands/manager.js @@ -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); diff --git a/user/example.config.js b/user/example.config.js index 4da4c2a..4d4986b 100644 --- a/user/example.config.js +++ b/user/example.config.js @@ -36,9 +36,7 @@ module.exports = { keep_for: 30 }, max_listeners: 10, - plugins: [ - 'dsctickets.settings-server', - ], + plugins: [], presence: { presences: [ {