parent
bfd2a168ff
commit
ab34712dec
@ -1,86 +1,60 @@ |
|||||||
"use strict"; |
"use strict"; |
||||||
Object.defineProperty(exports, "__esModule", { value: true }); |
Object.defineProperty(exports, "__esModule", { value: true }); |
||||||
exports.Page = exports.Pagination = void 0; |
exports.Pagination = void 0; |
||||||
const discord_js_1 = require("discord.js"); |
|
||||||
class Pagination { |
class Pagination { |
||||||
constructor(title, pages, zeroPage, client, message, activationMessage, timeout, description, pageTemplate) { |
constructor(channel, pages, originalMessage, client, message) { |
||||||
this.currentpos = 0; |
this.channel = channel; |
||||||
this.title = title; |
|
||||||
this.pages = pages; |
this.pages = pages; |
||||||
this.zeroPage = zeroPage; |
this.originalMessage = message; |
||||||
this.message = message; |
|
||||||
this.timeout = timeout; |
|
||||||
this.activationMessage = activationMessage; |
|
||||||
this.client = client; |
this.client = client; |
||||||
this.description = description ? description : `Requested by ${activationMessage.guild ? activationMessage.member.displayName : activationMessage.author.username}.`; |
this.currentPage = 0; |
||||||
this.pageTemplate = pageTemplate |
if (message) { |
||||||
? pageTemplate |
this.message = message; |
||||||
: new discord_js_1.MessageEmbed() |
} |
||||||
.setDescription(this.description) |
|
||||||
.addField('Navigation', `Click or tap the arrows below this message to navigate through the pages!\n\n*This menu will timeout in ${this.timeout}ms.`) |
|
||||||
.setColor('c375f0') |
|
||||||
.setFooter('Natsuki', this.client.user.avatarURL()) |
|
||||||
.setTimestamp(); |
|
||||||
} |
|
||||||
; |
|
||||||
addPage(page) { |
|
||||||
this.pages.push(page); |
|
||||||
return this; |
|
||||||
} |
} |
||||||
; |
; |
||||||
render(pos) { |
async setPage(page) { |
||||||
let page = this.pages[this.currentpos]; |
if (this.pages.length < page + 1) { } |
||||||
let pageEmbed = new discord_js_1.MessageEmbed() |
if (!this.message) { |
||||||
.setTitle(`${this.title} -> ${page.title}`) |
let tempm = await this.channel.send("One moment...") |
||||||
.setDescription(`${this.pageTemplate.description ? this.pageTemplate.description : this.description}\n\n${page.description}`) |
.catch(() => { this.originalMessage.reply("There seemed to be a problem doing that..."); return this; }); |
||||||
.setColor(this.pageTemplate.hexColor ? this.pageTemplate.hexColor : 'c375f0') |
if (tempm instanceof Pagination) { |
||||||
.setFooter(this.pageTemplate.footer ? `${this.pageTemplate.footer.text} | Page ${this.currentpos + 1} of ${this.pages.length}` : `Natsuki | Page ${this.currentpos + 1} of ${this.pages.length}`) |
return this; |
||||||
.setTimestamp(); |
} |
||||||
let item; |
else { |
||||||
for (item of page.items) { |
this.message = tempm; |
||||||
pageEmbed.addField(item.title, item.text); |
} |
||||||
} |
} |
||||||
if (this.pageTemplate.thumbnail) { |
await this.message.edit(this.pages[page] |
||||||
pageEmbed.setThumbnail(this.pageTemplate.thumbnail.url); |
.setFooter(`Natsuki | Page ${page + 1} of ${this.pages.length}`, this.client.user.avatarURL()) |
||||||
} |
.setTimestamp()); |
||||||
this.message.edit(pageEmbed); |
this.currentPage = page; |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
; |
; |
||||||
nextPage() { |
async nextPage() { |
||||||
return this.render(this.currentpos < (this.pages.length - 1) ? this.currentpos + 1 : this.currentpos); |
await this.setPage(typeof this.currentPage === "number" ? this.currentPage + 1 == this.pages.length ? this.currentPage : this.currentPage + 1 : 0); |
||||||
} |
|
||||||
; |
|
||||||
prevPage() { |
|
||||||
return this.render(this.currentpos > 0 ? this.currentpos - 1 : this.currentpos); |
|
||||||
} |
|
||||||
; |
|
||||||
destroy(delmsg, fmsg) { |
|
||||||
return this; |
return this; |
||||||
} |
} |
||||||
; |
; |
||||||
resetTimeout(newTimeout) { |
async prevPage() { |
||||||
|
await this.setPage(typeof this.currentPage === "number" ? this.currentPage === 0 ? 0 : this.currentPage - 1 : this.pages.length - 1); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
; |
; |
||||||
init() { |
addPage(page) { |
||||||
|
this.pages.push(page); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
; |
replacePage(index, page) { |
||||||
} |
if (index < 0) { |
||||||
exports.Pagination = Pagination; |
throw new RangeError("replacePage() param 'index' must be a value greater than 0"); |
||||||
class Page { |
} |
||||||
constructor(title, items, description) { |
if (index > this.pages.length - 1) { |
||||||
this.items = []; |
throw new RangeError("replacePage() param 'index' must be a value corresponding to an index that already exists in this instance's pages."); |
||||||
this.title = title; |
} |
||||||
this.items = items; |
this.pages[index] = page; |
||||||
this.description = description; |
|
||||||
} |
|
||||||
; |
|
||||||
addItem(item) { |
|
||||||
this.items.push(item); |
|
||||||
return this; |
return this; |
||||||
} |
} |
||||||
; |
|
||||||
} |
} |
||||||
exports.Page = Page; |
exports.Pagination = Pagination; |
||||||
|
@ -1,119 +1,66 @@ |
|||||||
import {MessageEmbed, Message, Client} from 'discord.js'; |
import {TextChannel, Message, MessageEmbed, Client} from 'discord.js'; |
||||||
|
|
||||||
import wait = require('../../util/wait'); |
|
||||||
|
|
||||||
export class Pagination { |
export class Pagination { |
||||||
title: string; |
channel: TextChannel; |
||||||
pages: Page[]; |
|
||||||
zeroPage: Page | MessageEmbed; |
|
||||||
pageTemplate: MessageEmbed; |
|
||||||
message: Message; |
message: Message; |
||||||
timeout: Number; |
pages: MessageEmbed[]; |
||||||
description: string; |
originalMessage: Message; |
||||||
activationMessage: Message; |
currentPage: number; |
||||||
client: Client; |
client: Client; |
||||||
currentpos: number = 0; |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
constructor (title: string, pages: Page[], zeroPage: Page | MessageEmbed, client: Client, message: Message, activationMessage: Message, timeout: number, description?: string, pageTemplate?: MessageEmbed) { |
|
||||||
this.title = title; |
|
||||||
|
|
||||||
let tpages = []; |
constructor (channel: TextChannel, pages: MessageEmbed[], originalMessage: Message, client: Client, message?: Message) { |
||||||
tpages.push(zeroPage); |
this.channel = channel; |
||||||
let tpage: Page; for (tpage of pages) {tpages.push(tpage);} |
this.pages = pages; |
||||||
this.pages = tpages; |
this.originalMessage = message; |
||||||
|
|
||||||
this.zeroPage = zeroPage; |
|
||||||
this.message = message; |
|
||||||
this.timeout = timeout; |
|
||||||
this.activationMessage = activationMessage; |
|
||||||
this.client = client; |
this.client = client; |
||||||
|
this.currentPage = 0; |
||||||
|
|
||||||
this.description = description ? description : `Requested by ${activationMessage.guild ? activationMessage.member.displayName : activationMessage.author.username}.`; |
if (message) {this.message = message;} |
||||||
|
|
||||||
this.pageTemplate = pageTemplate |
|
||||||
? pageTemplate |
|
||||||
: new MessageEmbed() |
|
||||||
.setDescription(this.description) |
|
||||||
.addField('Navigation', `Click or tap the arrows below this message to navigate through the pages!\n\n*This menu will timeout in ${this.timeout}ms.`) |
|
||||||
.setColor('c375f0') |
|
||||||
.setFooter('Natsuki', this.client.user.avatarURL()) |
|
||||||
.setTimestamp(); |
|
||||||
}; |
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public addPage(page: Page): Pagination { |
public async setPage(page: number): Promise<Pagination> { |
||||||
this.pages.push(page); |
if (this.pages.length < page + 1) {} |
||||||
return this; |
|
||||||
}; |
|
||||||
|
|
||||||
public render(pos: number): Pagination { |
if (!this.message) { |
||||||
let page = this.pages[this.currentpos]; |
let tempm = await this.channel.send("One moment...") |
||||||
let pageEmbed: MessageEmbed = new MessageEmbed() |
.catch(() => {this.originalMessage.reply("There seemed to be a problem doing that..."); return this;}); |
||||||
.setTitle(`${this.title} -> ${page.title}`) |
if (tempm instanceof Pagination) {return this;} |
||||||
.setDescription(`${this.pageTemplate.description ? this.pageTemplate.description : this.description}\n\n${page.description}`) |
else {this.message = tempm;} |
||||||
.setColor(this.pageTemplate.hexColor ? this.pageTemplate.hexColor : 'c375f0') |
} |
||||||
.setFooter(this.pageTemplate.footer ? `${this.pageTemplate.footer.text} | Page ${this.currentpos + 1} of ${this.pages.length}` : `Natsuki | Page ${this.currentpos + 1} of ${this.pages.length}`) |
|
||||||
.setTimestamp(); |
|
||||||
let item: PageItem; for (item of page.items) {pageEmbed.addField(item.title, item.text);} |
|
||||||
if (this.pageTemplate.thumbnail) {pageEmbed.setThumbnail(this.pageTemplate.thumbnail.url);} |
|
||||||
|
|
||||||
this.message.edit(pageEmbed); |
await this.message.edit(this.pages[page] |
||||||
|
.setFooter(`Natsuki | Page ${page + 1} of ${this.pages.length}`, this.client.user.avatarURL()) |
||||||
|
.setTimestamp() |
||||||
|
); |
||||||
|
this.currentPage = page; |
||||||
|
|
||||||
return this; |
return this; |
||||||
}; |
}; |
||||||
|
|
||||||
public nextPage(): Pagination { |
public async nextPage(): Promise<Pagination> { |
||||||
return this.render(this.currentpos < (this.pages.length - 1) ? this.currentpos + 1 : this.currentpos); |
await this.setPage(typeof this.currentPage === "number" ? this.currentPage + 1 == this.pages.length ? this.currentPage : this.currentPage + 1 : 0); |
||||||
}; |
|
||||||
|
|
||||||
public prevPage(): Pagination { |
|
||||||
return this.render(this.currentpos > 0 ? this.currentpos - 1 : this.currentpos); |
|
||||||
}; |
|
||||||
|
|
||||||
public destroy(delmsg?: boolean, fmsg?: Message): Pagination { |
|
||||||
return this; |
return this; |
||||||
}; |
}; |
||||||
|
|
||||||
public resetTimeout(newTimeout?: number): Pagination { |
public async prevPage(): Promise<Pagination> { |
||||||
|
await this.setPage(typeof this.currentPage === "number" ? this.currentPage === 0 ? 0 : this.currentPage - 1 : this.pages.length - 1); |
||||||
return this; |
return this; |
||||||
}; |
}; |
||||||
|
|
||||||
public init(): Pagination { |
public addPage(page: MessageEmbed): Pagination { |
||||||
|
this.pages.push(page); |
||||||
|
|
||||||
return this; |
return this; |
||||||
}; |
} |
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export class Page { |
|
||||||
items: PageItem[] = []; |
|
||||||
title: string; |
|
||||||
description: string; |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
constructor(title: string, items: PageItem[], description?: string) { |
|
||||||
this.title = title; |
|
||||||
this.items = items; |
|
||||||
this.description = description; |
|
||||||
}; |
|
||||||
|
|
||||||
|
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 > this.pages.length - 1) {throw new RangeError("replacePage() param 'index' must be a value corresponding to an index that already exists in this instance's pages.");} |
||||||
|
|
||||||
|
this.pages[index] = page; |
||||||
public addItem(item: PageItem): Page { |
|
||||||
this.items.push(item); |
|
||||||
return this; |
return this; |
||||||
}; |
} |
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
interface PageItem { |
|
||||||
title: string, |
|
||||||
text: string |
|
||||||
} |
} |
Loading…
Reference in new issue