all repos — simple-discord-music-bot @ 39feb6519fe341fa5db9d2f2e4278fcdd78439b2

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