Fixed issue where sound player would stop playing audio in a server until the bot is restarted

This commit is contained in:
Essem 2021-08-30 23:44:22 -05:00
parent 382f4da29e
commit b6db4a869e
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C

View file

@ -69,16 +69,11 @@ export async function play(client, sound, message, music = false) {
if (music) { if (music) {
queues.set(voiceChannel.guild.id, oldQueue ? [...oldQueue, tracks[0].track] : [tracks[0].track]); queues.set(voiceChannel.guild.id, oldQueue ? [...oldQueue, tracks[0].track] : [tracks[0].track]);
} }
let connection; const connection = await manager.join({
if (player) { guild: voiceChannel.guild.id,
connection = player.player; channel: voiceChannel.id,
} else { node: node.id
connection = await manager.join({ });
guild: voiceChannel.guild.id,
channel: voiceChannel.id,
node: node.id
});
}
if (oldQueue && music) { if (oldQueue && music) {
return `Your tune \`${tracks[0].info.title}\` has been added to the queue!`; return `Your tune \`${tracks[0].info.title}\` has been added to the queue!`;
@ -88,19 +83,20 @@ export async function play(client, sound, message, music = false) {
} }
} }
export async function nextSong(client, message, connection, track, info, music, voiceChannel, loop = false, inQueue = false, lastTrack = null) { export async function nextSong(client, message, connection, track, info, music, voiceChannel, loop = false, lastTrack = null) {
skipVotes.set(voiceChannel.guild.id, { count: 0, ids: [] }); skipVotes.delete(voiceChannel.guild.id);
const parts = Math.floor((0 / info.length) * 10); const parts = Math.floor((0 / info.length) * 10);
let playingMessage; let playingMessage;
if (!music && players.get(voiceChannel.guild.id)) { if (!music && players.get(voiceChannel.guild.id)) {
const playMessage = players.get(voiceChannel.guild.id).playMessage; const playMessage = players.get(voiceChannel.guild.id).playMessage;
try { try {
players.delete(voiceChannel.guild.id);
playMessage.delete(); playMessage.delete();
} catch { } catch {
// no-op // no-op
} }
} }
if (lastTrack === track) { if (music && lastTrack === track && players.get(voiceChannel.guild.id)) {
playingMessage = players.get(voiceChannel.guild.id).playMessage; playingMessage = players.get(voiceChannel.guild.id).playMessage;
} else { } else {
playingMessage = await client.createMessage(message.channel.id, !music ? "🔊 Playing sound..." : { playingMessage = await client.createMessage(message.channel.id, !music ? "🔊 Playing sound..." : {
@ -131,7 +127,7 @@ export async function nextSong(client, message, connection, track, info, music,
} }
await connection.play(track); await connection.play(track);
players.set(voiceChannel.guild.id, { player: connection, type: music ? "music" : "sound", host: message.author.id, voiceChannel: voiceChannel, originalChannel: message.channel, loop: loop, playMessage: playingMessage }); players.set(voiceChannel.guild.id, { player: connection, type: music ? "music" : "sound", host: message.author.id, voiceChannel: voiceChannel, originalChannel: message.channel, loop: loop, playMessage: playingMessage });
if (inQueue && connection.listeners("error").length === 0) { if (connection.listeners("error").length === 0) {
connection.on("error", (error) => { connection.on("error", (error) => {
if (playingMessage.channel.messages.get(playingMessage.id)) playingMessage.delete(); if (playingMessage.channel.messages.get(playingMessage.id)) playingMessage.delete();
const playMessage = players.get(voiceChannel.guild.id).playMessage; const playMessage = players.get(voiceChannel.guild.id).playMessage;
@ -149,7 +145,7 @@ export async function nextSong(client, message, connection, track, info, music,
const queue = queues.get(voiceChannel.guild.id); const queue = queues.get(voiceChannel.guild.id);
const player = players.get(voiceChannel.guild.id); const player = players.get(voiceChannel.guild.id);
let newQueue; let newQueue;
if (player.loop) { if (player && player.loop) {
queue.push(queue.shift()); queue.push(queue.shift());
newQueue = queue; newQueue = queue;
} else { } else {
@ -164,13 +160,13 @@ export async function nextSong(client, message, connection, track, info, music,
if (music) await client.createMessage(message.channel.id, "🔊 The current voice channel session has ended."); if (music) await client.createMessage(message.channel.id, "🔊 The current voice channel session has ended.");
try { try {
if (playingMessage.channel.messages.get(playingMessage.id)) await playingMessage.delete(); if (playingMessage.channel.messages.get(playingMessage.id)) await playingMessage.delete();
if (player.playMessage.channel.messages.get(player.playMessage.id)) await player.playMessage.delete(); if (player && player.playMessage.channel.messages.get(player.playMessage.id)) await player.playMessage.delete();
} catch { } catch {
// no-op // no-op
} }
} else { } else {
const newTrack = await fetch(`http://${connection.node.host}:${connection.node.port}/decodetrack?track=${encodeURIComponent(newQueue[0])}`, { headers: { Authorization: connection.node.password } }).then(res => res.json()); const newTrack = await fetch(`http://${connection.node.host}:${connection.node.port}/decodetrack?track=${encodeURIComponent(newQueue[0])}`, { headers: { Authorization: connection.node.password } }).then(res => res.json());
nextSong(client, message, connection, newQueue[0], newTrack, music, voiceChannel, player.loop, true, track); nextSong(client, message, connection, newQueue[0], newTrack, music, voiceChannel, player.loop, track);
try { try {
if (newQueue[0] !== track && playingMessage.channel.messages.get(playingMessage.id)) await playingMessage.delete(); if (newQueue[0] !== track && playingMessage.channel.messages.get(playingMessage.id)) await playingMessage.delete();
if (newQueue[0] !== track && player.playMessage.channel.messages.get(player.playMessage.id)) await player.playMessage.delete(); if (newQueue[0] !== track && player.playMessage.channel.messages.get(player.playMessage.id)) await player.playMessage.delete();