discord.js-buttons
Version:
HunterRock Discord.js Buttons
84 lines (67 loc) • 2.43 kB
JavaScript
const { Collector } = require("discord.js");
const Collection = require('discord.js').Collection;
const { Events } = require('discord.js').Constants;
class ButtonCollector extends Collector {
constructor(message, filter, options = {}) {
super(message.client, filter, options);
this.message = message;
this.users = new Collection();
this.total = 0;
this.empty = this.empty.bind(this);
this._handleChannelDeletion = this._handleChannelDeletion.bind(this);
this._handleGuildDeletion = this._handleGuildDeletion.bind(this);
this._handleMessageDeletion = this._handleMessageDeletion.bind(this);
this.client.incrementMaxListeners();
this.client.on('clickButton', this.handleCollect);
this.client.on(Events.MESSAGE_DELETE, this._handleMessageDeletion);
this.client.on(Events.CHANNEL_DELETE, this._handleChannelDeletion);
this.client.on(Events.GUILD_DELETE, this._handleGuildDeletion);
this.once('end', () => {
this.client.removeListener('clickButton', this.handleCollect);
this.client.removeListener(Events.MESSAGE_DELETE, this._handleMessageDeletion);
this.client.removeListener(Events.CHANNEL_DELETE, this._handleChannelDeletion);
this.client.removeListener(Events.GUILD_DELETE, this._handleGuildDeletion);
this.client.decrementMaxListeners();
});
this.on('collect', (button) => {
this.total++;
this.users.set(button.clicker.user.id, button.clicker.user);
});
}
collect(button) {
if (button.message.id !== this.message.id) return null;
return ButtonCollector.key(button);
}
dispose() {
return null;
}
empty() {
this.total = 0;
this.collected.clear();
this.users.clear();
this.checkEnd();
}
endReason() {
if (this.options.max && this.total >= this.options.max) return 'limit';
return null;
}
_handleMessageDeletion(message) {
if (message.id === this.message.id) {
this.stop('messageDelete');
}
}
_handleChannelDeletion(channel) {
if (channel.id === this.message.channel.id) {
this.stop('channelDelete');
}
}
_handleGuildDeletion(guild) {
if (this.message.guild && guild.id === this.message.guild.id) {
this.stop('guildDelete');
}
}
static key(button) {
return button.id;
}
}
module.exports = ButtonCollector;