@ayanaware/bentocord
Version:
Bentocord is a Bento plugin designed to rapidly build fully functional Discord Bots.
117 lines • 4.47 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChoicePrompt = void 0;
const CommandManager_1 = require("../../commands/constants/CommandManager");
const Button_1 = require("../../components/helpers/Button");
const Select_1 = require("../../components/helpers/Select");
const PaginationPrompt_1 = require("../PaginationPrompt");
class ChoicePrompt extends PaginationPrompt_1.PaginationPrompt {
constructor(ctx, paginator, options) {
super(ctx, paginator, options);
this.validator = this.handleText.bind(this);
this.sltChoice = new Select_1.Select(this.ctx, 'bc:choice:select', this.handleChoiceSelect.bind(this)).max(1);
this.btnChoice = new Button_1.Button(this.ctx, 'bc:choice:button', this.handleChoiceButton.bind(this)).success();
}
async start() {
await this.sltChoice.placeholderTranslated('BENTOCORD_CHOICE_SELECT', {}, 'Please choose an item');
await this.btnChoice.labelTranslated('BENTOCORD_CHOICE_SELECT_CHOOSE', {}, 'Choose');
return super.start();
}
async close(reason) {
// Handle close reason, respect PromptOptions
if (reason) {
if (typeof reason === 'object')
reason = await this.ctx.formatTranslation(reason);
if (this.options.showCloseError) {
if (this.options.closeErrorFollowup)
await this.ctx.createMessage(reason);
else
this.content(reason);
}
}
await this.cleanup();
this.reject(CommandManager_1.NON_ERROR_HALT);
}
async draw() {
this.clearRows();
// draw pagination
await super.draw();
const items = await this.paginator.getItems();
// single option per page
if (items.length === 1) {
this.addRow([this.btnChoice]);
return;
}
// multiple options per page
const options = [];
for (const { item, index } of items) {
const option = { value: index.toString(), label: index.toString() };
// label
let label = item.label;
if (typeof label === 'object')
label = await this.ctx.formatTranslation(label);
option.label = `${index + 1}: ${label}`;
// description
let description = item.description;
if (typeof description === 'object')
description = await this.ctx.formatTranslation(description);
option.description = description;
// emoji
if (item.emoji)
option.emoji = item.emoji;
options.push(option);
}
this.sltChoice.setOptions(options);
// add select
this.addRow([this.sltChoice]);
}
async handleChoiceSelect(slt) {
if (slt.values.length !== 1)
return;
const index = Number(slt.values[0]);
if (isNaN(index))
return;
await slt.deferUpdate();
const { item } = await this.paginator.getItem(index);
if (!item)
return;
await this.cleanup();
this.resolve(item.value);
}
async handleChoiceButton(btn) {
const items = await this.paginator.getItems();
if (items.length !== 1)
return;
const { item } = items[0];
if (!item)
return;
await btn.deferUpdate();
await this.cleanup();
this.resolve(item.value);
}
async handleText(response) {
response = response.toLocaleLowerCase();
// handle number select
const items = await this.paginator.getItems();
for (const { item, index } of items) {
// handle text: number
const num = index + 1; // convert to 1-index
if (response === num.toString()) {
await this.cleanup();
return [true, item.value];
}
// handle text: item.match
if (!Array.isArray(item.match))
continue;
// lowercase
const match = item.match.map(m => m.toLocaleLowerCase());
if (!match.some(m => response === m))
continue;
await this.cleanup();
return [true, item.value];
}
return super.handleText(response);
}
}
exports.ChoicePrompt = ChoicePrompt;
//# sourceMappingURL=ChoicePrompt.js.map