djs-menu-v13
Version:
A module for create simple and complex menu for your Discord Bot
185 lines (184 loc) • 8.68 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Menu = void 0;
var events_1 = require("events");
var discord_js_1 = require("discord.js");
var Menu = (function (_super) {
__extends(Menu, _super);
function Menu(interaction) {
var _this = _super.call(this) || this;
_this.pages = [];
_this.interaction = interaction;
_this.collector = {};
_this.ephemeral = false;
return _this;
}
Menu.prototype.addPage = function (page) {
if (!page)
throw new Error('The page is undefined');
this.pages.push(page);
return this;
};
Menu.prototype.start = function (id) {
if (!id)
throw new Error('The id is undefined');
if (!this.interaction)
throw new Error('The interaction is invalid');
var findPage = this.pages.find(function (page) { return page.id === id; });
if (!findPage)
throw new Error("The page with id " + id + " was not found");
var startPage = id && findPage ? findPage : this.pages[0];
this.setPage(startPage);
return this;
};
Menu.prototype.stop = function () {
this.emit('stop', this.interaction, 'stop', this.pages);
};
Menu.prototype.setPage = function (page) {
var _this = this;
var _a, _b, _c;
this.displayPage(page);
var filter = function (i) {
return i.user.id === _this.interaction.user.id &&
i.customId.split('.')[1] === _this.interaction.user.id;
};
this.collector = (_a = this.interaction.channel) === null || _a === void 0 ? void 0 : _a.createMessageComponentCollector({ filter: filter, time: page === null || page === void 0 ? void 0 : page.timeout, max: 1 });
this.emit('pageChanged', page, this.interaction, this.pages);
this.once('stop', function () {
_this.collector.stop();
});
(_b = this.collector) === null || _b === void 0 ? void 0 : _b.on('collect', function (i) {
i.deferUpdate();
var btnPage = page;
if (btnPage.type === 'MenuPage') {
var id_1 = i.customId.split('.')[0];
var findBtn = btnPage.buttons.find(function (button) { return button.id === id_1; });
if (findBtn && typeof findBtn.target !== 'function') {
var target_1 = findBtn.target;
var nextPage = _this.pages.find(function (ptf) { return ptf.id === target_1; });
_this.collector.stop();
if (nextPage)
_this.setPage(nextPage);
}
else if (findBtn) {
var target = findBtn.target;
var funcTarget = target;
funcTarget(page, _this.interaction, _this);
}
}
else if (btnPage.type === 'MenuSelectPage') {
var value_1 = i.values[0].split('.');
var currentPage = _this.pages.find(function (ptf) { return ptf.id === value_1[1]; });
var currentChoice_1 = currentPage.options.find(function (option) { return option.value.split('.')[0] === value_1[0]; });
if (!currentChoice_1) {
throw new Error('The target was not found');
}
if (typeof currentChoice_1.target === 'function') {
var funcTarget = currentChoice_1.target;
funcTarget(page, _this.interaction, _this);
}
else {
_this.collector.stop();
var nextPage = _this.pages.find(function (ptf) { return ptf.id === currentChoice_1.target; });
if (nextPage)
_this.setPage(nextPage);
}
}
});
(_c = this.collector) === null || _c === void 0 ? void 0 : _c.on('end', function (collected) {
if (collected.size === 0) {
_this.emit('stop', _this.interaction, 'noReply', _this.pages);
}
});
};
Menu.prototype.isEphemeral = function (isEphemeral) {
this.ephemeral = isEphemeral;
return this;
};
Menu.prototype.displayPage = function (page) {
var btnPage = page;
if (btnPage.type === 'MenuPage') {
var content = (page === null || page === void 0 ? void 0 : page.content) || '.';
var buttons = (btnPage === null || btnPage === void 0 ? void 0 : btnPage.buttons) || [];
var raw = new discord_js_1.MessageActionRow();
for (var _i = 0, buttons_1 = buttons; _i < buttons_1.length; _i++) {
var button = buttons_1[_i];
var btn = new discord_js_1.MessageButton()
.setCustomId(button.id + "." + this.interaction.user.id)
.setLabel("" + button.label)
.setStyle(button.style);
if (button.emoji) {
btn.setEmoji(button.emoji);
}
if (button === null || button === void 0 ? void 0 : button.url) {
btn.setURL("" + (button === null || button === void 0 ? void 0 : button.url));
}
raw.addComponents(btn);
}
if (this.interaction.replied) {
this.interaction.editReply({
embeds: btnPage === null || btnPage === void 0 ? void 0 : btnPage.embeds,
content: "" + content,
components: [raw],
files: btnPage === null || btnPage === void 0 ? void 0 : btnPage.files,
});
}
else {
this.interaction.reply({
embeds: btnPage === null || btnPage === void 0 ? void 0 : btnPage.embeds,
content: "" + content,
components: [raw],
ephemeral: this.ephemeral,
files: btnPage === null || btnPage === void 0 ? void 0 : btnPage.files,
});
}
}
else {
var selectPage = page;
var content = (page === null || page === void 0 ? void 0 : page.content) || '.';
var selectMenuOptions = selectPage.options;
var raw = new discord_js_1.MessageActionRow();
var selectMenu = new discord_js_1.MessageSelectMenu()
.addOptions(selectMenuOptions)
.setCustomId(selectPage.id + "." + this.interaction.user.id)
.setPlaceholder(selectPage.placeholder);
selectMenu.setMinValues(1);
selectMenu.setMaxValues(1);
raw.addComponents(selectMenu);
if (this.interaction.replied) {
this.interaction.editReply({
embeds: selectPage === null || selectPage === void 0 ? void 0 : selectPage.embeds,
content: "" + content,
components: [raw],
files: selectPage === null || selectPage === void 0 ? void 0 : selectPage.files,
});
}
else {
this.interaction.reply({
embeds: selectPage === null || selectPage === void 0 ? void 0 : selectPage.embeds,
content: "" + content,
components: [raw],
ephemeral: this.ephemeral,
files: selectPage === null || selectPage === void 0 ? void 0 : selectPage.files,
});
}
}
};
return Menu;
}(events_1.EventEmitter));
exports.Menu = Menu;