all repos — simple-discord-music-bot @ 259d1de4ecac5f8665db926bf702f81e4d90e829

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'));
10let player;
11
12for (const file of commandFiles) {
13    const filePath = path.join(commandsPath, file);
14    const command = require(filePath);
15    client.commands.set(command.data.name, command);
16}
17
18client.once('ready', () => {
19    client.user.setActivity('FOSS', { type: ActivityType.Competing });
20    console.log('Bot online!');
21});
22
23client.on('interactionCreate', async interaction => {
24    if (!interaction.isChatInputCommand()) return;
25    const command = client.commands.get(interaction.commandName);
26    if (!command) return;
27    try {
28        await command.execute(interaction);
29    }
30    catch (error) {
31        console.error(error);
32        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
33    }
34});
35
36if (!token)
37    throw 'Check your config.json!';
38
39client.login(token);