code cleaning
Marco Andronaco andronacomarco@gmail.com
Wed, 14 Sep 2022 22:55:35 +0200
5 files changed,
31 insertions(+),
32 deletions(-)
M
commands/outro.js
→
commands/outro.js
@@ -1,7 +1,8 @@
+const path = require('node:path'); const { SlashCommandBuilder } = require('discord.js'); const { createAudioResource, createAudioPlayer, NoSubscriberBehavior, joinVoiceChannel, AudioPlayerStatus } = require('@discordjs/voice'); -const OUTRO_PATH = 'resources/outro.mp3'; +const OUTRO_PATH = path.join(process.cwd(), 'resources', 'outro.mp3'); function get_player(resource) {
M
delete-commands.js
→
tools/delete-commands.js
@@ -1,10 +1,9 @@
-const { REST } = require('@discordjs/rest'); +const path = require('node:path'); const { Routes } = require('discord.js'); -const { clientId, guildId, token } = require('./config.json'); +const { REST } = require('@discordjs/rest'); +const { clientId, guildId, token } = require(path.join(process.cwd(), 'config.json')); const rest = new REST({ version: '10' }).setToken(token); - -// ... // for guild-based commands rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: [] })@@ -14,4 +13,4 @@
// for global commands rest.put(Routes.applicationCommands(clientId), { body: [] }) .then(() => console.log('Successfully deleted all application commands.')) - .catch(console.error);+ .catch(console.error);
D
deploy-commands.js
@@ -1,19 +0,0 @@
-const { SlashCommandBuilder, Routes } = require('discord.js'); -const { REST } = require('@discordjs/rest'); -const { clientId, token } = require('./config.json'); - -const commands = [ - new SlashCommandBuilder().setName('outro').setDescription('Leave voice channel with an outro.'), -] - .map(command => command.toJSON()); - -const rest = new REST({ version: '10' }).setToken(token); - -/* -rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands }) - .then((data) => console.log(`Successfully registered ${data.length} application commands.`)) - .catch(console.error); - */ -rest.put(Routes.applicationCommands(clientId), { body: commands }) - .then((data) => console.log(`Successfully registered ${data.length} application commands.`)) - .catch(console.error);
M
index.js
→
index.js
@@ -1,7 +1,7 @@
const fs = require('node:fs'); const path = require('node:path'); const { Client, Collection, GatewayIntentBits, ActivityType, PresenceUpdateStatus, Presence } = require('discord.js'); -const { token } = require('./config.json'); +const { token } = require(path.join(process.cwd(), 'config.json')); const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });@@ -14,10 +14,6 @@ const filePath = path.join(commandsPath, file);
const command = require(filePath); client.commands.set(command.data.name, command); } - -function getRandomInt(max) { // not inclusive - return Math.floor(Math.random() * (max)); - } const activities = [ { activity: ActivityType.Watching, subject: 'i cantieri'}, // sta guardando i cantieri@@ -30,7 +26,8 @@ { activity: ActivityType.Competing, subject: 'cacata ranked al cesso pubblico'},
] client.once('ready', () => { - choice = activities[getRandomInt(activities.length)] + const random = Math.floor(Math.random() * (activities.length)); + const choice = activities[random] client.user.setActivity(choice.subject, { type: choice.activity }) console.log('Bot online!');@@ -52,4 +49,4 @@ await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
} }); -client.login(token);+client.login(token);
A
tools/deploy-commands.js
@@ -0,0 +1,21 @@
+const fs = require('node:fs'); +const path = require('node:path'); +const { Routes } = require('discord.js'); +const { REST } = require('@discordjs/rest'); +const { clientId, token } = require(path.join(process.cwd(), 'config.json')); + +const commands = new Array(); +const commandsPath = path.join(process.cwd(), 'commands'); +const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); + +for (const file of commandFiles) { + const filePath = path.join(commandsPath, file); + const command = require(filePath); + commands.push(command.data.toJSON()); +} + +const rest = new REST({ version: '10' }).setToken(token); + +rest.put(Routes.applicationCommands(clientId), { body: commands }) + .then((data) => console.log(`Successfully registered ${data.length} application commands.`)) + .catch(console.error);