-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
200 lines (165 loc) · 6.21 KB
/
main.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
//Bring in dependencies and environment variables
require('dotenv').config();
const Discord = require('discord.js');
const ytdl = require('ytdl-core');
const token = process.env.TOKEN;
const prefix ="!";
//This contains the queues
const queue = new Map();
//Create a Client and Log In
const client = new Discord.Client();
client.login(token);
//Log Connection Statuses
client.once('ready', () => {
console.log("Client is Ready!");
});
client.once('reconnecting', () => {
console.log("Client is reconnecting!");
});
client.once('disconnect', () => {
console.log("Client has disconnected!");
});
//Read Messages and Act
client.on('message', async message => {
//Ignore Bot's messages
if (message.author.bot) return;
//Ignore messages without command prefix
if (!message.content.startsWith(prefix)) return;
const serverQueue = queue.get(message.guild.id);
//Determine which command to apply
const args = message.content.split(" ");
//Ensure member is in a voice channel and that bot can join it
const voiceChannel = message.member.voice.channel;
if((!voiceChannel)) {
return message.channel.send("You need to be in a voice channel to hear music or control its playback.");
};
const permissions = voiceChannel.permissionsFor(message.client.user);
if((!permissions.has("CONNECT")) || (!permissions.has("SPEAK"))) {
return message.channel.send("You need to be in a voice channel that I can join to hear music!");
};
switch(args[0].toLowerCase()) {
case prefix+"play" :
addOrCreateQueue(message, args, serverQueue, voiceChannel);
break;
case prefix+"stop":
stop(message, serverQueue);
break;
case prefix+"skip":
skip(message, serverQueue);
break;
case prefix+"pause":
pause(message, serverQueue);
break;
case prefix+"resume":
resume(message, serverQueue);
break;
case prefix+"volume":
setVolume(message, args, serverQueue);
break;
default:
message.channel.send("Invalid command! Choose one of the following options: !play !pause !resume !stop !skip !volume")
}
});
//Function that creates song queues and invokes play function
//QueueConstructs are created and added to queue map
async function addOrCreateQueue(message, args, serverQueue, voiceChannel) {
const songInfo = await ytdl.getInfo(args[1]);
const song = {
title: songInfo.videoDetails.title,
url: songInfo.videoDetails.video_url,
};
//Create new queue if one doesn't exist
if (!serverQueue) {
const queueConstruct = {
textChannel: message.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
Volume: 3,
playing: true
};
//referencing main's queue map
queue.set(message.guild.id, queueConstruct);
queueConstruct.songs.push(song);
//Get in channel and begin play
try {
var connection = await voiceChannel.join();
queueConstruct.connection = connection;
play(message.guild, queueConstruct.songs[0]);
} catch (err) {
console.log(err);
if(err.startsWith('[VOICE_CONNECTION_TIMEOUT]')){
play(message.guild, queueConstruct.songs[0]);
} else {
queue.delete(message.guild.id);
};
return message.channel.send("Something went wrong creating the queue!: " + err);
}
} else {
serverQueue.songs.push(song);
return message.channel.send(song.title + ' has been added to the queue!');
};
};
//Recursive function play until queue is empty
function play(guild, song){
const serverQueue = queue.get(guild.id);
if (!song) {
serverQueue.voiceChannel.leave();
queue.delete(guild.id);
return;
}
const dispatcher = serverQueue.connection
.play(ytdl(song.url, { quality: "highestaudio", filter: "audioonly"}))
.on("finish", ()=> {
serverQueue.songs.shift();
play(guild, serverQueue.songs[0]);
})
.on("error", error => console.error(error));
dispatcher.setVolume(0.1 * serverQueue.Volume);
serverQueue.textChannel.send('Now playing: ~ ' + song.title + ' ~');
};
//Set Volume
function setVolume(message, args, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send("You have to be in a voice channel to set the volume!");
if (!serverQueue)
return message.channel.send("There is no song playing!");
serverQueue.connection.dispatcher.setVolume(parseInt(args[1]) * 0.1);
message.channel.send("Set volume to " + args[1]);
};
//Skip current song
function skip(message, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send("You have to be in a voice channel to skip the song!");
if (!serverQueue)
return message.channel.send("There is no song playing!");
serverQueue.connection.dispatcher.end();
message.channel.send("Skipping");
};
//Stop playback
function stop(message, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send("You have to be in a voice channel to stop the music!");
if (!serverQueue)
return message.channel.send("There is no song playing!");
serverQueue.songs = [];
serverQueue.connection.dispatcher.end();
};
//Pause playback
function pause(message, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send("You have to be in a voice channel to pause the music!");
if (!serverQueue)
return message.channel.send("There is no song playing!");
serverQueue.connection.dispatcher.pause();
message.channel.send("Pausing playback!");
};
//Resume playback
function resume(message, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send("You have to be in a voice channel to pause the music!");
if (!serverQueue)
return message.channel.send("There is no song playing!");
serverQueue.connection.dispatcher.resume();
message.channel.send("Resuming playback!");
};