This repository has been archived by the owner on Sep 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
303 lines (256 loc) · 8.28 KB
/
commands.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const {
joinVoiceChannel,
createAudioResource,
AudioPlayerStatus,
createAudioPlayer,
} = require("@discordjs/voice");
const ytdl = require("ytdl-core");
const YouTube = require("youtube-sr");
const { setPrefix } = require("./Database/database.js");
//Create an array to store the queue of songs
let queue = [];
//Create a boolean to store whether a song is currently playing
let isPlaying = false;
async function playCommand(message, args) {
const url = args[0]; //Assuming the URL is the first argument passed to the command
if (isPlaying) {
queue.push(url);
message.reply(`Added ${url} to the queue!`);
} else {
isPlaying = true;
play(connection, url, message);
}
}
//This function will deal with the play command
async function play(connection, message, args) {
const voiceChannel = message.member.voice.channel; //Get the user's voice channel
//The user must be present in the voice channel for the bot to join and play music
if (!voiceChannel) {
return message.channel.send(
"You need to be in a voice channel to play music!"
);
}
//Set up the connection to the voice channel
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id.toString(),
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});
//Debugging
connection.on("debug", console.debug);
//Log the connection state changes - this may produce a lot of console output, so consider commenting it out
connection.on("stateChange", (oldState, newState) => {
console.log(
`Connection transitioned from ${oldState.status} to ${newState.status}`
);
});
const url = args[0]; //Assuming the URL is the first argument passed to the command
const stream = ytdl(url, { filter: "audioonly" }); //Create a stream from the YouTube video
console.log("Stream created with URL:", url); //Debugging
const resource = createAudioResource(stream); //Create an audio resource from the stream
const player = createAudioPlayer(); //Create an audio player
player.play(resource); //Play the audio resource
player.on(AudioPlayerStatus.Idle, () => {
if (queue.length > 0) {
play(connection, queue.shift(), message);
} else {
connection.destroy();
isPlaying = false;
}
});
player.on("error", (error) => {
console.error("Error in player:", error);
message.channel.send(
"An error occurred while playing the audio. Have a look at the bot's console for more details."
);
});
connection.on("error", (error) => {
console.error("Error in connection:", error);
message.channel.send(
"An error occurred in the connection. Have a look at the bot's console for more details."
);
});
connection.subscribe(player);
return message.reply(`Now playing: ${url}!`);
}
async function skipCommand(message, args) {
if (isPlaying) {
/*
There is an option to add a role check here to allow only certain users to skip songs
For example, you could add a DJ role and only allow users with that role to skip songs
Upon creating a DJ role in the server settings, paste the role ID in the code below
if (message.member.roles.cache.has('DJ_ROLE_ID')) {
// Execute DJ command (e.g. paster the skip command here)
} else {
message.reply('You need the DJ role to use this command.');
}
*/
message.channel.send("Skipping current song!");
connection.destroy();
isPlaying = false;
} else {
message.channel.send("There is no song to skip!");
}
}
async function stopCommand(message, args) {
if (isPlaying) {
message.channel.send("Stopping playback!");
connection.destroy();
isPlaying = false;
queue = []; //Empty the queue
} else {
message.channel.send("There is no song to stop!");
}
}
async function pauseCommand(message, args) {
if (isPlaying) {
message.channel.send("Pausing playback!");
connection.pause();
} else {
message.channel.send("There is no song to pause!");
}
}
async function resumeCommand(message, args) {
if (isPlaying) {
message.channel.send("Resuming playback!");
connection.resume();
} else {
message.channel.send("There is no song to resume!");
}
}
async function volumeControlCommand(message, args) {
if (isPlaying) {
const volume = args[0];
if (volume) {
message.channel.send(`Setting volume to ${volume}!`);
connection.setVolume(volume);
} else {
message.channel.send(`The current volume is ${connection.state.volume}!`);
}
} else {
message.channel.send("There is no song to set the volume for!");
}
}
async function searchCommand(message, args) {
//Retrieve the search term from the arguments
const searchTerm = args.join(" ");
if (!searchTerm) {
return message.channel.send("You must specify a search term!");
}
try {
//Search for videos based on the search term
const videos = await YouTube.search(searchTerm, { limit: 5 });
//Check if any videos were found
if (videos.length === 0) {
return message.channel.send("No videos found!");
}
//Create a string with the video titles to display
let content = "Here are the top 5 results:\n";
videos.forEach((video, index) => {
content += `${index + 1}: [${video.title}](${video.url})\n`;
});
content += "Please enter the number of the video you want to play!";
//Send the message with the video titles
message.channel.send(content);
//Create a filter to check if the user's message is a number between 1 and 5
const filter = (response) =>
!isNaN(response.content) && response.content < 6 && response.content > 0;
//Create a message collector
const collector = message.channel.createMessageCollector({
filter,
time: 30000,
});
collector.on("collect", async (response) => {
const videoIndex = parseInt(response.content) - 1;
const url = videos[videoIndex].url;
//Play the video
play(connection, message, url);
});
collector.on("end", (collected, reason) => {
if (reason === "time") {
message.channel.send("Video selection timed out!");
}
});
} catch (error) {
console.error(error);
message.channel.send("An error occurred while searching for videos!");
}
}
module.exports = {
ping: {
description: "Bot responds with Pong!",
execute(message, args) {
message.channel.send("Pong!");
},
},
setprefix: {
description: "Sets the prefix for the bot (Admin only)",
async execute(message, args) {
if (message.member.permissions.has("ADMINISTRATOR")) {
const newPrefix = args[0];
if (newPrefix) {
await setPrefix(message.guild.id, newPrefix);
message.channel.send(`Prefix set to ${newPrefix}`);
} else {
message.channel.send("You must specify a new prefix!");
}
} else {
message.channel.send(
"You must have admin privileges to use this command!"
);
}
},
},
help: {
description: "List all of my commands.",
execute(message, args, commands) {
let helpMessage = "Here are the available commands:\n";
for (const [name, command] of Object.entries(commands)) {
helpMessage += `!${name}: ${command.description}\n`;
}
message.channel.send(helpMessage);
},
},
playCommand: {
description: "Play a song from YouTube",
async execute(message, args) {
playCommand(message, args);
},
},
skip: {
description: "Skip the current song",
async execute(message, args) {
skipCommand(message, args);
},
},
stop: {
description: "Stop playing queued songs",
async execute(message, args) {
stopCommand(message, args);
},
},
pause: {
description: "Pause the currently playing song",
async execute(message, args) {
pauseCommand(message, args);
},
},
resume: {
description: "Resume the currently playing song",
async execute(message, args) {
resumeCommand(message, args);
},
},
volume: {
description: "Set the volume of the currently playing song",
async execute(message, args) {
volumeControlCommand(message, args);
},
},
search: {
description: "Search for a video on YouTube",
async execute(message, args) {
searchCommand(message, args);
},
},
};