Book.ts

mail@pastecode.io avatar
unknown
typescript
2 years ago
2.0 kB
2
Indexable
Never
import { CommandInteraction, MessageActionRow, MessageButton } from "discord.js";
import { Page } from "./Page.js";

export class Book {
    private interaction: CommandInteraction;
    private pages: Page[] = [];
    private currentPagePosition = 0;
    private _row: MessageActionRow;

    constructor(interaction: CommandInteraction) {
        this.interaction = interaction;
        this.updateRow();
    }

    get currentPage() {
        return this.pages[this.currentPagePosition];
    }

    get row() {
        return this._row;
    }

    addPages(...pages: Page[]): number {
        return this.pages.push(...pages);
    }

    updateRow() {
        this._row = new MessageActionRow().addComponents(
            this.createLeftButton(),
            this.createRightButton(),
            this.createCloseButton()
        );
    }

    update() {
        this.updateRow();
        this.interaction.editReply({
            embeds: [this.currentPage.content],
            components: [this._row],
        });
    }

    nextPage() {
        this.currentPagePosition++;
        this.update();
    }

    previousPage() {
        this.currentPagePosition--;
        this.update();
    }

    private createLeftButton() {
        return new MessageButton()
            .setCustomId("left_button")
            .setStyle("PRIMARY")
            .setDisabled(this.currentPagePosition === 0)
            .setEmoji("⬅")
            .setLabel("Retornar");
    }

    private createRightButton() {
        return new MessageButton()
            .setCustomId("right_button")
            .setStyle("PRIMARY")
            .setDisabled(this.currentPagePosition === this.pages.length - 1)
            .setEmoji("➡")
            .setLabel("Avançar");
    }

    private createCloseButton() {
        return new MessageButton()
            .setCustomId("close_button")
            .setStyle("DANGER")
            .setEmoji("❌")
            .setLabel("Fechar");
    }
}