mrmBot-Matrix/utils/pagination/awaitinteractions.js

34 lines
1.1 KiB
JavaScript
Raw Normal View History

// eris doesn't come with a method to wait for interactions by default, so we make our own
import { EventEmitter } from "events";
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);
};
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);
return true;
}
stop(reason) {
if (this.ended) return;
this.ended = true;
this.bot.removeListener("rawWS", this.listener);
this.emit("end", this.collected, reason);
}
}
export default InteractionCollector;