all repos — simple-discord-music-bot @ ee48e4f6561bef07c4fda9326fd805761a0030fd

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

src/index.ts (view raw)

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