diff --git a/package.json b/package.json index 8714855..eeacf7a 100644 --- a/package.json +++ b/package.json @@ -21,8 +21,8 @@ "test": "echo \"Error: no test specified\" && exit 1", "start": "node ./src/index.js", "start-pretty": "node ./src/index.js | pino-pretty", - "deploy": "node ./src/utils/deploy-commands.js && node ./src/utils/deploy-system-commands.js", - "deploy-pretty": "node ./src/utils/deploy-commands.js | pino-pretty && node ./src/utils/deploy-system-commands.js | pino-pretty", + "deploy": "node ./src/utils/deploy-slash-commands.js", + "deploy-pretty": "node ./src/utils/deploy-slash-commands.js | pino-pretty", "eslint": "eslint ./" }, "dependencies": { diff --git a/src/commands/help.js b/src/commands/info/help.js similarity index 100% rename from src/commands/help.js rename to src/commands/info/help.js diff --git a/src/commands/filters.js b/src/commands/player/filters.js similarity index 100% rename from src/commands/filters.js rename to src/commands/player/filters.js diff --git a/src/commands/leave.js b/src/commands/player/leave.js similarity index 100% rename from src/commands/leave.js rename to src/commands/player/leave.js diff --git a/src/commands/loop.js b/src/commands/player/loop.js similarity index 100% rename from src/commands/loop.js rename to src/commands/player/loop.js diff --git a/src/commands/nowplaying.js b/src/commands/player/nowplaying.js similarity index 100% rename from src/commands/nowplaying.js rename to src/commands/player/nowplaying.js diff --git a/src/commands/pause.js b/src/commands/player/pause.js similarity index 100% rename from src/commands/pause.js rename to src/commands/player/pause.js diff --git a/src/commands/play.js b/src/commands/player/play.js similarity index 100% rename from src/commands/play.js rename to src/commands/player/play.js diff --git a/src/commands/queue.js b/src/commands/player/queue.js similarity index 100% rename from src/commands/queue.js rename to src/commands/player/queue.js diff --git a/src/commands/remove.js b/src/commands/player/remove.js similarity index 100% rename from src/commands/remove.js rename to src/commands/player/remove.js diff --git a/src/commands/seek.js b/src/commands/player/seek.js similarity index 100% rename from src/commands/seek.js rename to src/commands/player/seek.js diff --git a/src/commands/skip.js b/src/commands/player/skip.js similarity index 100% rename from src/commands/skip.js rename to src/commands/player/skip.js diff --git a/src/commands/volume.js b/src/commands/player/volume.js similarity index 100% rename from src/commands/volume.js rename to src/commands/player/volume.js diff --git a/src/system-commands/guilds.js b/src/commands/system/guilds.js similarity index 98% rename from src/system-commands/guilds.js rename to src/commands/system/guilds.js index 556d5d1..7f15277 100644 --- a/src/system-commands/guilds.js +++ b/src/commands/system/guilds.js @@ -8,6 +8,7 @@ const { } = require(path.resolve('./config.json')); module.exports = { + isSystemCommand: true, data: new SlashCommandBuilder() .setName('guilds') .setDescription('Show list of guilds where bot is added.') diff --git a/src/system-commands/status.js b/src/commands/system/status.js similarity index 99% rename from src/system-commands/status.js rename to src/commands/system/status.js index 1e4c663..cef758b 100644 --- a/src/system-commands/status.js +++ b/src/commands/system/status.js @@ -11,6 +11,7 @@ const osu = require('node-os-utils'); const packageJson = require(path.resolve('./package.json')); module.exports = { + isSystemCommand: true, data: new SlashCommandBuilder() .setName('status') .setDescription('Show bot status.') diff --git a/src/index.js b/src/index.js index 0e16650..abe0b67 100644 --- a/src/index.js +++ b/src/index.js @@ -18,27 +18,6 @@ const client = new Discord.Client({ ] }); -// Setup commands collection and load commands -// todo: extract this logic to a separate file -client.commands = new Discord.Collection(); -const commandFiles = fs - .readdirSync(path.resolve('./src/commands')) - .filter((file) => file.endsWith('.js')); -for (const file of commandFiles) { - const command = require(path.resolve(`./src/commands/${file}`)); - client.commands.set(command.data.name, command); -} - -const systemCommandFiles = fs - .readdirSync(path.resolve('./src/system-commands')) - .filter((file) => file.endsWith('.js')); -for (const file of systemCommandFiles) { - const systemCommand = require(path.resolve( - `./src/system-commands/${file}` - )); - client.commands.set(systemCommand.data.name, systemCommand); -} - const player = new Player(client, { useLegacyFFmpeg: false, ytdlOptions: { @@ -67,6 +46,39 @@ onBeforeCreateStream(async (track) => { return null; }); +// Setup commands collection and load commands +// todo: extract this logic to a separate file +client.commands = new Discord.Collection(); +const commandFolders = fs.readdirSync(path.resolve('./src/commands')); +for (const folder of commandFolders) { + const commandFiles = fs + .readdirSync(path.resolve(`./src/commands/${folder}`)) + .filter((file) => file.endsWith('.js')); + + for (const file of commandFiles) { + const command = require(path.resolve( + `./src/commands/${folder}/${file}` + )); + client.commands.set(command.data.name, command); + } +} + +const eventFolders = fs.readdirSync(path.resolve('./src/events')); +for (const folder of eventFolders) { + const eventFiles = fs + .readdirSync(path.resolve(`./src/events/${folder}`)) + .filter((file) => file.endsWith('.js')); + + for (const file of eventFiles) { + const event = require(path.resolve(`./src/events/${folder}/${file}`)); + if (event.once) { + client.once(event.name, (...args) => event.execute(...args)); + } else { + client.on(event.name, (...args) => event.execute(...args)); + } + } +} + if (process.env.NODE_ENV === 'development') { player.on('debug', (message) => { logger.debug(message); diff --git a/src/utils/deploy-commands.js b/src/utils/deploy-commands.js deleted file mode 100644 index ad73daf..0000000 --- a/src/utils/deploy-commands.js +++ /dev/null @@ -1,44 +0,0 @@ -const path = require('node:path'); -const fs = require('node:fs'); -const logger = require(path.resolve('./src/services/logger.js')); -const { REST, Routes } = require('discord.js'); -require('dotenv').config(); - -const commands = []; -const commandFiles = fs - .readdirSync(path.resolve('./src/commands')) - .filter((file) => file.endsWith('.js')); - -for (const file of commandFiles) { - const command = require(path.resolve(`./src/commands/${file}`)); - commands.push(command.data.toJSON()); -} - -const rest = new REST({ version: '10' }).setToken( - process.env.DISCORD_BOT_TOKEN -); - -(async () => { - try { - logger.info('DEPLOYING BOT SLASH COMMANDS'); - logger.info( - commands.map((commands) => { - return commands.name; - }), - 'Bot commands found:' - ); - - logger.info('Started refreshing application (/) bot commands.'); - - await rest.put( - Routes.applicationCommands(process.env.DISCORD_CLIENT_ID), - { - body: commands - } - ); - - logger.info('Successfully refreshed application (/) bot commands.'); - } catch (error) { - logger.error(error, 'Failed to refresh application (/) bot commands.'); - } -})(); diff --git a/src/utils/deploy-slash-commands.js b/src/utils/deploy-slash-commands.js new file mode 100644 index 0000000..f29998b --- /dev/null +++ b/src/utils/deploy-slash-commands.js @@ -0,0 +1,85 @@ +const path = require('node:path'); +const fs = require('node:fs'); +const logger = require(path.resolve('./src/services/logger.js')); +const { REST, Routes } = require('discord.js'); +const { systemServerGuildIds } = require(path.resolve('./config.json')); +require('dotenv').config(); + +const slashCommands = []; +const systemCommands = []; +const commandFolders = fs.readdirSync(path.resolve('./src/commands')); +for (const folder of commandFolders) { + const commandFiles = fs + .readdirSync(path.resolve(`./src/commands/${folder}`)) + .filter((file) => file.endsWith('.js')); + + for (const file of commandFiles) { + const command = require(path.resolve( + `./src/commands/${folder}/${file}` + )); + command.isSystemCommand + ? systemCommands.push(command.data.toJSON()) + : slashCommands.push(command.data.toJSON()); + } +} + +const rest = new REST({ version: '10' }).setToken( + process.env.DISCORD_BOT_TOKEN +); + +(async () => { + try { + logger.info('DEPLOYING BOT SLASH COMMANDS'); + logger.info( + slashCommands.map((command) => { + return command.name; + }), + 'Bot commands found:' + ); + + logger.info('Started refreshing application (/) bot commands.'); + await rest.put( + Routes.applicationCommands(process.env.DISCORD_CLIENT_ID), + { + body: slashCommands + } + ); + + logger.info('Successfully refreshed application (/) bot commands.'); + } catch (error) { + logger.error(error, 'Failed to refresh application (/) bot commands.'); + } + + try { + logger.info('DEPLOYING SYSTEM SLASH COMMANDS'); + logger.info( + systemCommands.map((systemCommand) => { + return systemCommand.name; + }), + 'System commands found:' + ); + + logger.info('Started refreshing application (/) system commands.'); + for (const systemServerGuildId of systemServerGuildIds) { + logger.info( + `Refreshing system commands for guild id ${systemServerGuildId}.` + ); + await rest.put( + Routes.applicationGuildCommands( + process.env.DISCORD_CLIENT_ID, + systemServerGuildId + ), + { + body: systemCommands + } + ); + } + + logger.info('Successfully refreshed application (/) system commands.'); + } catch (error) { + logger.error( + error, + 'Failed to refresh application (/) system commands.' + ); + } +})(); diff --git a/src/utils/deploy-system-commands.js b/src/utils/deploy-system-commands.js deleted file mode 100644 index 97036fa..0000000 --- a/src/utils/deploy-system-commands.js +++ /dev/null @@ -1,53 +0,0 @@ -const path = require('node:path'); -const fs = require('node:fs'); -const logger = require(path.resolve('./src/services/logger.js')); -const { REST, Routes } = require('discord.js'); -const { systemServerGuildIds } = require(path.resolve('./config.json')); -require('dotenv').config(); - -const systemCommands = []; -const systemCommandFiles = fs - .readdirSync(path.resolve('./src/system-commands')) - .filter((file) => file.endsWith('.js')); - -for (const file of systemCommandFiles) { - const systemCommand = require(path.resolve(`./src/system-commands/${file}`)); - systemCommands.push(systemCommand.data.toJSON()); -} - -const rest = new REST({ version: '10' }).setToken( - process.env.DISCORD_BOT_TOKEN -); - -(async () => { - try { - logger.info('DEPLOYING SYSTEM SLASH COMMANDS'); - logger.info( - systemCommands.map((systemCommand) => { - return systemCommand.name; - }), - 'System commands found:' - ); - - logger.info('Started refreshing application (/) system commands.'); - - for (const systemServerGuildId of systemServerGuildIds) { - await rest.put( - Routes.applicationGuildCommands( - process.env.DISCORD_CLIENT_ID, - systemServerGuildId - ), - { - body: systemCommands - } - ); - } - - logger.info('Successfully refreshed application (/) system commands.'); - } catch (error) { - logger.error( - error, - 'Failed to refresh application (/) system commands.' - ); - } -})();