djs-systems
Version:
The simplest way to build complex Discord bots.
153 lines (152 loc) • 6.58 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.menuPages = void 0;
const discord_js_1 = require("discord.js");
const SimplyError_1 = require("./error/SimplyError");
const misc_1 = require("./misc");
// ------------------------------
// ------ F U N C T I O N -------
// ------------------------------
/**
* An Embed paginator using Select Menus
* @param msgOrInt
* @param options
* @link `Documentation:` https://simplyd.js.org/docs/general/menuPages
* @example simplydjs.menuPages(interaction, { data: [{...}] })
*/
async function menuPages(msgOrInt, options = { strict: false }) {
return new Promise(async () => {
try {
const type = options.type || 'Send';
if (type !== 'Send' && type !== 'Receive') {
if (options?.strict)
throw new SimplyError_1.SimplyError({
function: 'menuPages',
title: 'There are only two types. You have provided a type which doesnt exist',
tip: 'There are only "Send" and "Edit" where "Send" sends a ephemeral message but "Edit" edits the original message'
});
else
console.log(`SimplyError - menuPages | Error: There are only two types. You have provided a type which doesnt exist.\n\nThere are only "Send" and "Edit" where "Send" sends a ephemeral message but "Edit" edits the original message`);
}
const data = options.data;
const rowOption = options.rows;
const menuOptions = [];
for (let i = 0; i < data.length; i++) {
if (data[i].emoji) {
const dataopt = {
label: data[i].label,
description: data[i].description,
value: data[i].label,
emoji: data[i].emoji
};
menuOptions.push(dataopt);
}
else if (!data[i].emoji) {
const dataopt = {
label: data[i].label,
description: data[i].description,
value: data[i].label
};
menuOptions.push(dataopt);
}
}
if (options.delete?.enable === undefined ||
options.delete?.enable === true) {
const delopt = {
label: options?.delete?.label || 'Delete',
description: options.delete?.description || 'Delete the Select Menu Embed',
value: 'delete_menuPages',
emoji: options?.delete?.emoji || '❌'
};
menuOptions.push(delopt);
}
const slct = new discord_js_1.StringSelectMenuBuilder()
.setMaxValues(1)
.setCustomId('menuPages')
.setPlaceholder(options.placeHolder || 'Dropdown Pages')
.addOptions(menuOptions);
const row = new discord_js_1.ActionRowBuilder().addComponents(slct);
const rows = [];
rows.push(row);
if (rowOption) {
for (let i = 0; i < rowOption.length; i++) {
rows.push(rowOption[i]);
}
}
let interaction;
if (msgOrInt.commandId) {
interaction = msgOrInt;
if (!interaction.deferred)
await interaction.deferReply({ fetchReply: true });
}
const extInteraction = msgOrInt;
const extMessage = msgOrInt;
let m;
if (interaction) {
m = await extInteraction.followUp({
embeds: [options.embed],
components: rows,
fetchReply: true
});
}
else if (!interaction) {
m = await extMessage.reply({
embeds: [options.embed],
components: rows
});
}
const collector = m.createMessageComponentCollector({
componentType: discord_js_1.ComponentType.StringSelect,
idle: (0, misc_1.ms)('10m')
});
collector.on('collect', async (menu) => {
const selected = menu.values[0];
if (type === 'Edit') {
if (msgOrInt.member.user.id !== menu.user.id)
await menu.reply({
content: "You cannot access other's pagination.",
ephemeral: true
});
return;
}
if (selected === 'delete_menuPages') {
if (msgOrInt.member.user.id !== menu.user.id)
await menu.reply({
content: "You cannot access other's pagination.",
ephemeral: true
});
else
collector.stop('delete');
}
for (let i = 0; i < data.length; i++) {
if (selected === data[i].label) {
if (type === 'Send') {
await menu.reply({ embeds: [data[i].embed], ephemeral: true });
}
else if (type === 'Edit') {
menu.message.edit({ embeds: [data[i].embed] });
}
}
}
});
collector.on('end', async (collected, reason) => {
if (reason === 'delete')
await m.delete();
if (collected.size === 0) {
m.edit({ embeds: [options.embed], components: [] });
}
});
}
catch (err) {
if (options?.strict)
throw new SimplyError_1.SimplyError({
function: 'menuPages',
title: 'An Error occured when running the function ',
tip: err.stack
});
else
console.log(`SimplyError - menuPages | Error: ${err.stack}`);
}
});
}
exports.menuPages = menuPages;