djs-systems
Version:
The simplest way to build complex Discord bots.
229 lines (228 loc) • 11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.btnRole = void 0;
const discord_js_1 = require("discord.js");
const error_1 = require("./error");
const misc_1 = require("./misc");
// ------------------------------
// ------ F U N C T I O N -------
// ------------------------------
/**
* A **Button Role System** that lets you create button roles with your own message. | *Requires: [**manageBtnRole()**](https://simplyd.js.org/docs/handler/manageBtnRole)*
* @param msgOrInt
* @param options
* @link `Documentation:` https://simplyd.js.org/docs/general/btnRole
* @example simplydjs.btnRole(message, { data: [{...}] })
*/
async function btnRole(msgOrInt, options = { strict: false }) {
return new Promise(async (resolve) => {
try {
if (!options?.data) {
if (options?.strict)
throw new error_1.SimplyError({
function: 'btnRole',
title: 'Expected data object in options',
tip: `Received ${options.data || 'undefined'}`
});
else
console.log(`SimplyError - btnRole | Error: Expected data object in options.. Received ${options?.data || 'undefined'}`);
}
const extMessage = msgOrInt;
const extInteraction = msgOrInt;
if (msgOrInt.commandId) {
if (!extInteraction.deferred)
await extInteraction.deferReply({ fetchReply: true });
if (!extInteraction.member.permissions.has(discord_js_1.PermissionFlagsBits.Administrator))
await extInteraction.followUp({
content: 'You need `ADMINISTRATOR` permission to use this command'
});
return;
}
else if (!msgOrInt.customId) {
if (!extMessage.member.permissions.has(discord_js_1.PermissionFlagsBits.Administrator))
return await extMessage.reply({
content: 'You need `ADMINISTRATOR` permission to use this command'
});
}
const row = [];
const data = options.data;
if (data.length <= 5) {
const button = [[]];
GenButton(data, button, row);
}
else if (data.length > 5 && data.length <= 10) {
const button = [[], []];
GenButton(data, button, row);
}
else if (data.length > 11 && data.length <= 15) {
const button = [[], [], []];
GenButton(data, button, row);
}
else if (data.length > 16 && data.length <= 20) {
const button = [[], [], [], []];
GenButton(data, button, row);
}
else if (data.length > 21 && data.length <= 25) {
const button = [[], [], [], [], []];
GenButton(data, button, row);
}
else if (data.length > 25) {
if (options?.strict)
throw new error_1.SimplyError({
function: 'btnRole',
title: 'Reached the limit of 25 buttons..',
tip: 'Discord allows only 25 buttons in a message. Send a new message with more buttons.'
});
else
console.log(`SimplyError - btnRole | Error: Reached the limit of 25 buttons..\n\nDiscord allows only 25 buttons in a message. Send a new message with more buttons.`);
}
// Generates buttons from the data provided
async function GenButton(data, button, row) {
let current = 0;
for (let i = 0; i < data.length; i++) {
if (button[current].length === 5)
current++;
const emoji = data[i].emoji || null;
let color = data[i].style || discord_js_1.ButtonStyle.Secondary;
let url = '';
if (color)
color = (0, misc_1.toButtonStyle)(color);
const role = msgOrInt.guild.roles.cache.find((r) => r.id ===
(data[i].role?.id
? data[i].role.id
: data[i].role));
if (!role) {
if (options.strict)
throw new error_1.SimplyError({
function: 'btnRole',
title: 'Role not found',
tip: `Expected a Role or Role Id. Received ${data[i].role}`
});
else
console.log(`SimplyError - btnRole | Error: Role not found. Expected a Role or Role Id. Received ${data[i].role}`);
}
const label = data[i].label || role?.name;
if (!role && color === discord_js_1.ButtonStyle.Link) {
url = data[i].url;
button[current].push(createLink(label, url, emoji));
}
else {
button[current].push(createButton(label, role.id, color, emoji));
}
// push the row into array (So you can add more than a row of buttons)
if (i === data.length - 1) {
const newRow = addRow(button[current]);
row.push(newRow);
}
}
if (!options.embed && !options.content)
if (options.strict)
throw new error_1.SimplyError({
function: 'btnRole',
title: 'Provide an embed (or) content in the options.',
tip: `Expected embed (or) content options to send. Received ${options.embed || undefined}`
});
else
console.log(`SimplyError - btnRole | Error: Provide an embed (or) content in the options. Expected embed (or) content options to send. Received ${options.embed || undefined}`);
const embed = new discord_js_1.EmbedBuilder()
.setFooter(options?.embed?.footer
? options?.embed?.footer
: {
text: '©️ Rahuletto. npm i simply-djs',
iconURL: 'https://i.imgur.com/XFUIwPh.png'
})
.setColor(options?.embed?.color || (0, misc_1.toRgb)('#406DBC'));
if (options?.embed?.description)
embed.setDescription(options.embed?.description);
if (options?.embed?.fields)
embed.setFields(options.embed?.fields);
if (options?.embed?.author)
embed.setAuthor(options.embed?.author);
if (options?.embed?.image)
embed.setImage(options.embed?.image);
if (options?.embed?.thumbnail)
embed.setThumbnail(options.embed?.thumbnail);
if (options?.embed?.timestamp)
embed.setTimestamp(options.embed?.timestamp);
if (options?.embed?.title)
embed.setTitle(options.embed?.title);
if (options?.embed?.url)
embed.setURL(options.embed?.url);
if (extInteraction?.commandId) {
if (!options.embed) {
extInteraction.followUp({
content: options?.content || '** **',
components: row
});
}
else
extInteraction.followUp({
content: options.content || '** **',
embeds: [embed],
components: row
});
resolve(true);
}
else if (!extInteraction?.commandId) {
if (!options.embed) {
extMessage.channel.send({
content: options.content || '** **',
components: row
});
}
else
extMessage.channel.send({
content: options.content || '** **',
embeds: [embed],
components: row
});
resolve(true);
}
function addRow(btns) {
const btnRow = new discord_js_1.ActionRowBuilder();
btnRow.addComponents(btns);
return btnRow;
}
function createLink(label, url, emoji) {
const button = new discord_js_1.ButtonBuilder();
if (!emoji || emoji === null) {
button.setLabel(label).setStyle(discord_js_1.ButtonStyle.Link).setURL(url);
}
else if (emoji && emoji !== null) {
button
.setLabel(label)
.setStyle(discord_js_1.ButtonStyle.Link)
.setURL(url)
.setEmoji(emoji);
}
return button;
}
function createButton(label, role, color, emoji) {
if (color)
color = (0, misc_1.toButtonStyle)(color);
const btn = new discord_js_1.ButtonBuilder();
btn
.setLabel(label)
.setStyle(color || discord_js_1.ButtonStyle.Secondary)
.setCustomId('role-' + role);
if (emoji && emoji !== null) {
btn.setEmoji(emoji);
}
return btn;
}
}
}
catch (err) {
resolve(false);
if (options?.strict)
throw new error_1.SimplyError({
function: 'btnRole',
title: 'An Error occured when running the function ',
tip: err.stack
});
else
console.log(`SimplyError - btnRole | Error: ${err.stack}`);
}
});
}
exports.btnRole = btnRole;