forked from Androz2091/discord-music-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.js
57 lines (48 loc) · 2.04 KB
/
play.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
const { SlashCommand, CommandOptionType } = require('slash-create');
const { QueryType } = require('discord-player');
module.exports = class extends SlashCommand {
constructor(creator) {
super(creator, {
name: "play",
description: "Plays a song from youtube",
options: [
{
name: "query",
type: CommandOptionType.STRING,
description: "The song you want to play",
required: true
}
],
guildIDs: process.env.DISCORD_GUILD_ID ? [ process.env.DISCORD_GUILD_ID ] : undefined
});
}
async run (ctx) {
const { client } = require('..');
await ctx.defer();
const guild = client.guilds.cache.get(ctx.guildID);
const channel = guild.channels.cache.get(ctx.channelID);
const query = ctx.options.query;
const searchResult = await client.player
.search(query, {
requestedBy: ctx.user,
searchEngine: QueryType.AUTO
})
.catch(() => {
console.log('he')
});
if (!searchResult || !searchResult.tracks.length) return void ctx.sendFollowUp({ content: "No results were found!" });
const queue = await client.player.createQueue(guild, {
metadata: channel
});
const member = guild.members.cache.get(ctx.user.id) ?? await guild.members.fetch(ctx.user.id);
try {
if (!queue.connection) await queue.connect(member.voice.channel);
} catch {
void client.player.deleteQueue(ctx.guildID);
return void ctx.sendFollowUp({ content: "Could not join your voice channel!" });
}
await ctx.sendFollowUp({ content: `⏱ | Loading your ${searchResult.playlist ? "playlist" : "track"}...` });
searchResult.playlist ? queue.addTracks(searchResult.tracks) : queue.addTrack(searchResult.tracks[0]);
if (!queue.playing) await queue.play();
}
}