From e93f69952461a212b6d670d49d81aee8e1bbc30c Mon Sep 17 00:00:00 2001 From: Bill ZHANG <36790218+Lutra-Fs@users.noreply.github.com> Date: Thu, 24 Nov 2022 12:07:25 +1100 Subject: [PATCH] feat(deploy): add deploy command as server-side function remove the !deploy function (actually it is not available --- config.json | 4 ++- helper/deploy-command.js | 55 ++++++++++++++++++++++++++++++++++++++++ index.js | 24 +++--------------- 3 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 helper/deploy-command.js diff --git a/config.json b/config.json index a68e9af..f785b04 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,7 @@ { "token": "YOUR_TOKEN", "activityType": "PLAYING", - "activity": "your music selections" + "activity": "your music selections", + "clientId": "YOUR_CLIENT_ID", + "guildId": "YOUR_GUILD_ID,TARGET_SERVER_GUILD_ID", } \ No newline at end of file diff --git a/helper/deploy-command.js b/helper/deploy-command.js new file mode 100644 index 0000000..dc3d6f9 --- /dev/null +++ b/helper/deploy-command.js @@ -0,0 +1,55 @@ +const { REST, Routes } = require('discord.js'); +const { clientId, guildId, token } = require('../config.json'); +const fs = require('node:fs'); + +const commands = []; +// Grab all the command files from the commands directory you created earlier +const commandFiles = fs.readdirSync('../commands') + .filter(file => file.endsWith('.js')); + +// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment +for (const file of commandFiles) { + const command = require(`../commands/${file}`); + commands.push(command.data.toJSON()); +} +console.log(commands); +// log each option of each command +for (const command of commands) { + console.log(command.options); +} +// ask for the target before deploying +console.log('Please enter the target (guild or global):'); +const rl = require('readline').createInterface({ + input: process.stdin, + output: process.stdout, +}); +rl.on('line', (input) => { + // Create a new REST instance + const rest = new REST({ version: '10' }).setToken(token); + + (async () => { + try { + console.log('Started refreshing application (/) commands.'); + + if (input === 'global') { + await rest.put( + Routes.applicationCommands(clientId), + { body: commands }, + ); + } + // guild is the default, set the guild id in config.json + else { + await rest.put( + Routes.applicationGuildCommands(clientId, guildId), + { body: commands }, + ); + } + + console.log('Successfully reloaded application (/) commands.'); + } + catch (error) { + console.error(error); + } + })(); + rl.close(); +}); \ No newline at end of file diff --git a/index.js b/index.js index 65f684a..85d15ff 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ const Discord = require('discord.js'); const Client = require('./client/Client'); const config = require('./config.json'); const { Player } = require('discord-player'); -//from helpers extractor.js import ncmExtractor +// from helpers extractor.js import ncmExtractor const { NcmExtractor } = require('./helper/extractor.js'); const client = new Client(); @@ -75,31 +75,13 @@ client.once('disconnect', () => { console.log('Disconnect!'); }); -client.on('messageCreate', async message => { - if (message.author.bot || !message.guild) return; - if (!client.application?.owner) await client.application?.fetch(); - - if (message.content === '!deploy' && message.author.id === - client.application?.owner?.id) { - await message.guild.commands - .set(client.commands) - .then(() => { - message.reply('Deployed!'); - }) - .catch(err => { - message.reply( - 'Could not deploy commands! Make sure the bot has the application.commands permission!'); - console.error(err); - }); - } -}); - client.on('interactionCreate', async interaction => { const command = client.commands.get(interaction.commandName.toLowerCase()); try { command.execute(interaction, player); - } catch (error) { + } + catch (error) { console.error(error); interaction.followUp({ content: 'There was an error trying to execute that command!',