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();
|
|
|
|
|
2022-09-21 05:05:03 +00:00
|
|
|
export default async (client, member, oldChannel) => {
|
2021-07-09 02:03:19 +00:00
|
|
|
if (!oldChannel) return;
|
2022-09-24 03:25:16 +00:00
|
|
|
const connection = players.get(oldChannel.guildID);
|
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);
|
2022-09-24 03:25:16 +00:00
|
|
|
const waitMessage = await client.rest.channels.createMessage(connection.originalChannel.id, {
|
|
|
|
content: "🔊 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) {
|
2022-09-24 03:25:16 +00:00
|
|
|
players.set(connection.voiceChannel.guildID, { 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({
|
|
|
|
content: `🔊 ${newMember.mention} is the new voice channel host.`
|
|
|
|
});
|
2022-08-27 18:27:42 +00:00
|
|
|
} 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-09-24 03:25:16 +00:00
|
|
|
connection.player.node.leaveChannel(connection.originalChannel.guildID);
|
2022-01-08 21:54:34 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2022-09-24 03:25:16 +00:00
|
|
|
players.delete(connection.originalChannel.guildID);
|
|
|
|
queues.delete(connection.originalChannel.guildID);
|
|
|
|
skipVotes.delete(connection.originalChannel.guildID);
|
|
|
|
client.rest.channels.createMessage(connection.originalChannel.id, {
|
2022-09-24 04:50:59 +00:00
|
|
|
content: `🔊 The voice channel session in \`${connection.voiceChannel.name}\` has ended.`
|
2022-09-24 03:25:16 +00:00
|
|
|
});
|
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);
|
2022-09-24 03:25:16 +00:00
|
|
|
const waitMessage = await client.rest.channels.createMessage(connection.originalChannel.id, {
|
|
|
|
content: "🔊 Waiting 10 seconds for the host to return..."
|
|
|
|
});
|
2020-07-06 20:19:30 +00:00
|
|
|
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-09-24 03:25:16 +00:00
|
|
|
connection.player.node.leaveChannel(connection.originalChannel.guildID);
|
2022-01-08 21:54:34 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2022-09-24 03:25:16 +00:00
|
|
|
players.delete(connection.originalChannel.guildID);
|
|
|
|
queues.delete(connection.originalChannel.guildID);
|
|
|
|
skipVotes.delete(connection.originalChannel.guildID);
|
|
|
|
client.rest.channels.createMessage(connection.originalChannel.id, {
|
2022-09-24 04:50:59 +00:00
|
|
|
content: `🔊 The voice channel session in \`${connection.voiceChannel.name}\` has ended.`
|
2022-09-24 03:25:16 +00:00
|
|
|
});
|
2020-07-06 20:19:30 +00:00
|
|
|
} else {
|
2020-08-16 16:48:37 +00:00
|
|
|
const randomMember = random(members);
|
2022-09-24 03:25:16 +00:00
|
|
|
players.set(connection.voiceChannel.guildID, { player: connection.player, type: connection.type, host: randomMember.id, voiceChannel: connection.voiceChannel, originalChannel: connection.originalChannel, loop: connection.loop, shuffle: connection.shuffle, playMessage: connection.playMessage });
|
|
|
|
waitMessage.edit({
|
|
|
|
content: `🔊 ${randomMember.mention} is the new voice channel host.`
|
|
|
|
});
|
2020-07-06 20:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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-09-24 03:25:16 +00:00
|
|
|
connection.player.node.leaveChannel(connection.originalChannel.guildID);
|
2022-01-08 21:54:34 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2022-09-24 03:25:16 +00:00
|
|
|
players.delete(connection.originalChannel.guildID);
|
|
|
|
queues.delete(connection.originalChannel.guildID);
|
|
|
|
skipVotes.delete(connection.originalChannel.guildID);
|
|
|
|
await client.rest.channels.createMessage(connection.originalChannel.id, {
|
2022-09-24 04:50:59 +00:00
|
|
|
content: `🔊 The voice channel session in \`${connection.voiceChannel.name}\` has ended.`
|
2022-09-24 03:25:16 +00:00
|
|
|
});
|
2020-07-06 20:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-12 05:44:49 +00:00
|
|
|
};
|