-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.js
130 lines (115 loc) · 4.31 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
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
const { play } = require("../include/play");
const { YOUTUBE_API_KEY } = require("../config");
const ytdl = require("ytdl-core");
const YouTubeAPI = require("simple-youtube-api");
const youtube = new YouTubeAPI(YOUTUBE_API_KEY);
const { MessageEmbed } = require('discord.js');
module.exports = {
name: "play",
description: "Plays audio from YouTube",
async execute(message, args) {
const { channel } = message.member.voice;
if (!args.length) return message.reply("Usage: /play <YouTube URL | Video Name>").catch(console.error);
if (!channel) return message.reply("You need to join a voice channel first!").catch(console.error);
const permissions = channel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT"))
return message.reply("Cannot connect to voice channel, missing permissions");
if (!permissions.has("SPEAK"))
return message.reply("I cannot speak in this voice channel, make sure I have the proper permissions!");
const search = args.join(" ");
const videoPattern = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/gi;
const playlistPattern = /^.*(list=)([^#\&\?]*).*/gi;
const url = args[0];
const urlValid = videoPattern.test(args[0]);
// Start the playlist if playlist url was provided
if (!videoPattern.test(args[0]) && playlistPattern.test(args[0])) {
return message.client.commands.get("playlist").execute(message, args);
}
const serverQueue = message.client.queue.get(message.guild.id);
const queueConstruct = {
textChannel: message.channel,
channel,
connection: null,
songs: [],
loop: false,
volume: 100,
playing: true
};
let songInfo = null;
let song = null;
if (urlValid) {
try {
let searchEmbed = new MessageEmbed()
.setTitle("Searching")
.setDescription(`Searching for song with url **${url}**.`)
.setColor(`#FF0000`);
message.channel.send(searchEmbed).then((msg) => {
setTimeout(() => {
if(msg.deletable){
msg.delete();
}
}, 10000);
})
songInfo = await ytdl.getInfo(url);
song = {
title: songInfo.title,
url: songInfo.video_url,
duration: songInfo.length_seconds
};
} catch (error) {
if (error.message.includes("copyright")) {
return message
.reply("⛔ The video could not be played due to copyright protection ⛔")
.catch(console.error);
} else {
console.error(error);
}
}
} else {
try {
let searchEmbed = new MessageEmbed()
.setTitle("Searching")
.setDescription(`Searching for song with name **${search}**.`)
.setColor(`#FF0000`);
message.channel.send(searchEmbed).then((msg) => {
setTimeout(() => {
if(msg.deletable){
msg.delete();
}
}, 10000);
})
const results = await youtube.searchVideos(search, 1);
songInfo = await ytdl.getInfo(results[0].url);
song = {
title: songInfo.title,
url: songInfo.video_url,
duration: songInfo.length_seconds
};
} catch (error) {
console.error(error);
}
}
if (serverQueue) {
serverQueue.songs.push(song);
let addToQueueEmbed = new MessageEmbed()
.setTitle("Added to queue")
.setDescription(`You've successfully added **${song.title}** to the queue.`)
.setColor("#388e3c");
return serverQueue.textChannel.send(addToQueueEmbed).catch(console.error);
} else {
queueConstruct.songs.push(song);
}
if (!serverQueue) message.client.queue.set(message.guild.id, queueConstruct);
if (!serverQueue) {
try {
queueConstruct.connection = await channel.join();
play(queueConstruct.songs[0], message);
} catch (error) {
console.error(`Could not join voice channel: ${error}`);
message.client.queue.delete(message.guild.id);
await channel.leave();
return message.channel.send(`Could not join the channel: ${error}`).catch(console.error);
}
}
}
};