From 382a7b0298b7a84d379d97c092ac2df56fdf04e6 Mon Sep 17 00:00:00 2001 From: Essem Date: Sat, 8 Jan 2022 15:54:34 -0600 Subject: [PATCH] Refine audio playback some more --- events/voiceChannelLeave.js | 30 +++++++++++++++++++++--------- utils/soundplayer.js | 25 +++++++++++++++++++++---- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/events/voiceChannelLeave.js b/events/voiceChannelLeave.js index c0065c2..a16b8dd 100644 --- a/events/voiceChannelLeave.js +++ b/events/voiceChannelLeave.js @@ -21,9 +21,13 @@ export default async (client, cluster, worker, ipc, member, oldChannel) => { } catch { // no-op } - await connection.player.stop(connection.originalChannel.guild.id); - await manager.leave(connection.originalChannel.guild.id); - await connection.player.destroy(); + try { + await connection.player.stop(connection.originalChannel.guild.id); + await manager.leave(connection.originalChannel.guild.id); + await connection.player.destroy(); + } catch { + // no-op + } players.delete(connection.originalChannel.guild.id); queues.delete(connection.originalChannel.guild.id); skipVotes.delete(connection.originalChannel.guild.id); @@ -48,9 +52,13 @@ export default async (client, cluster, worker, ipc, member, oldChannel) => { } catch { // no-op } - await connection.player.stop(connection.originalChannel.guild.id); - await manager.leave(connection.originalChannel.guild.id); - await connection.player.destroy(); + try { + await connection.player.stop(connection.originalChannel.guild.id); + await manager.leave(connection.originalChannel.guild.id); + await connection.player.destroy(); + } catch { + // no-op + } players.delete(connection.originalChannel.guild.id); queues.delete(connection.originalChannel.guild.id); skipVotes.delete(connection.originalChannel.guild.id); @@ -63,9 +71,13 @@ export default async (client, cluster, worker, ipc, member, oldChannel) => { } }); } else if (member.id === client.user.id) { - await connection.player.stop(connection.originalChannel.guild.id); - await manager.leave(connection.originalChannel.guild.id); - await connection.player.destroy(); + try { + await connection.player.stop(connection.originalChannel.guild.id); + await manager.leave(connection.originalChannel.guild.id); + await connection.player.destroy(); + } catch { + // no-op + } players.delete(connection.originalChannel.guild.id); queues.delete(connection.originalChannel.guild.id); skipVotes.delete(connection.originalChannel.guild.id); diff --git a/utils/soundplayer.js b/utils/soundplayer.js index 99572e5..dbda677 100644 --- a/utils/soundplayer.js +++ b/utils/soundplayer.js @@ -59,11 +59,23 @@ export async function play(client, sound, message, music = false) { if (!voiceChannel.permissionsOf(client.user.id).has("voiceConnect")) return "I don't have permission to join this voice channel!"; const player = players.get(message.channel.guild.id); if (!music && manager.voiceStates.has(message.channel.guild.id) && (player && player.type === "music")) return "I can't play a sound effect while playing music!"; - const node = manager.idealNodes[0]; + let node = manager.idealNodes[0]; + if (!node) { + const status = await checkStatus(); + if (!status) { + await connect(client); + node = manager.idealNodes[0]; + } + } if (!music && !nodes.filter(obj => obj.host === node.host)[0].local) { sound = sound.replace(/\.\//, "https://raw.githubusercontent.com/esmBot/esmBot/master/"); } - const { tracks, playlistInfo } = await Rest.load(node, sound); + let tracks, playlistInfo; + try { + ({ tracks, playlistInfo } = await Rest.load(node, sound)); + } catch { + return "🔊 Hmmm, seems that all of the audio servers are down. Try again in a bit."; + } const oldQueue = queues.get(voiceChannel.guild.id); if (!tracks || tracks.length === 0) return "I couldn't find that song!"; if (music) { @@ -144,12 +156,17 @@ export async function nextSong(client, message, connection, track, info, music, } catch { // no-op } - await manager.leave(voiceChannel.guild.id); + try { + await manager.leave(voiceChannel.guild.id); + await connection.destroy(); + } catch { + // no-op + } connection.removeAllListeners("end"); - await connection.destroy(); players.delete(voiceChannel.guild.id); queues.delete(voiceChannel.guild.id); logger.error(error); + await client.createMessage(message.channel.id, `🔊 Looks like there was an error regarding sound playback:\n\`\`\`${error.type}: ${error.error}\`\`\``); }); connection.on("end", async (data) => { if (data.reason === "REPLACED") return;