diff --git a/src/commands/ReactionMenu.js b/src/commands/ReactionMenu.js index 6e95cd06..8b01fd7b 100644 --- a/src/commands/ReactionMenu.js +++ b/src/commands/ReactionMenu.js @@ -1,3 +1,5 @@ +const { MessageEmbed } = require('discord.js'); + /** * Calypso's Reaction Menu class */ @@ -8,10 +10,18 @@ module.exports = class ReactionMenu { * @param {TextChannel} channel * @param {GuildMember} member * @param {MessageEmbed} embed + * @param {Array} arr + * @param {int} interval * @param {Object} reactions * @param {int} timeout */ - constructor(channel, member, embed, reactions, timeout = 120000) { + constructor(channel, member, embed, arr = null, interval = 10, reactions = { + '⏪': this.first.bind(this), + '◀️': this.previous.bind(this), + '▶️': this.next.bind(this), + '⏩': this.last.bind(this), + '⏹️': this.stop.bind(this) + }, timeout = 120000) { /** * The text channel @@ -26,11 +36,41 @@ module.exports = class ReactionMenu { this.memberId = member.id; /** - * The message embed + * The embed passed to the Reaction Menu * @type {MessageEmbed} */ this.embed = embed; + /** + * JSON from the embed + * @type {Object} + */ + this.json = this.embed.toJSON(); + + /** + * The array to be iterated over + * @type {Array} + */ + this.arr = arr; + + /** + * The size of each array window + * @type {int} + */ + this.interval = interval; + + /** + * The current array window start + * @type {int} + */ + this.current = 0; + + /** + * The max length of the array + * @type {int} + */ + this.max = arr.length; + /** * The reactions for menu * @type {Object} @@ -49,7 +89,13 @@ module.exports = class ReactionMenu { */ this.timeout = timeout; - this.channel.send(this.embed).then(message => { + const first = new MessageEmbed(this.json); + const description = (this.arr) ? this.arr.slice(this.current, this.interval) : null; + if (description) first + .setTitle(this.embed.title + ` [1 - ${this.interval}]`) + .setDescription(description); + + this.channel.send(first).then(message => { this.message = message; this.addReactions(); this.createCollector(); @@ -75,12 +121,12 @@ module.exports = class ReactionMenu { return (this.emojis.includes(reaction.emoji.name) || this.emojis.includes(reaction.emoji.id)) && user.id == this.memberId; }, { time: this.timeout }); - + // On collect collector.on('collect', async reaction => { let newPage = this.reactions[reaction.emoji.name] || this.reactions[reaction.emoji.id]; if (typeof newPage === 'function') newPage = newPage(); - await this.message.edit(newPage); + if (newPage) await this.message.edit(newPage); await reaction.users.remove(this.memberId); }); @@ -88,5 +134,63 @@ module.exports = class ReactionMenu { collector.on('end', () => { this.message.reactions.removeAll(); }); + + this.collector = collector; + } + + /** + * Skips to the first array interval + */ + first() { + if (this.current === 0) return; + this.current = 0; + return new MessageEmbed(this.json) + .setTitle(this.embed.title + ` [${this.current + 1} - ${this.current + this.interval}]`) + .setDescription(this.arr.slice(this.current, this.current + this.interval)); + } + + /** + * Goes back an array interval + */ + previous() { + if (this.current === 0) return; + this.current -= this.interval; + if (this.current < 0) this.current = 0; + return new MessageEmbed(this.json) + .setTitle(this.embed.title + ` [${this.current + 1} - ${this.current + this.interval}]`) + .setDescription(this.arr.slice(this.current, this.current + this.interval)); + } + + /** + * Goes to the next array interval + */ + next() { + const cap = this.max - (this.max % this.interval); + if (this.current === cap) return; + this.current += this.interval; + let max = this.current + this.interval; + if (max >= this.max) max = this.max; + return new MessageEmbed(this.json) + .setTitle(this.embed.title + ` [${this.current + 1} - ${max}]`) + .setDescription(this.arr.slice(this.current, max)); + } + + /** + * Goes to the last array interval + */ + last() { + const cap = this.max - (this.max % this.interval); + if (this.current === cap) return; + this.current = cap; + return new MessageEmbed(this.json) + .setTitle(this.embed.title + ` [${this.current + 1} - ${this.max}]`) + .setDescription(this.arr.slice(this.current, this.max)); + } + + /** + * Stops the collector + */ + stop() { + this.collector.stop(); } }; \ No newline at end of file diff --git a/src/commands/admin/settings.js b/src/commands/admin/settings.js index 3ff11962..507a8949 100644 --- a/src/commands/admin/settings.js +++ b/src/commands/admin/settings.js @@ -55,7 +55,7 @@ module.exports = class SettingsCommand extends Command { const welcomeStatus = `\`${message.client.utils.getStatus(row.welcome_message && row.welcome_channel_id)}\``; const leaveStatus = `\`${message.client.utils.getStatus(row.leave_message && row.leave_channel_id)}\``; const pointsStatus = `\`${message.client.utils.getStatus(row.point_tracking)}\``; - const crownStatus = `\`${message.client.utils.getStatus(row.crown_role && row.crown_schedule)}\``; + const crownStatus = `\`${message.client.utils.getStatus(row.crown_role_id && row.crown_schedule)}\``; /** ------------------------------------------------------------------------------------------------ * CATEGORY CHECKS diff --git a/src/commands/points/leaderboard.js b/src/commands/points/leaderboard.js index ed51f080..c103709a 100644 --- a/src/commands/points/leaderboard.js +++ b/src/commands/points/leaderboard.js @@ -55,65 +55,16 @@ module.exports = class LeaderboardCommand extends Command { // Reaction Menu } else { - let n = 0, interval = max; embed - .setTitle(`Points Leaderboard [1 - ${max}]`) + .setTitle('Points Leaderboard') .setThumbnail(message.guild.iconURL({ dynamic: true })) .setFooter( 'Expires after two minutes.\n' + `${message.member.displayName}'s position: ${position + 1}`, message.author.displayAvatarURL({ dynamic: true }) - ) - .setDescription(members.slice(n, max).join('\n')); - - const json = embed.toJSON(); + ); - const first = () => { - if (n === 0) return; - n = 0; - max = interval; - return new MessageEmbed(json) - .setTitle(`Points Leaderboard [${n + 1} - ${max}]`) - .setDescription(members.slice(n, max).join('\n')); - }; - - const previous = () => { - if (n === 0) return; - n -= interval; - max -= interval; - if (max <= n + interval) max = n + interval; - return new MessageEmbed(json) - .setTitle(`Points Leaderboard [${n + 1} - ${max}]`) - .setDescription(members.slice(n, max).join('\n')); - }; - - const next = () => { - if (max === members.length) return; - n += interval; - max += interval; - if (max >= members.length) max = members.length; - return new MessageEmbed(json) - .setTitle(`Points Leaderboard [${n + 1} - ${max}]`) - .setDescription(members.slice(n, max).join('\n')); - }; + new ReactionMenu(message.channel, message.member, embed, members, max); - const last = () => { - if (max === members.length) return; - n = members.length - (members.length % interval); - max = members.length; - return new MessageEmbed(json) - .setTitle(`Points Leaderboard [${n + 1} - ${max}]`) - .setDescription(members.slice(n, max).join('\n')); - }; - - const reactions = { - '⏪': first, - '◀️': previous, - '▶️': next, - '⏩': last - }; - - new ReactionMenu(message.channel, message.member, embed, reactions); - } - + } } };