2021-08-12 23:45:17 +00:00
|
|
|
// eris doesn't come with a method to wait for interactions by default, so we make our own
|
2021-08-19 14:19:14 +00:00
|
|
|
import { EventEmitter } from "events";
|
2021-08-12 23:45:17 +00:00
|
|
|
|
|
|
|
class InteractionCollector extends EventEmitter {
|
|
|
|
constructor(client, message, options = {}) {
|
|
|
|
super();
|
|
|
|
this.message = message;
|
|
|
|
this.options = options;
|
|
|
|
this.ended = false;
|
|
|
|
this.bot = client;
|
|
|
|
this.listener = async (packet) => {
|
|
|
|
if (packet.t !== "INTERACTION_CREATE") return;
|
2021-08-14 21:15:21 +00:00
|
|
|
await this.verify(packet.d.message, packet.d.data.custom_id, packet.d.id, packet.d.token, packet.d.member ? packet.d.member.user.id : packet.d.user.id);
|
2021-08-12 23:45:17 +00:00
|
|
|
};
|
|
|
|
this.bot.on("rawWS", this.listener);
|
|
|
|
if (options.time) setTimeout(() => this.stop("time"), options.time);
|
|
|
|
}
|
|
|
|
|
|
|
|
async verify(message, interaction, id, token, member) {
|
|
|
|
if (this.message.id !== message.id) return false;
|
2021-08-14 21:15:21 +00:00
|
|
|
this.emit("interaction", interaction, id, token, member);
|
2021-08-12 23:45:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
stop(reason) {
|
|
|
|
if (this.ended) return;
|
|
|
|
this.ended = true;
|
|
|
|
this.bot.removeListener("rawWS", this.listener);
|
|
|
|
this.emit("end", this.collected, reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:19:14 +00:00
|
|
|
export default InteractionCollector;
|