-
Notifications
You must be signed in to change notification settings - Fork 354
/
voice_manager.js
361 lines (338 loc) · 13.1 KB
/
voice_manager.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
const Discord = require('discord.js');
const DiscordVoice = require('@discordjs/voice');
const EventEmitter = require('events');
function log_state(manager) {
console.log(`State for ${manager.guild_name}(${manager.guild_id}):\n\tstate: ${manager.state}\n\tconnection: ${manager.connection_state}\n\tplayer: ${manager.player_state}\n\tresource: ${manager.resource_state}\n\tqueue size: ${manager.queue.length}`);
}
function check_state(manager) {
if (manager.state === 'playing' && manager.player_state === 'idle' && manager.resource_state !== 'playing') {
console.log('completed playback');
manager.state = 'finished_playback';
manager.events.emit('done');
play_next(manager);
}
if (manager.state === 'finished_playback' && manager.resource_state === 'closed') {
if (manager.player_state === 'idle' || manager.player_state === 'buffering') {
//console.log('STUCK DETECTION TRIGGERED, SKIPPING AUDIO');
//manager.events.emit('error');
//play_next(manager);
}
}
if (manager.player_state === 'buffering') {
if (manager.playing.playStream.readable) {
console.log("Attempting to employ super hack!");
manager.playing.playStream.emit('readable');
}
}
if (manager.state !== 'playing' && manager.player_state === 'playing') {
manager.events.emit('playing');
manager.state = 'playing';
}
}
function set_resource_callbacks(manager,resource) {
resource.playStream.on('end', () => {
console.log(`Playing resource in ${manager.guild_name}(${manager.guild_id}) ended`);
manager.resource_state = 'ended';
log_state(manager);
check_state(manager);
});
resource.playStream.on('close', () => {
console.log(`Playing resource in ${manager.guild_name}(${manager.guild_id}) closed`);
manager.resource_state = 'closed';
log_state(manager);
check_state(manager);
});
resource.playStream.on('error', (error) => {
console.log(`Playing resource in ${manager.guild_name}(${manager.guild_id}) had an error: ${error}`);
manager.resource_state = 'error';
log_state(manager);
check_state(manager);
});
}
function register_player_callbacks(manager,player) {
player.on('stateChange', (oldState, newState) => {
console.log(`Audio player for ${manager.guild_name}(${manager.guild_id}) transitioned from ${oldState.status} to ${newState.status}`);
if (manager.player_state !== oldState.status) {
console.log(`We thought it was in ${manager.player_state} instead!`);
}
manager.player_state = newState.status;
log_state(manager);
check_state(manager);
});
}
function play_next(manager) {
const next = manager.queue.shift();
if (next === undefined) {
console.log(`nothing queued, leaving voice in ${manager.guild_name}(${manager.guild_id})`);
manager.state = 'closing';
const connection = DiscordVoice.getVoiceConnection(manager.guild_id);
connection.destroy();
delete playback_managers[manager.guild_id];
} else {
console.log(`starting playback of next item in queue in ${manager.guild_name}(${manager.guild_id})`);
manager.playing = next.resource;
manager.events = next.events;
set_resource_callbacks(manager,next.resource);
manager.player = DiscordVoice.createAudioPlayer();
register_player_callbacks(manager,manager.player);
manager.player.play(next.resource);
const connection = DiscordVoice.getVoiceConnection(manager.guild_id);
if (connection) {
connection.subscribe(manager.player);
} else {
console.log(`!!COULDN'T get voice connection, killing voice player for ${manager.guild_name}(${manager.guild_id})`);
delete playback_managers[manager.guild_id];
}
}
}
function make_playback_manager(guild, connection, player, resource) {
const manager = {
guild_id: guild.id,
guild_name: guild.name,
player: player,
playing: resource,
events: new EventEmitter(),
queue: [],
loop: false,
state: 'starting',
connection_state: 'signalling',
player_state: 'buffering',
resource_state: 'playing',
};
set_resource_callbacks(manager,resource);
connection.on('stateChange', (oldState, newState) => {
console.log(`Connection for ${guild.name}(${guild.id}) transitioned from ${oldState.status} to ${newState.status}`);
if (manager.connection_state !== oldState.status) {
console.log(`Connection for ${guild.name}(${guild.id}) was in state ${oldState} but we thought it was in ${manager.connection_state}`)
}
manager.connection_state = newState.status;
log_state(manager);
if (manager.state === 'starting' && manager.connection_state === 'ready') {
console.log(`starting playback in ${guild.name}(${guild.id})`);
manager.state = 'playing';
//connection.subscribe(player);
}
});
register_player_callbacks(manager,player);
return manager;
}
playback_managers = {};
function make_on_finished(connection,player,events,manager) {
return () => {
events.emit('done');
const next = manager.queue.shift();
if (next === undefined) {
connection.destroy();
delete playback_managers[manager.guild_id];
} else {
player.once('playing', () => {
next.events.emit('playing', player);
player.once('idle', make_on_finished(connection,player,next.events,manager));
});
player.play(next.resource);
manager.playing = next.resource;
manager.events = next.events;
}
};
}
exports.getUserVoiceChannel = (msg) => {
var voiceChannelArray = msg.guild.channels.cache.filter((v)=>v.type == "GUILD_VOICE").filter((v)=>v.members.has(msg.author.id));
console.log(JSON.stringify(voiceChannelArray));
if(voiceChannelArray.length == 0) return null;
else return voiceChannelArray.at(0);
}
exports.queue = (guild_channel, resource, description) => {
resource.playStream.on('error', () => {
console.log('resource error');
});
resource.playStream.on('end', () => {
console.log('resource end');
});
resource.playStream.on('pause', () => {
console.log('resource pause');
});
resource.playStream.on('resume', () => {
console.log('resource resume');
});
if (playback_managers.hasOwnProperty(guild_channel.guild.id)) {
console.log(`already playing in ${guild_channel.guild.name}(${guild_channel.guild.id})`);
const manager = playback_managers[guild_channel.guild.id];
const events = new EventEmitter();
manager.queue.push({
resource: resource,
events: events,
description: description,
});
return events;
} else {
// Nothing already playing for this guild
const connection = DiscordVoice.joinVoiceChannel({
channelId: guild_channel.id,
guildId: guild_channel.guild.id,
adapterCreator: guild_channel.guild.voiceAdapterCreator,
});
// Set up a new audio player
const player = DiscordVoice.createAudioPlayer();
player.on('stateChange', (oldState, newState) => {
console.log(`Audio player for ${guild_channel.guild.name}(${guild_channel.guild.id}) transitioned from ${oldState.status} to ${newState.status}`);
});
player.play(resource);
// Make a playback manager to manage playback in this channel
const manager = make_playback_manager(guild_channel.guild, connection, player, resource);
manager.description = description;
const events = manager.events;
playback_managers[guild_channel.guild.id] = manager;
// Start playback as soon as the connection is ready
connection.once('ready', () => {
connection.subscribe(player);
});
return events;
}
}
exports.skip = (guild_id) => {
if (playback_managers.hasOwnProperty(guild_id)) {
const manager = playback_managers[guild_id];
manager.player.stop(true);
return true;
}
return false;
}
exports.pause = (guild_id) => {
if (playback_managers.hasOwnProperty(guild_id)) {
const manager = playback_managers[guild_id];
return manager.player.pause();
}
return false;
}
exports.unpause = (guild_id) => {
if (playback_managers.hasOwnProperty(guild_id)) {
const manager = playback_managers[guild_id];
return manager.player.unpause();
}
return false;
}
exports.stop_playback = (guild_id) => {
if (playback_managers.hasOwnProperty(guild_id)) {
const manager = playback_managers[guild_id];
manager.queue.length = 0;
manager.player.stop(true);
return true;
}
return false;
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
exports.shuffle = (guild_id) => {
if (playback_managers.hasOwnProperty(guild_id)) {
const manager = playback_managers[guild_id];
shuffleArray(manager.queue);
return true;
}
return false;
}
exports.loop = (guild_id) => {
throw 'Not implemented!';
if (playback_managers.hasOwnProperty(guild_id)) {
const manager = playback_managers[guild_id];
manager.loop = !manager.loop;
return manager.loop;
}
return 'error';
}
exports.dequeue = (guild_id, index) => {
if (playback_managers.hasOwnProperty(guild_id)) {
const manager = playback_managers[guild_id];
if ( index >=0 && index < manager.queue.length) {
manager.queue.splice(index, 1);
return true;
}
}
return false;
}
exports.show_queue = async (msg, compact) => {
const guild_id = msg.guild.id;
if (playback_managers.hasOwnProperty(guild_id)) {
const manager = playback_managers[guild_id];
if (manager.queue.length > 9) {
compact = true;
}
if(compact) {
let list = manager.queue.map((entry) => {
if (entry.hasOwnProperty('description')) {
if (entry.description.hasOwnProperty('content')) {
return entry.description.content;
}
}
return '<unknown>';
});
let playing = '<unknown>';
if (manager.hasOwnProperty('description')) {
if (manager.description.hasOwnProperty('content')) {
playing = manager.description.content;
}
}
let response = `Now Playing: ${playing}\n`;
let index = 0;
while (list.length > 0) {
let entry = `${index}: ${list.shift()}\n`;
index += 1;
let newmsg = `${response}${entry}`;
if(newmsg.length > (1024 - 8)){
await msg.channel.send(response);
response = entry;
} else {
response = newmsg;
}
}
msg.channel.send(response);
} else {
//Assume we can stuff all the embeds into one message
let list = manager.queue.map((entry) => {
if (entry.hasOwnProperty('description')) {
if (entry.description.hasOwnProperty('embed')) {
return entry.description.embed;
} else if (entry.description.hasOwnProperty('content')) {
return new Discord.MessageEmbed().setTitle(`Queued: ${entry.description.content}`);
}
}
return new Discord.MessageEmbed().setTitle('<unknown>');
});
let playing = new Discord.MessageEmbed().setTitle('Queued: <unknown>');
if (manager.hasOwnProperty('description')) {
if (manager.description.hasOwnProperty('embed')) {
playing = manager.description.embed;
} else if (manager.description.hasOwnProperty('content')) {
playing = new Discord.MessageEmbed().setTitle(`Queued: ${manager.description.content}`);
}
}
playing.title.replace('Queued','Now Playing');
let embeds = [playing];
embeds = embeds.concat(list);
await msg.channel.send({embeds: embeds});
}
return true;
}
return false;
}
exports.EventWaiter = (events) => {
return {
playing: () => {
return new Promise((resolve, reject) => {
events.once('playing',() => {
resolve();
});
});
},
done: () => {
return new Promise((resolve, reject) => {
events.once('done', () => {
resolve();
});
});
}
}
}