Skip to content

Commit

Permalink
feat(deploy): add deploy command as server-side function
Browse files Browse the repository at this point in the history
remove the !deploy function (actually it is not available
  • Loading branch information
Lutra-Fs committed Nov 24, 2022
1 parent a72b18a commit e93f699
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 22 deletions.
4 changes: 3 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -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",
}
55 changes: 55 additions & 0 deletions helper/deploy-command.js
Original file line number Diff line number Diff line change
@@ -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();
});
24 changes: 3 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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!',
Expand Down

0 comments on commit e93f699

Please sign in to comment.