|
|
@ -1,4 +1,4 @@ |
|
|
|
import {TextChannel, Message, MessageEmbed, Client} from 'discord.js'; |
|
|
|
import {TextChannel, Message, MessageEmbed, Client, MessageReaction, ReactionCollector} from 'discord.js'; |
|
|
|
|
|
|
|
|
|
|
|
export class Pagination { |
|
|
|
export class Pagination { |
|
|
|
channel: TextChannel; |
|
|
|
channel: TextChannel; |
|
|
@ -7,10 +7,12 @@ export class Pagination { |
|
|
|
originalMessage: Message; |
|
|
|
originalMessage: Message; |
|
|
|
currentPage: number; |
|
|
|
currentPage: number; |
|
|
|
client: Client; |
|
|
|
client: Client; |
|
|
|
|
|
|
|
loopPages: boolean = true; |
|
|
|
|
|
|
|
controllers: ControllerData = {enabled: false, endTime: null, collector: null, lastInteraction: null}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor (channel: TextChannel, pages: MessageEmbed[], originalMessage: Message, client: Client, message?: Message) { |
|
|
|
constructor (channel: TextChannel, pages: MessageEmbed[], originalMessage: Message, client: Client, loopPages?: boolean, message?: Message) { |
|
|
|
this.channel = channel; |
|
|
|
this.channel = channel; |
|
|
|
this.pages = pages; |
|
|
|
this.pages = pages; |
|
|
|
this.originalMessage = message; |
|
|
|
this.originalMessage = message; |
|
|
@ -18,6 +20,7 @@ export class Pagination { |
|
|
|
this.currentPage = 0; |
|
|
|
this.currentPage = 0; |
|
|
|
|
|
|
|
|
|
|
|
if (message) {this.message = message;} |
|
|
|
if (message) {this.message = message;} |
|
|
|
|
|
|
|
if (loopPages) {this.loopPages = loopPages;} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -32,7 +35,7 @@ export class Pagination { |
|
|
|
else {this.message = tempm;} |
|
|
|
else {this.message = tempm;} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await this.message.edit(this.pages[page] |
|
|
|
await this.message.edit('', this.pages[page] |
|
|
|
.setFooter(`Natsuki | Page ${page + 1} of ${this.pages.length}`, this.client.user.avatarURL()) |
|
|
|
.setFooter(`Natsuki | Page ${page + 1} of ${this.pages.length}`, this.client.user.avatarURL()) |
|
|
|
.setTimestamp() |
|
|
|
.setTimestamp() |
|
|
|
); |
|
|
|
); |
|
|
@ -42,19 +45,33 @@ export class Pagination { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
public async nextPage(): Promise<Pagination> { |
|
|
|
public async nextPage(): Promise<Pagination> { |
|
|
|
await this.setPage(typeof this.currentPage === "number" ? this.currentPage + 1 == this.pages.length ? this.currentPage : this.currentPage + 1 : 0); |
|
|
|
await this.setPage(typeof this.currentPage === "number" |
|
|
|
|
|
|
|
? this.currentPage + 1 == this.pages.length |
|
|
|
|
|
|
|
? this.loopPages |
|
|
|
|
|
|
|
? 0 |
|
|
|
|
|
|
|
: this.currentPage |
|
|
|
|
|
|
|
: this.currentPage + 1 |
|
|
|
|
|
|
|
: 0 |
|
|
|
|
|
|
|
); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
public async prevPage(): Promise<Pagination> { |
|
|
|
public async prevPage(): Promise<Pagination> { |
|
|
|
await this.setPage(typeof this.currentPage === "number" ? this.currentPage === 0 ? 0 : this.currentPage - 1 : this.pages.length - 1); |
|
|
|
await this.setPage(typeof this.currentPage === "number" |
|
|
|
|
|
|
|
? this.currentPage === 0 |
|
|
|
|
|
|
|
? this.loopPages |
|
|
|
|
|
|
|
? this.pages.length - 1 |
|
|
|
|
|
|
|
: 0 |
|
|
|
|
|
|
|
: this.currentPage - 1 |
|
|
|
|
|
|
|
: this.pages.length - 1 |
|
|
|
|
|
|
|
); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
public addPage(page: MessageEmbed): Pagination { |
|
|
|
public addPage(page: MessageEmbed): Pagination { |
|
|
|
this.pages.push(page); |
|
|
|
this.pages.push(page); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
public replacePage(index: number, page: MessageEmbed): Pagination { |
|
|
|
public replacePage(index: number, page: MessageEmbed): Pagination { |
|
|
|
if (index < 0) {throw new RangeError("replacePage() param 'index' must be a value greater than 0");} |
|
|
|
if (index < 0) {throw new RangeError("replacePage() param 'index' must be a value greater than 0");} |
|
|
@ -62,5 +79,82 @@ export class Pagination { |
|
|
|
|
|
|
|
|
|
|
|
this.pages[index] = page; |
|
|
|
this.pages[index] = page; |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async setControllers(endTime: number, user?: 'any' | string, extraControls?: ExtraControls): Promise<Pagination> { |
|
|
|
|
|
|
|
if (this.controllers.enabled) {return;} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await this.message.react('⬅'); |
|
|
|
|
|
|
|
await this.message.react('➡'); |
|
|
|
|
|
|
|
await this.message.react('⏹'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let emoji = ['⬅', '➡', '⏹']; |
|
|
|
|
|
|
|
let filter = user && user.toLowerCase().trim() !== 'any' |
|
|
|
|
|
|
|
? (r: MessageReaction, u) => {return u.id === user.trim() && emoji.includes(r.emoji.name);} |
|
|
|
|
|
|
|
: (r: MessageReaction) => {return emoji.includes(r.emoji.name);}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.controllers.collector = this.message.createReactionCollector(filter, {time: 450000}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.controllers.collector.on('collect', async (r: MessageReaction) => { |
|
|
|
|
|
|
|
console.log(r); |
|
|
|
|
|
|
|
let functions = { |
|
|
|
|
|
|
|
'⬅': () => {return this.prevPage();}, |
|
|
|
|
|
|
|
'➡': () => {return this.nextPage();}, |
|
|
|
|
|
|
|
'⏹': () => {return this.endControllers();} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
this.controllers.lastInteraction = new Date(); |
|
|
|
|
|
|
|
return functions[r.emoji.name](); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.controllers.enabled = true; |
|
|
|
|
|
|
|
this.controllers.endTime = endTime; |
|
|
|
|
|
|
|
this.controllers.lastInteraction = new Date(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setInterval(() => { |
|
|
|
|
|
|
|
if (new Date().getTime() - this.controllers.lastInteraction.getTime() > this.controllers.endTime && this.controllers.enabled) {return this.endControllers();} |
|
|
|
|
|
|
|
}, this.controllers.endTime); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async updateControllers(): Promise<Pagination> {return this;}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async endControllers(): Promise<Pagination> { |
|
|
|
|
|
|
|
await this.message.reactions.removeAll(); |
|
|
|
|
|
|
|
this.controllers.collector.stop(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let fe = this.message.embeds[0]; |
|
|
|
|
|
|
|
fe.setDescription(`${fe.description}\n\n*This menu has ended, start a new one to interact with it!*`); |
|
|
|
|
|
|
|
fe.setFooter(`${fe.footer.text} | Menu ended`, this.client.user.avatarURL()); |
|
|
|
|
|
|
|
await this.message.edit(fe); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async start(options?: {endTime?: number, startPage?: number, user?: 'any' | string}): Promise<Pagination> { |
|
|
|
|
|
|
|
await this.setPage(options && options.startPage ? options.startPage : 0); |
|
|
|
|
|
|
|
await this.setControllers(options && options.endTime ? options.endTime : 60, options && options.user ? options.user : 'any'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async stop(): Promise<Pagination> { |
|
|
|
|
|
|
|
return await this.endControllers(); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface ExtraControls { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface ControllerData { |
|
|
|
|
|
|
|
endTime: number, |
|
|
|
|
|
|
|
enabled: boolean, |
|
|
|
|
|
|
|
lastInteraction: Date, |
|
|
|
|
|
|
|
collector: ReactionCollector |
|
|
|
} |
|
|
|
} |