-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (73 loc) · 2.64 KB
/
index.js
File metadata and controls
87 lines (73 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Require the necessary discord.js classes
const fs = require('fs');
const mysql = require('mysql2');
const { Client, Collection, Intents } = require('discord.js');
const { token, host, user, password, database } = require('./config.json');
const { Player }= require('discord-player');
// Create a new client instance
const client = new Client({
intents: [Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_MESSAGES,Intents.FLAGS.GUILDS]
});
// Create a new Player (you don't need any API Key)
const player = new Player(client);
player.on('error', (queue, error) => {
console.log(`[${queue.guild.name}] Error emitted from the queue: ${error.message}`);
});
player.on('connectionError', (queue, error) => {
console.log(`[${queue.guild.name}] Error emitted from the connection: ${error.message}`);
});
player.on('trackStart', (queue, track) => {
queue.metadata.channel.send(`Started playing: **${track.title}** in **${queue.connection.channel.name}**!`);
});
player.on('trackAdd', (queue, track) => {
queue.metadata.channel.send(`Track **${track.title}** queued!`);
});
player.on('botDisconnect', queue => {
queue.metadata.channel.send('I was manually disconnected from the voice channel, clearing queue!');
});
player.on('channelEmpty', queue => {
queue.metadata.channel.send('Nobody is in the voice channel, leaving...');
});
player.on('queueEnd', queue => {
queue.metadata.channel.send('Queue finished!');
});
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
}
// connects to your database
const con = mysql.createConnection({
host: host,
user: user,
password: password,
database: database
});
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
// Checks if you connected to your database
con.connect(function(err) {
if (err) throw err;
console.log("MySQL connected!");
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName.toLowerCase());
if (!command) return;
try {
await command.execute(interaction, player, con);
} catch (error) {
console.error(error);
await interaction.followUp({
content: 'There was an error while executing this command!',
ephemeral: true
});
}
});
// Login to Discord with your client's token
client.login(token);