From 4944eeba1db4304470a145875f94133e58060bab Mon Sep 17 00:00:00 2001 From: Guz <43732358+Guz013@users.noreply.github.com> Date: Wed, 1 Sep 2021 13:59:02 -0300 Subject: [PATCH] Added the command type system. Created the command type `per-guild` these commands are registered when the bot enters a new guild, different of the global type, they can have permissions edited (the permissions system need to be developed yet). `guild` commands will be registered just in the development guild (probably the purpose of them need to be rethought). The reload command became adapted to be compatible with the command type system. When a command is reloaded, it's on all other guilds. --- commands/dev/reload.js | 102 ++++++++++++++++------- commands/utils/ping.js | 3 + events/client/ready.js | 32 ++++++- events/guild/guildCreate.js | 25 ++++++ events/interactions/interactionCreate.js | 8 +- functions/meta/console.js | 2 +- index.js | 39 +++++++-- nodemon.json | 3 +- 8 files changed, 170 insertions(+), 44 deletions(-) create mode 100644 events/guild/guildCreate.js diff --git a/commands/dev/reload.js b/commands/dev/reload.js index ce32214..362e9b5 100644 --- a/commands/dev/reload.js +++ b/commands/dev/reload.js @@ -20,68 +20,113 @@ module.exports = { permission: true, }, ], + properties: { + type: [ 'guild', 'per-guild' ], + }, async execute(interaction, client) { + await interaction.deferReply({ ephemeral: true }); + const { commands, commandsData, rest } = require('../../index'); const cmdName = interaction.options.getString('command').toLowerCase(); - const cmdIdx = commands.findIndex(c => c.data.name === cmdName); const command = commands.find(c => c.data.name === cmdName); - if(command?.properties.folder == 'dev') { - interaction.reply({ content: `You can't reload \`${cmdName}\` because it is a development command.`, ephemeral: true }); + switch (command?.properties?.folder) { + case 'dev': + interaction.editReply({ content: `You can't reload \`${cmdName}\` because it is a development command.`, ephemeral: true }); return; - } - else if(!command) { - interaction.reply({ content: `Command \`${cmdName}\` wasn't found.`, ephemeral: true }); + case undefined: + interaction.editReply({ content: `Command \`${cmdName}\` wasn't found.`, ephemeral: true }); return; } - const oldCommand = commands[cmdIdx]; - const oldCommandData = commandsData[cmdIdx]; + const cmdIdx = commands.findIndex(c => c.data.name === cmdName); + const dataIdx = commandsData.guild.findIndex(c => c.name === cmdName); - const clientCommand = (await client.guilds.cache.get(process.env.DEV_GUILD)?.commands.fetch()).find(c => c.name === cmdName); + const oldCommand = command; + const oldCommandData = await commandsData.guild[dataIdx]; - const commandFolder = fs.readdirSync('./commands').find(folder => fs.readdirSync(`./commands/${folder}`).includes(`${cmdName}.js`)); + const folder = fs.readdirSync('./commands').find(f => fs.readdirSync(`./commands/${f}`).includes(`${cmdName}.js`)); - delete require.cache[require.resolve(`../../commands/${commandFolder}/${cmdName}.js`)]; + delete require.cache[require.resolve(`../../commands/${folder}/${cmdName}.js`)]; try { - const newCommand = require(`../../commands/${commandFolder}/${cmdName}.js`); + console.log( + 'Old Command --------------------------------\n', oldCommand, + '\nData:\n', oldCommandData, + ); + if(oldCommand.permissions) { + console.log('Permissions'); + console.table(oldCommand.permissions); + } + + const newCommand = require(`../../commands/${folder}/${cmdName}.js`); + + const clientCommand = (await client.guilds.cache.get(process.env.DEV_GUILD)?.commands.fetch()).find(c => c.name === cmdName); + + if(!newCommand.properties) newCommand.properties = { folder: folder }; + else newCommand.properties.folder = folder; commands[cmdIdx] = { data: newCommand.data, + properties: newCommand.properties, permissions: newCommand.permissions, execute: newCommand.execute, id: clientCommand.id, name: clientCommand.name, }; - commandsData[cmdIdx] = newCommand.data.toJSON(); + if(newCommand.properties.type.includes('guild')) { - await rest.put( - Routes.applicationGuildCommands(process.env.CLIENT, process.env.DEV_GUILD), { - body: commandsData, - }, - ); + commandsData.guild[dataIdx] = newCommand.data.toJSON(); - await clientCommand.permissions.set({ id: clientCommand.id, permissions: newCommand.permissions }); + await rest.put( + Routes.applicationGuildCommands(process.env.CLIENT, process.env.DEV_GUILD), { + body: commandsData.guild, + }, + ); - interaction.reply({ content: `Command \`${cmdName}\` reloaded successfully.`, ephemeral: true }); + if(newCommand.permissions) { + await clientCommand.permissions.set({ id: clientCommand.id, permissions: newCommand.permissions }); + } - console.log( - 'Old Command --------------------------------\n', oldCommand, - '\nData:\n', oldCommandData, - '\nPermissions:'); - console.table(oldCommand.permissions); + } + + const guilds = client.guilds.cache.map(g => g.id); + + if(newCommand.properties.type.includes('per-guild')) { + + commandsData.perGuild[dataIdx] = newCommand.data.toJSON(); + + for (const guild of guilds) { + + if(guild == process.env.DEV_GUILD) continue; + + await rest.put( + Routes.applicationGuildCommands(process.env.CLIENT, guild), { + body: commandsData.perGuild, + }, + ); + + } + + } console.log( '\nNew Command --------------------------------\n', commands[cmdIdx], - '\nData:\n', commandsData[cmdIdx], - '\nPermissions:'); - console.table(newCommand.permissions); + '\nData:\n', commandsData.guild[dataIdx], + ); + if(newCommand.permissions) { + console.log('Permissions'); + console.table(newCommand.permissions); + } + + console.table(commands); + + interaction.editReply({ content: `Command \`${cmdName}\` reloaded on ${guilds.length} guilds.`, ephemeral: true }); } catch (error) { @@ -91,6 +136,5 @@ module.exports = { ephemeral: true, }); } - }, }; \ No newline at end of file diff --git a/commands/utils/ping.js b/commands/utils/ping.js index dccec25..16df81a 100644 --- a/commands/utils/ping.js +++ b/commands/utils/ping.js @@ -11,6 +11,9 @@ module.exports = { permission: true, }, ], + properties: { + type: [ 'guild', 'per-guild' ], + }, async execute(interaction) { console.log('ping'); diff --git a/events/client/ready.js b/events/client/ready.js index 2cf056d..9e51102 100644 --- a/events/client/ready.js +++ b/events/client/ready.js @@ -1,4 +1,6 @@ -const { commands } = require('../../index'); +const { Routes } = require('discord-api-types/v9'); + +const { commandsData, commands, rest } = require('../../index'); const console = require('../../functions/meta/console'); module.exports = { @@ -7,18 +9,44 @@ module.exports = { await client.application?.fetch(); + const guilds = await client.guilds.cache.map(guild => guild.id); + + console.debug(guilds); + for (const command of commands) { + if(!command.properties.type.includes('guild')) continue; + const clientCommand = (await client.guilds.cache.get(process.env.DEV_GUILD)?.commands.fetch()).find(c => c.name === command.data.name); - await clientCommand.permissions.set({ id: clientCommand.id, permissions: command.permissions }); + if(command.permissions) { + await clientCommand.permissions.set({ id: clientCommand.id, permissions: command.permissions }); + } command.id = clientCommand.id; command.name = clientCommand.name; } + for (const guild of guilds) { + + if(guild.id == process.env.DEV_GUILD) continue; + + try { + await rest.put( + Routes.applicationGuildCommands(process.env.CLIENT, guild), { + body: commandsData.perGuild, + }, + ); + } + catch (error) { + console.error(error); + } + + } + console.info('Guild Commands:'); console.table(commands); + console.info('Bot configurations:'); console.table({ 'Owner Id': process.env.OWNER, diff --git a/events/guild/guildCreate.js b/events/guild/guildCreate.js new file mode 100644 index 0000000..4084013 --- /dev/null +++ b/events/guild/guildCreate.js @@ -0,0 +1,25 @@ +const { Routes } = require('discord-api-types/v9'); + +const fs = require('fs'); + +const console = require('../../functions/meta/console'); + +module.exports = { + name: 'guildCreate', + async execute(guild, client) { + + const { commands, commandsData, rest } = require('../../index'); + + try { + await rest.put( + Routes.applicationGuildCommands(process.env.CLIENT, guild.id), { + body: commandsData.perGuild, + }, + ); + } + catch (error) { + console.error(error); + } + + }, +}; \ No newline at end of file diff --git a/events/interactions/interactionCreate.js b/events/interactions/interactionCreate.js index 9ce0c32..98d1e0b 100644 --- a/events/interactions/interactionCreate.js +++ b/events/interactions/interactionCreate.js @@ -5,12 +5,12 @@ module.exports = { name: 'interactionCreate', async execute(interaction, client) { - console.system('NEW INTERACTION ################################'); + console.system(`New interaction fired: ${interaction.type} ${interaction.id}`); console.time('Interaction'); if(interaction.isCommand()) { - console.system(`Command ${interaction.commandName} executing ----------------\n`); + console.system(`Command ${interaction.commandName} executing\n`); const command = commands.find(c => c.data.name === interaction.commandName); @@ -31,12 +31,12 @@ module.exports = { }); } - console.system(`Command ${interaction.commandName} executed ----------------`); + console.system(`Command ${interaction.commandName} executed`); } console.timeEnd('Interaction'); - console.system('################################################'); + console.system(`Interaction finished: ${interaction.type} ${interaction.id}`); }, }; \ No newline at end of file diff --git a/functions/meta/console.js b/functions/meta/console.js index 11bf959..1aae4ad 100644 --- a/functions/meta/console.js +++ b/functions/meta/console.js @@ -4,7 +4,7 @@ module.exports = { CONSOLE: require('console'), - log: (msg) => console.log(msg), + log: (...args) => console.log(...args), time: (label) => console.time(chalk.hex('#DF82FF').bold(`\n[ TIME LOG ] ${label}`)), timeEnd: (label) => console.timeEnd(chalk.hex('#DF82FF').bold(`\n[ TIME LOG ] ${label}`)), diff --git a/index.js b/index.js index 845333f..d45ba99 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,11 @@ client.commands = new Collection(); const commandFolders = fs.readdirSync('./commands'); const commands = []; -const commandsData = []; +const commandsData = { + guild: [], + perGuild: [], + global: [], +}; for (const folder of commandFolders) { @@ -24,15 +28,28 @@ for (const folder of commandFolders) { const command = require(`./commands/${folder}/${file}`); - if(command.properties) command.proprieties.folder = folder; + if(command.properties) command.properties.folder = folder; else command.properties = { folder: folder }; - commands.push(command); - commandsData.push(command.data.toJSON()); + if(!command.properties.guild) command.properties.guild = process.env.DEV_GUILD; + commands.push(command); + + if(command.properties.type.includes('guild')) { + commandsData.guild.push(command.data.toJSON()); + } + + if(command.properties.type.includes('per-guild')) { + commandsData.perGuild.push(command.data.toJSON()); + } + + if(command.properties.type.includes('per-guild')) { + commandsData.global.push(command.data.toJSON()); + } } } + const rest = new REST({ version: '9', }).setToken(process.env.TOKEN); @@ -45,7 +62,7 @@ module.exports = { commands, commandsData, rest }; await rest.put( Routes.applicationGuildCommands(process.env.CLIENT, process.env.DEV_GUILD), { - body: commandsData, + body: commandsData.guild, }, ); @@ -67,10 +84,18 @@ for (const folder of eventFolders) { const event = require(`./events/${folder}/${file}`); if (event.once) { - client.once(event.name, (...args) => event.execute(...args, client)); + client.once(event.name, (...args) => { + console.system(`New event fired: ${event.name}`); + event.execute(...args, client); + console.system(`Event finished: ${event.name}`); + }); } else { - client.on(event.name, (...args) => event.execute(...args, client)); + client.on(event.name, (...args) => { + console.system(`New event fired: ${event.name}`); + event.execute(...args, client); + console.system(`Event finished: ${event.name}`); + }); } } diff --git a/nodemon.json b/nodemon.json index 341bd6c..1827493 100644 --- a/nodemon.json +++ b/nodemon.json @@ -5,7 +5,8 @@ "node_modules/**/node_modules" ], "watch": [ - "commands/dev/*" + "commands/dev/*", + "./index.js" ], "env": { "NODE_ENV": "development"