diff --git a/index.js b/index.js index 8e7ed6e..1909e3e 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +const fs = require('node:fs'); const Discord = require('discord.js'); const { Player } = require('discord-player'); const { token } = require('./config.json'); @@ -6,21 +7,82 @@ const { token } = require('./config.json'); const client = new Discord.Client({ intents: [ Discord.GatewayIntentBits.Guilds, - Discord.GatewayIntentBits.GuildMessages, - Discord.GatewayIntentBits.GuildVoiceStates, - Discord.GatewayIntentBits.GuildMessageReactions + Discord.GatewayIntentBits.GuildVoiceStates ] }); -// Log to console when successfully logged in -client.on('ready', () => { - console.log(`Logged in as ${client.user.tag}!`); +// Setup commands collection and load commands +client.commands = new Discord.Collection(); +const commandFiles = fs + .readdirSync('./commands') + .filter((file) => file.endsWith('.js')); +for (const file of commandFiles) { + const command = require(`./commands/${file}`); + client.commands.set(command.data.name, command); +} + +// Create a new Player, and attach it to the bot client. +client.player = new Player(client, { + ytdlOptions: { + quality: 'highestaudio', + highWaterMark: 1 << 25 + }, + FFMPEG_OPTIONS: { + before_options: + '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', + options: '-vn' + } }); -// this is the entrypoint for discord-player based application -const player = new Player(client); +client.once('ready', async () => { + console.log(`Logged in as ${client.user.tag}!`); -// This method will load all the extractors from the @discord-player/extractor package -await player.extractors.loadDefault(); + // This method will load all the extractors from the @discord-player/extractor package + player.extractors.loadDefault(); +}); + +client.once('reconnecting', () => { + console.log('Reconnecting!'); +}); + +client.once('disconnect', () => { + console.log('Disconnected'); +}); + +client.on('warn', (info) => { + console.log(info); +}); + +client.on('error', console.error); + +client.on('interactionCreate', async (interaction) => { + if (!interaction.isCommand()) { + return; + } + + const command = client.commands.get(interaction.commandName); + if (!command) return; + + console.log(`Command '/${interaction.commandName}' was used.`); + + try { + await interaction.deferReply(); + await command.run({ client, interaction }); + } catch (error) { + console.error(error); + console.log( + `Command '/${interaction.commandName}' failed unexpectedly.` + ); + + await interaction.editReply({ + content: 'There was an error while executing this command!', + ephemeral: true + }); + } finally { + console.log( + `Command '/${interaction.commandName}' was executed successfully.` + ); + } +}); client.login(token);