forked from TannerGabriel/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (95 loc) · 3.55 KB
/
index.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require('dotenv').config()
const fs = require('fs');
const Discord = require('discord.js');
const Client = require('./client/Client');
const config = require('./config.json');
const {Player} = require('discord-player');
const { ActivityType } = require('discord.js');
const client = new Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
console.log(client.commands);
const player = new Player(client);
player.on('connectionCreate', (queue) => {
queue.connection.voiceConnection.on('stateChange', (oldState, newState) => {
const oldNetworking = Reflect.get(oldState, 'networking');
const newNetworking = Reflect.get(newState, 'networking');
const networkStateChangeHandler = (oldNetworkState, newNetworkState) => {
const newUdp = Reflect.get(newNetworkState, 'udp');
clearInterval(newUdp?.keepAliveInterval);
}
oldNetworking?.off('stateChange', networkStateChangeHandler);
newNetworking?.on('stateChange', networkStateChangeHandler);
});
});
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.send(`▶ | Started playing: **${track.title}** in **${queue.connection.channel.name}**!`);
});
player.on('trackAdd', (queue, track) => {
queue.metadata.send(`🎶 | Track **${track.title}** queued!`);
});
player.on('botDisconnect', queue => {
queue.metadata.send('❌ | I was manually disconnected from the voice channel, clearing queue!');
});
player.on('channelEmpty', queue => {
queue.metadata.send('❌ | Nobody is in the voice channel, leaving...');
});
player.on('queueEnd', queue => {
queue.metadata.send('✅ | Queue finished!');
});
client.once('ready', async () => {
console.log('Ready!');
});
client.on('ready', function() {
client.user.setPresence({
activities: [{ name: config.activity, type: Number(config.activityType) }],
status: Discord.PresenceUpdateStatus.Online,
});
});
client.once('reconnecting', () => {
console.log('Reconnecting!');
});
client.once('disconnect', () => {
console.log('Disconnect!');
});
client.on('messageCreate', async message => {
if (message.author.bot || !message.guild) return;
if (!client.application?.owner) await client.application?.fetch();
if (message.content === '!deploy' && message.author.id === client.application?.owner?.id) {
await message.guild.commands
.set(client.commands)
.then(() => {
message.reply('Deployed!');
})
.catch(err => {
message.reply('Could not deploy commands! Make sure the bot has the application.commands permission!');
console.error(err);
});
}
});
client.on('interactionCreate', async interaction => {
const command = client.commands.get(interaction.commandName.toLowerCase());
try {
if (interaction.commandName == 'ban' || interaction.commandName == 'userinfo') {
command.execute(interaction, client);
} else {
command.execute(interaction, player);
}
} catch (error) {
console.error(error);
interaction.followUp({
content: 'There was an error trying to execute that command!',
});
}
});
client.login(process.env.DISCORD_TOKEN);