Skip to content

Commit

Permalink
updated reaction menu to take an array, updated leaderboard command, …
Browse files Browse the repository at this point in the history
…fixed typo in settings
  • Loading branch information
sabattle committed Aug 22, 2020
1 parent 71e5db5 commit 0998ff5
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 59 deletions.
114 changes: 109 additions & 5 deletions src/commands/ReactionMenu.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { MessageEmbed } = require('discord.js');

/**
* Calypso's Reaction Menu class
*/
Expand All @@ -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
Expand All @@ -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}
Expand All @@ -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();
Expand All @@ -75,18 +121,76 @@ 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);
});

// On end
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();
}
};
2 changes: 1 addition & 1 deletion src/commands/admin/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 4 additions & 53 deletions src/commands/points/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
}
};

0 comments on commit 0998ff5

Please sign in to comment.