Skip to content

Instantly share code, notes, and snippets.

@DusterTheFirst
Last active August 12, 2019 18:35
Show Gist options
  • Save DusterTheFirst/fecdfe04afd85c9374b89a89e02d20a0 to your computer and use it in GitHub Desktop.
Save DusterTheFirst/fecdfe04afd85c9374b89a89e02d20a0 to your computer and use it in GitHub Desktop.
Discord.js Paginator (MIT licensed)
import { User, TextChannel, Message, ReactionCollector } from "discord.js";
class Paginator {
/**
* Current page
*/
current: number;
/**
* Total pages
*/
total: number;
pages: string[];
first: "⏮";
back: "◀";
stop: "⏹";
next: "▶";
last: "⏭";
message: Message;
collector: ReactionCollector;
constructor(channel: TextChannel, dad: User, pages: string[]) {
this.current = 0;
this.total = pages.length;
this.pages = pages;
this.first = "⏮";
this.back = "◀";
this.stop = "⏹";
this.next = "▶";
this.last = "⏭";
channel.send(pages[0]).then(async (msg) => {
/**
* Message sent
* @type {Message}
*/
this.message = msg as Message;
await this.message.react(this.first);
await this.message.react(this.back);
await this.message.react(this.stop);
await this.message.react(this.next);
await this.message.react(this.last);
this.collector = this.message.createReactionCollector((reaction, user) => reaction.me && user.id === dad.id && user.id !== this.message.author.id, {time: 100000});
this.collector.on("collect", (reaction, collector) => {
reaction.remove(dad);
switch (reaction.emoji.toString()) {
case this.first:
this.current = 0;
break;
case this.last:
this.current = this.total - 1;
break;
case this.stop:
this.collector.stop();
this.message.clearReactions();
break;
case this.back:
this.current--;
if (this.current < 0)
this.current = this.total - 1;
break;
case this.next:
this.current++;
if (this.current > this.total - 1)
this.current = 0;
break;
}
this.refresh();
});
});
}
refresh() {
this.message.edit(this.pages[this.current]);
}
}
@brol1dev
Copy link

hey, can i use this snippet in my open source project? Found it simple and easy to use :) Asking because is a gist and it doesn't contain a explicit License :)

@DusterTheFirst
Copy link
Author

@rcvrgs indeed, I’ll license it as MIT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment