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

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] });
 7
 8client.commands = new Collection();
 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	client.commands.set(command.data.name, command);
16}
17
18const activities = [
19	{ activity: ActivityType.Watching, subject: 'i cantieri' }, // sta guardando i cantieri
20	{ activity: ActivityType.Playing, subject: 'briscola' }, // sta giocando a briscola
21	{ activity: ActivityType.Competing, subject: 'prato fiorito' }, // in competizione su prato fiorito
22
23	{ activity: ActivityType.Watching, subject: 'i bambini' },
24	{ activity: ActivityType.Playing, subject: 'quel gioco là non mi viene il titolo' },
25	{ activity: ActivityType.Playing, subject: 'mosca cieca in autostrada' },
26	{ activity: ActivityType.Competing, subject: 'cacata ranked al cesso pubblico' },
27];
28
29client.once('ready', () => {
30	const random = Math.floor(Math.random() * (activities.length));
31	const choice = activities[random];
32
33	client.user.setActivity(choice.subject, { type: choice.activity });
34	console.log('Bot online!');
35});
36
37client.on('interactionCreate', async interaction => {
38	if (!interaction.isChatInputCommand()) return;
39
40	const command = client.commands.get(interaction.commandName);
41
42	if (!command) return;
43
44	try {
45		await command.execute(interaction);
46	}
47	catch (error) {
48		console.error(error);
49		await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
50	}
51});
52
53if (!token)
54	throw 'Check your config.json!';
55
56client.login(token);