Pause music when everyone leaves the voice channel, try a slightly different approach to voice events

This commit is contained in:
Essem 2021-09-13 17:24:15 -05:00
parent 95dc02aed5
commit c59a0bf0c4
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
5 changed files with 366 additions and 363 deletions

3
.gitignore vendored
View file

@ -37,4 +37,5 @@ bannedusers.json
todo.txt
.vscode/
migratedb.js
processed.txt
processed.txt
data.sqlite

View file

@ -7,10 +7,12 @@ export default async (client, cluster, worker, ipc, member, oldChannel) => {
const connection = players.get(oldChannel.guild.id);
if (connection && connection.type === "music" && oldChannel.id === connection.voiceChannel.id) {
if (oldChannel.voiceMembers.filter((i) => i.id !== client.user.id).length === 0) {
connection.player.pause(true);
const waitMessage = await client.createMessage(connection.originalChannel.id, "🔊 Waiting 10 seconds for someone to return...");
const awaitRejoin = new AwaitRejoin(oldChannel, true);
awaitRejoin.on("end", (rejoined, member) => {
if (rejoined) {
connection.player.pause(false);
players.set(connection.voiceChannel.guild.id, { player: connection.player, type: connection.type, host: member.id, voiceChannel: connection.voiceChannel, originalChannel: connection.originalChannel });
waitMessage.edit(`🔊 ${member.mention} is the new voice channel host.`);
} else {

637
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -29,7 +29,7 @@
"cowsay2": "^2.0.4",
"dotenv": "^9.0.2",
"emoji-regex": "^9.2.2",
"eris": "github:abalabahaha/eris#dev",
"eris": "^0.15.1",
"eris-fleet": "github:esmBot/eris-fleet",
"file-type": "^16.1.0",
"format-duration": "^1.4.0",

View file

@ -125,55 +125,54 @@ export async function nextSong(client, message, connection, track, info, music,
}
});
}
connection.removeAllListeners("error");
connection.removeAllListeners("end");
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 });
if (connection.listeners("error").length === 0) {
connection.on("error", (error) => {
if (playingMessage.channel.messages.get(playingMessage.id)) playingMessage.delete();
const playMessage = players.get(voiceChannel.guild.id).playMessage;
if (playMessage.channel.messages.get(playMessage.id)) playMessage.delete();
connection.once("error", (error) => {
if (playingMessage.channel.messages.get(playingMessage.id)) playingMessage.delete();
const playMessage = players.get(voiceChannel.guild.id).playMessage;
if (playMessage.channel.messages.get(playMessage.id)) playMessage.delete();
manager.leave(voiceChannel.guild.id);
connection.removeAllListeners("end");
connection.destroy();
players.delete(voiceChannel.guild.id);
queues.delete(voiceChannel.guild.id);
logger.error(error);
});
connection.on("end", async (data) => {
if (data.reason === "REPLACED") return;
const queue = queues.get(voiceChannel.guild.id);
const player = players.get(voiceChannel.guild.id);
let newQueue;
if (player && player.loop) {
queue.push(queue.shift());
newQueue = queue;
} else {
newQueue = queue ? queue.slice(1) : [];
}
queues.set(voiceChannel.guild.id, newQueue);
if (newQueue.length === 0) {
manager.leave(voiceChannel.guild.id);
connection.destroy();
players.delete(voiceChannel.guild.id);
queues.delete(voiceChannel.guild.id);
logger.error(error);
});
}
if (connection.listeners("end").length === 0) {
connection.on("end", async (data) => {
if (data.reason === "REPLACED") return;
const queue = queues.get(voiceChannel.guild.id);
const player = players.get(voiceChannel.guild.id);
let newQueue;
if (player && player.loop) {
queue.push(queue.shift());
newQueue = queue;
} else {
newQueue = queue ? queue.slice(1) : [];
if (music) await client.createMessage(message.channel.id, "🔊 The current voice channel session has ended.");
try {
if (playingMessage.channel.messages.get(playingMessage.id)) await playingMessage.delete();
if (player && player.playMessage.channel.messages.get(player.playMessage.id)) await player.playMessage.delete();
} catch {
// no-op
}
queues.set(voiceChannel.guild.id, newQueue);
if (newQueue.length === 0) {
manager.leave(voiceChannel.guild.id);
connection.destroy();
players.delete(voiceChannel.guild.id);
queues.delete(voiceChannel.guild.id);
if (music) await client.createMessage(message.channel.id, "🔊 The current voice channel session has ended.");
try {
if (playingMessage.channel.messages.get(playingMessage.id)) await playingMessage.delete();
if (player && player.playMessage.channel.messages.get(player.playMessage.id)) await player.playMessage.delete();
} catch {
// no-op
}
} 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());
nextSong(client, message, connection, newQueue[0], newTrack, music, voiceChannel, player.loop, track);
try {
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();
} catch {
// no-op
}
} 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());
nextSong(client, message, connection, newQueue[0], newTrack, music, voiceChannel, player.loop, track);
try {
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();
} catch {
// no-op
}
});
}
}
});
}