Skip to content

Instantly share code, notes, and snippets.

@cfanoulis
Created October 25, 2020 10:51
Show Gist options
  • Save cfanoulis/e80b0122897f50b80a56350b4a3547c9 to your computer and use it in GitHub Desktop.
Save cfanoulis/e80b0122897f50b80a56350b4a3547c9 to your computer and use it in GitHub Desktop.
(Bad) port of RichDisplay from Klasa
import { EventIterator, EventIteratorOptions } from '@sapphire/event-iterator';
import { mergeDefault } from '@sapphire/utilities';
import { wrapAroundNumber } from '@utils/util';
import { Message, MessageEmbed, MessageReaction, ReactionEmoji, User } from 'discord.js';
export interface PagedDisplayOptions extends Partial<EventIteratorOptions<[MessageReaction]>> {
noPageNumber?: boolean;
emojis: {
previousPage: string;
nextPage: string;
};
}
export class PagedDisplay {
private iterator: EventIterator<[MessageReaction]> | undefined;
private pages: (string | MessageEmbed)[];
private index = 0;
private options: PagedDisplayOptions;
private user: User;
public constructor(message: Message, template: (item: string) => string | MessageEmbed, items: string[], options?: PagedDisplayOptions) {
this.user = message.author;
this.options = mergeDefault(
{
idle: 50000,
limit: 15,
filter: this.iteratorFilter.bind(this),
emojis: {
previousPage: '◀️',
nextPage: '▶️'
}
},
options
);
this.pages = items.map(template).map(this.addPageFooter.bind(this));
}
public async run(message: Message) {
const pageMsg = await message.channel.send(this.pages[0]);
for (const emoji of Object.values(this.options.emojis)) {
await pageMsg.react(emoji);
}
message.client.once('messageReactionAdd', () => console.log('got a reaction'));
this.iterator = new EventIterator<[MessageReaction]>(message.client, 'messageReactionAdd', this.options);
for await (const [reaction] of this.iterator) {
switch (reaction.emoji.name) {
case this.options.emojis.nextPage:
this.index = wrapAroundNumber(this.index + 1, 0, this.pages.length - 1);
await pageMsg.edit(this.pages[this.index]);
await reaction.users.remove(this.user.id);
for (const emoji of Object.values(this.options.emojis)) {
await pageMsg.react(emoji);
}
break;
case this.options.emojis.previousPage:
this.index = wrapAroundNumber(this.index - 1, 0, this.pages.length - 1);
await pageMsg.edit(this.pages[this.index]);
await reaction.users.remove(this.user.id);
for (const emoji of Object.values(this.options.emojis)) {
await pageMsg.react(emoji);
}
break;
}
}
await pageMsg.reactions.removeAll();
}
private addPageFooter(page: string | MessageEmbed, pageIndex: number, pages: unknown[]) {
if (this.options.noPageNumber || typeof page === 'string') return page;
page.setFooter(`Page ${pageIndex + 1}/${pages.length}`);
return page;
}
private iteratorFilter([value]: [MessageReaction]) {
return Object.values(this.options.emojis).includes((value.emoji as ReactionEmoji).name) && value.users.cache.has(this.user.id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment