2019-09-13 20:02:41 +00:00
|
|
|
// eris doesn't come with an awaitReactions method by default, so we make our own
|
|
|
|
const EventEmitter = require("events").EventEmitter;
|
2020-09-01 22:10:19 +00:00
|
|
|
const client = require("../client.js");
|
|
|
|
|
2019-09-13 20:02:41 +00:00
|
|
|
class ReactionCollector extends EventEmitter {
|
|
|
|
constructor(message, filter, options = {}) {
|
|
|
|
super();
|
|
|
|
this.filter = filter;
|
|
|
|
this.message = message;
|
|
|
|
this.options = options;
|
|
|
|
this.ended = false;
|
|
|
|
this.collected = [];
|
2020-09-01 22:10:19 +00:00
|
|
|
this.bot = client;
|
2020-10-06 15:18:33 +00:00
|
|
|
this.listener = async (message, emoji, userID) => await this.verify(message, emoji, userID);
|
2019-09-13 20:02:41 +00:00
|
|
|
this.bot.on("messageReactionAdd", this.listener);
|
|
|
|
if (options.time) setTimeout(() => this.stop("time"), options.time);
|
|
|
|
}
|
|
|
|
|
2020-10-06 15:18:33 +00:00
|
|
|
async verify(message, emoji, userID) {
|
2019-09-13 20:02:41 +00:00
|
|
|
if (this.message.id !== message.id) return false;
|
|
|
|
if (this.filter(message, emoji, userID)) {
|
|
|
|
this.collected.push({ message: message, emoji: emoji, userID: userID });
|
2020-10-06 15:18:33 +00:00
|
|
|
this.emit("reaction", await client.getMessage(message.channel.id, message.id), emoji, userID);
|
2019-09-13 20:02:41 +00:00
|
|
|
if (this.collected.length >= this.options.maxMatches) this.stop("maxMatches");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stop(reason) {
|
|
|
|
if (this.ended) return;
|
|
|
|
this.ended = true;
|
|
|
|
this.bot.removeListener("messageReactionAdd", this.listener);
|
|
|
|
this.emit("end", this.collected, reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ReactionCollector;
|