-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
143 lines (110 loc) · 3.35 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {
Client,
GatewayIntentBits,
SlashCommandBuilder,
REST,
Routes,
ActivityType
} from 'discord.js';
import { inVoiceChannel, leaveVoiceChannel, getUsersInVoice, MINUTES } from './utils/utils.js';
import commands from './commands/index.js';
import { servers } from './utils/utils.js';
const { TOKEN, CLIENT_ID, GUILD_ID } = process.env;
if (!TOKEN) throw new Error('Please provide a bot token!');
if (!CLIENT_ID) throw new Error('Please provide a client id!');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates
]
});
client.on('ready', async () => {
const commandData = [];
// setup commands
for (const command of Object.values(commands)) {
if (command.interactionOptions) {
commandData.push(command.interactionOptions.toJSON());
continue;
}
const data = new SlashCommandBuilder()
.setName(command.name)
.setDescription(command.description);
commandData.push(data.toJSON());
}
const rest = new REST().setToken(TOKEN);
console.info(`Started refreshing ${commandData.length} application (/) commands.`);
if (GUILD_ID) {
const data = await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), {
body: commandData
});
// @ts-ignore
console.info(`Successfully reloaded ${data?.length} application (/) commands.`);
// clear guild commands
// guild.commands.set([]);
// console.log('Commands cleared');
} else {
// register global commands
const data = await rest.put(Routes.applicationCommands(CLIENT_ID), { body: commandData });
// @ts-ignore
console.info(`Successfully reloaded ${data?.length} global application (/) commands.`);
}
console.info('Ready!');
if (!client.user) return;
client.user.setPresence({
status: 'online',
activities: [
{
name: 'Listening to music | /help',
type: ActivityType.Custom
}
]
});
});
client.on('reconnecting', () => {
console.log('Reconnecting!');
});
client.on('disconnect', () => {
console.log('Disconnect!');
});
client.on('voiceStateUpdate', (oldState, newState) => {
// disconnect
if (oldState.channelId && !newState.channelId) {
if (!client.user) return;
const guildQueue = servers.get(newState.guild.id);
if (!guildQueue) return;
// bot was Disconnected
if (newState.id === client.user.id) {
if (!guildQueue.textChannel) return;
// bot gets disconnected from voice channel
guildQueue.textChannel.send('Left voice channel');
leaveVoiceChannel(newState.guild.id);
return;
}
// other user gets disconnected from voice channel
if (guildQueue && getUsersInVoice(guildQueue) < 2) {
setTimeout(() => {
if (getUsersInVoice(guildQueue) < 2) {
if (!guildQueue.textChannel) return;
// Left the voice channel
guildQueue.textChannel.send('No one in the voice channel');
leaveVoiceChannel(newState.guild.id);
}
}, 5 * MINUTES);
}
}
});
client.on('interactionCreate', (interaction) => {
if (!interaction.isChatInputCommand()) return;
const { commandName } = interaction;
const command = commands[commandName];
if (!command) return;
if (command.permissions?.memberInVoice && !inVoiceChannel(interaction)) {
return;
}
if (!interaction.guildId) return;
const server = servers.get(interaction.guildId);
command.interaction({ interaction, server });
});
client.login(TOKEN);