all repos — simple-discord-music-bot @ 3b18ef76a09748a67db32742c1911d937086dba4

A Discord bot making use of discord.js and play-yt.

index.js (view raw)

 1const fs = require('node:fs');
 2const path = require('node:path');
 3const { Client, Collection, GatewayIntentBits, ActivityType } = require('discord.js');
 4const { token } = require(path.join(process.cwd(), 'config.json'));
 5
 6const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });
 7client.commands = new Collection();
 8const commandsPath = path.join(__dirname, 'commands');
 9const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
10
11for (const file of commandFiles) {
12    const filePath = path.join(commandsPath, file);
13    const command = require(filePath);
14    if ('data' in command && 'execute' in command) {
15        client.commands.set(command.data.name, command);
16    } else {
17        console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
18    }
19}
20
21client.once('ready', () => {
22    client.user.setActivity('FOSS', { type: ActivityType.Competing });
23    console.log('Bot online!');
24});
25
26client.on('interactionCreate', async interaction => {
27    if (!interaction.isChatInputCommand()) return;
28    const command = client.commands.get(interaction.commandName);
29    if (!command) return;
30    try {
31        await command.execute(interaction);
32    }
33    catch (error) {
34        console.error(error);
35        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
36    }
37});
38
39if (!token)
40    throw 'Check your config.json!';
41
42client.login(token);