-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplayFile.js
47 lines (41 loc) · 2.05 KB
/
playFile.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
const Dysnomia = require("@projectdysnomia/dysnomia");
// Replace TOKEN with your bot account's token
const bot = new Dysnomia.Client("Bot TOKEN");
const playCommand = "!play";
bot.on("ready", () => { // When the bot is ready
console.log("Ready!"); // Log "Ready!"
});
bot.on("error", (err) => {
console.error(err); // or your preferred logger
});
bot.on("messageCreate", (msg) => { // When a message is created
if(msg.content.startsWith(playCommand)) { // If the message content starts with "!play "
if(msg.content.length <= playCommand.length + 1) { // Check if a filename was specified
bot.createMessage(msg.channel.id, "Please specify a filename.");
return;
}
if(!msg.channel.guild) { // Check if the message was sent in a guild
bot.createMessage(msg.channel.id, "This command can only be run in a server.");
return;
}
if(!msg.member.voiceState.channelID) { // Check if the user is in a voice channel
bot.createMessage(msg.channel.id, "You are not in a voice channel.");
return;
}
const filename = msg.content.substring(playCommand.length + 1); // Get the filename
bot.joinVoiceChannel(msg.member.voiceState.channelID).catch((err) => { // Join the user's voice channel
bot.createMessage(msg.channel.id, "Error joining voice channel: " + err.message); // Notify the user if there is an error
console.log(err); // Log the error
}).then((connection) => {
if(connection.playing) { // Stop playing if the connection is playing something
connection.stopPlaying();
}
connection.play(filename); // Play the file and notify the user
bot.createMessage(msg.channel.id, `Now playing **${filename}**`);
connection.once("end", () => {
bot.createMessage(msg.channel.id, `Finished **${filename}**`); // Say when the file has finished playing
});
});
}
});
bot.connect(); // Get the bot to connect to Discord