2022-06-29 01:36:32 +00:00
|
|
|
import { players, queues, skipVotes } from "../utils/soundplayer.js";
|
2021-08-19 14:19:14 +00:00
|
|
|
import AwaitRejoin from "../utils/awaitrejoin.js";
|
|
|
|
import { random } from "../utils/misc.js";
|
2020-07-06 20:19:30 +00:00
|
|
|
|
2022-08-27 18:27:42 +00:00
|
|
|
const isWaiting = new Map();
|
|
|
|
|
2021-08-19 14:19:14 +00:00
|
|
|
export default async (client, cluster, worker, ipc, member, oldChannel) => {
|
2021-07-09 02:03:19 +00:00
|
|
|
if (!oldChannel) return;
|
2021-08-19 14:19:14 +00:00
|
|
|
const connection = players.get(oldChannel.guild.id);
|
2022-07-23 21:02:04 +00:00
|
|
|
if (oldChannel.id === connection?.voiceChannel.id) {
|
2021-10-08 04:58:24 +00:00
|
|
|
if (oldChannel.voiceMembers.filter((i) => i.id !== client.user.id && !i.bot).length === 0) {
|
2022-08-27 18:27:42 +00:00
|
|
|
if (isWaiting.has(oldChannel.id)) return;
|
|
|
|
isWaiting.set(oldChannel.id, true);
|
2022-06-29 01:36:32 +00:00
|
|
|
connection.player.setPaused(true);
|
2020-07-06 20:19:30 +00:00
|
|
|
const waitMessage = await client.createMessage(connection.originalChannel.id, "🔊 Waiting 10 seconds for someone to return...");
|
2022-08-27 18:27:42 +00:00
|
|
|
const awaitRejoin = new AwaitRejoin(oldChannel, true, member.id);
|
|
|
|
awaitRejoin.on("end", async (rejoined, newMember) => {
|
|
|
|
isWaiting.delete(oldChannel.id);
|
2020-07-06 20:19:30 +00:00
|
|
|
if (rejoined) {
|
2022-06-29 01:36:32 +00:00
|
|
|
connection.player.setPaused(false);
|
2022-08-27 18:27:42 +00:00
|
|
|
if (member.id !== newMember.id) {
|
|
|
|
players.set(connection.voiceChannel.guild.id, { player: connection.player, type: connection.type, host: newMember.id, voiceChannel: connection.voiceChannel, originalChannel: connection.originalChannel, loop: connection.loop, shuffle: connection.shuffle, playMessage: connection.playMessage });
|
|
|
|
waitMessage.edit(`🔊 ${newMember.mention} is the new voice channel host.`);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
await waitMessage.delete();
|
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
}
|
2020-07-06 20:19:30 +00:00
|
|
|
} else {
|
2021-09-22 00:15:19 +00:00
|
|
|
try {
|
2022-03-17 19:28:35 +00:00
|
|
|
if (waitMessage.channel.messages.has(waitMessage.id)) await waitMessage.delete();
|
2021-09-22 00:15:19 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2022-01-08 21:54:34 +00:00
|
|
|
try {
|
2022-06-29 01:36:32 +00:00
|
|
|
connection.player.node.leaveChannel(connection.originalChannel.guild.id);
|
2022-01-08 21:54:34 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2021-08-19 14:19:14 +00:00
|
|
|
players.delete(connection.originalChannel.guild.id);
|
|
|
|
queues.delete(connection.originalChannel.guild.id);
|
2021-09-19 22:49:02 +00:00
|
|
|
skipVotes.delete(connection.originalChannel.guild.id);
|
2022-09-09 19:55:03 +00:00
|
|
|
client.createMessage(connection.originalChannel.id, `🔊 The voice channel session in \`${connection.originalChannel.name}\` has ended.`);
|
2020-07-06 20:19:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (member.id === connection.host) {
|
2022-08-27 18:27:42 +00:00
|
|
|
if (isWaiting.has(oldChannel.id)) return;
|
|
|
|
isWaiting.set(oldChannel.id, true);
|
2020-07-06 20:19:30 +00:00
|
|
|
const waitMessage = await client.createMessage(connection.originalChannel.id, "🔊 Waiting 10 seconds for the host to return...");
|
|
|
|
const awaitRejoin = new AwaitRejoin(oldChannel, false, member.id);
|
2021-12-13 21:46:08 +00:00
|
|
|
awaitRejoin.on("end", async (rejoined) => {
|
2022-08-27 18:27:42 +00:00
|
|
|
isWaiting.delete(oldChannel.id);
|
2020-07-06 20:19:30 +00:00
|
|
|
if (rejoined) {
|
2021-09-22 00:15:19 +00:00
|
|
|
try {
|
2022-03-17 19:28:35 +00:00
|
|
|
if (waitMessage.channel.messages.has(waitMessage.id)) await waitMessage.delete();
|
2021-09-22 00:15:19 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2020-07-06 20:19:30 +00:00
|
|
|
} else {
|
2021-10-08 04:58:24 +00:00
|
|
|
const members = oldChannel.voiceMembers.filter((i) => i.id !== client.user.id && !i.bot);
|
2020-07-06 20:19:30 +00:00
|
|
|
if (members.length === 0) {
|
2021-09-22 00:15:19 +00:00
|
|
|
try {
|
2022-03-17 19:28:35 +00:00
|
|
|
if (waitMessage.channel.messages.has(waitMessage.id)) await waitMessage.delete();
|
2021-09-22 00:15:19 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2022-01-08 21:54:34 +00:00
|
|
|
try {
|
2022-06-29 01:36:32 +00:00
|
|
|
connection.player.node.leaveChannel(connection.originalChannel.guild.id);
|
2022-01-08 21:54:34 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2021-08-19 14:19:14 +00:00
|
|
|
players.delete(connection.originalChannel.guild.id);
|
|
|
|
queues.delete(connection.originalChannel.guild.id);
|
2021-09-19 22:49:02 +00:00
|
|
|
skipVotes.delete(connection.originalChannel.guild.id);
|
2022-09-09 19:55:03 +00:00
|
|
|
client.createMessage(connection.originalChannel.id, `🔊 The voice channel session in \`${connection.originalChannel.name}\` has ended.`);
|
2020-07-06 20:19:30 +00:00
|
|
|
} else {
|
2020-08-16 16:48:37 +00:00
|
|
|
const randomMember = random(members);
|
2021-09-20 17:26:40 +00:00
|
|
|
players.set(connection.voiceChannel.guild.id, { player: connection.player, type: connection.type, host: randomMember.id, voiceChannel: connection.voiceChannel, originalChannel: connection.originalChannel, loop: connection.loop, shuffle: connection.shuffle, playMessage: connection.playMessage });
|
2020-07-06 20:19:30 +00:00
|
|
|
waitMessage.edit(`🔊 ${randomMember.mention} is the new voice channel host.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-09-02 02:52:12 +00:00
|
|
|
} else if (member.id === client.user.id) {
|
2022-08-27 18:27:42 +00:00
|
|
|
isWaiting.delete(oldChannel.id);
|
2022-01-08 21:54:34 +00:00
|
|
|
try {
|
2022-06-29 01:36:32 +00:00
|
|
|
connection.player.node.leaveChannel(connection.originalChannel.guild.id);
|
2022-01-08 21:54:34 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2021-08-19 14:19:14 +00:00
|
|
|
players.delete(connection.originalChannel.guild.id);
|
|
|
|
queues.delete(connection.originalChannel.guild.id);
|
2021-09-19 22:49:02 +00:00
|
|
|
skipVotes.delete(connection.originalChannel.guild.id);
|
2022-09-09 19:55:03 +00:00
|
|
|
await client.createMessage(connection.originalChannel.id, `🔊 The voice channel session in \`${connection.originalChannel.name}\` has ended.`);
|
2020-07-06 20:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-12 05:44:49 +00:00
|
|
|
};
|