-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (117 loc) · 3.63 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
122
123
124
125
126
127
128
129
require('dotenv').config()
const Logger = require('./logger/Logger')
const DiscordClient = require('./client/Client')
const GuildsTimeout = require('./guildsTimeout/GuildsTimeout')
const { Player } = require('discord-player')
const registerSlashCommands = require('./commands/registerSlashCommands')
const config = require('./config.js')
const client = new DiscordClient(config)
const player = new Player(client)
const guildsTimeout = new GuildsTimeout(client)
client.once('ready', async (client) => {
Logger.log('Discord Bot ready!')
Logger.debug('Client ready', client)
client.user.setActivity('/help', { type: 'LISTENING' })
const guildIds = Array.from((await client.guilds.fetch()).keys())
registerSlashCommands(guildIds, client)
})
client.on('guildCreate', async (guild) => {
Logger.log('Bot joined a guild!', guild)
registerSlashCommands([guild.id], client)
})
player.on('trackStart', (queue, track) => {
Logger.debug('trackStarted', queue, track)
queue.metadata.channel.send(
`🎶 | Now playing **${track.title}** in **${queue.connection.channel.name}**!`
)
})
player.on('error', (queue, error) => {
Logger.error(
`[${queue.guild.name}] Error emitted from the player: ${error.message}`
)
try {
queue.metadata.interaction.followUp({
content: `❌ **I failed to execute that command.** Encountered: \`${error.message}\``,
})
} catch (error) {
Logger.error(error)
}
})
player.on('connectionError', (queue, error) => {
Logger.error(
`[${queue.guild.name}] Error emitted from the connection: ${error.message}`
)
})
player.on('trackAdd', (queue, track) => {
Logger.debug('trackAdded', queue, track)
try {
queue.metadata.channel.send(`🗒️ | **${track.title}** added to queue!`)
guildsTimeout.clearTimeout(queue)
} catch (error) {
Logger.error(error)
}
})
player.on('connectionCreate', (queue, connection) => {
Logger.debug('playerConenctionCreated', queue, connection)
try {
queue.metadata.interaction.followUp({
content: `👍 **Joined \`${connection.channel.name}\` and bound to <#${queue.metadata.channel.id}>**`,
})
Logger.log(
`New Voice Connection on ${
queue.guild.id
}. Total voice connections: ${client.addVoiceConnection(queue.guild.id)}`
)
} catch (error) {
Logger.error(error)
}
})
player.on('botDisconnect', (queue) => {
Logger.debug('playerBotDisconnected', queue)
Logger.log(
`Voice Channel on Guild ${
queue.guild.id
} disconnected. Total voice connections: ${client.deductVoiceConnection(
queue.guild.id
)}`
)
try {
guildsTimeout.clearTimeout(queue)
} catch (error) {
Logger.error(error)
}
})
player.on('channelEmpty', (queue) => {
Logger.debug('playerChannelEmptied', queue)
try {
if (client.config.leaveOnEmpty) {
if (queue.connection) queue.connection.disconnect()
queue.metadata.channel.send('❌ | Leaving the channel, nobody is here')
guildsTimeout.clearTimeout(queue)
}
} catch (error) {
Logger.error(error)
}
})
player.on('queueEnd', (queue) => {
Logger.debug('queueEnded', queue)
try {
guildsTimeout.setTimeout(queue)
} catch (error) {
Logger.error(error)
}
})
client.on('interactionCreate', async (interaction) => {
Logger.debug('interactionCreated', interaction)
const command = client.commands.get(interaction.commandName.toLowerCase())
try {
command.execute(interaction, player)
} catch (error) {
Logger.log(error)
interaction.followUp({
content: 'There was a problem executing the command that you requested',
})
}
})
client.login(client.config.token)
Logger.debug('Bot Settings', client.config)