2020-07-06 20:19:30 +00:00
|
|
|
// this is a method to wait for someone to rejoin a voice channel
|
2021-08-19 14:19:14 +00:00
|
|
|
import { EventEmitter } from "events";
|
2022-08-27 18:27:42 +00:00
|
|
|
import { random } from "./misc.js";
|
2020-07-06 20:19:30 +00:00
|
|
|
|
|
|
|
class AwaitRejoin extends EventEmitter {
|
|
|
|
constructor(channel, anyone, memberID) {
|
|
|
|
super();
|
|
|
|
this.member = memberID;
|
|
|
|
this.anyone = anyone;
|
|
|
|
this.channel = channel;
|
|
|
|
this.rejoined = false;
|
|
|
|
this.ended = false;
|
2022-08-11 15:40:10 +00:00
|
|
|
this.bot = channel.guild ? channel.guild.shard._client : channel._client;
|
2020-07-06 20:19:30 +00:00
|
|
|
this.listener = (member, newChannel) => this.verify(member, newChannel);
|
|
|
|
this.bot.on("voiceChannelJoin", this.listener);
|
|
|
|
this.bot.on("voiceChannelSwitch", this.listener);
|
2022-09-09 19:55:03 +00:00
|
|
|
this.stopTimeout = setTimeout(() => this.stop(), 10000);
|
2022-08-27 18:27:42 +00:00
|
|
|
this.checkInterval = setInterval(() => this.verify({ id: memberID }, channel, true), 1000);
|
2020-07-06 20:19:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 18:27:42 +00:00
|
|
|
verify(member, channel, checked) {
|
2020-07-06 20:19:30 +00:00
|
|
|
if (this.channel.id === channel.id) {
|
2022-08-27 18:27:42 +00:00
|
|
|
if ((this.member === member.id && this.channel.voiceMembers.has(member.id)) || (this.anyone && !checked)) {
|
2022-09-09 19:55:03 +00:00
|
|
|
clearTimeout(this.stopTimeout);
|
2020-07-06 20:19:30 +00:00
|
|
|
this.rejoined = true;
|
|
|
|
this.stop(member);
|
|
|
|
return true;
|
2022-08-27 18:27:42 +00:00
|
|
|
} else if (this.anyone && (!checked || this.channel.voiceMembers.size > 1)) {
|
2022-09-09 19:55:03 +00:00
|
|
|
clearTimeout(this.stopTimeout);
|
2022-08-27 18:27:42 +00:00
|
|
|
this.rejoined = true;
|
|
|
|
this.stop(random(this.channel.voiceMembers.filter((i) => i.id !== this.bot.user.id && !i.bot)));
|
|
|
|
return true;
|
2020-07-06 20:19:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop(member) {
|
|
|
|
if (this.ended) return;
|
|
|
|
this.ended = true;
|
2022-08-27 18:27:42 +00:00
|
|
|
clearInterval(this.checkInterval);
|
2020-07-06 20:19:30 +00:00
|
|
|
this.bot.removeListener("voiceChannelJoin", this.listener);
|
|
|
|
this.bot.removeListener("voiceChannelSwitch", this.listener);
|
|
|
|
this.emit("end", this.rejoined, member);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:19:14 +00:00
|
|
|
export default AwaitRejoin;
|