djs-systems
Version:
The simplest way to build complex Discord bots.
92 lines (91 loc) • 4.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.manageBtnRole = void 0;
const error_1 = require("../error");
// ------------------------------
// ------ F U N C T I O N -------
// ------------------------------
/**
* A Button Role Handler for **simplydjs button role system.**
* @param button
* @param options
* @link `Documentation:` https://simplyd.js.org/docs/hhandler/manageBtnRole
* @example simplydjs.manageBtnRole(interaction)
*/
async function manageBtnRole(button, options = {}) {
return new Promise(async (resolve) => {
if (button.isButton()) {
try {
const member = button.member;
if (button.customId.startsWith('role-')) {
const roleId = button.customId.replace('role-', '');
const role = await button.guild.roles.fetch(roleId, {
force: true
});
if (!role)
return;
else {
await button.deferReply({ ephemeral: true });
if (!member.roles.cache.find((r) => r.id === role.id)) {
member.roles
.add(role)
.catch((err) => {
resolve(false);
if (options?.strict)
throw new error_1.SimplyError({
function: 'manageBtnRole',
title: 'Role is higher than the bot. Missing Permissions',
tip: err.stack
});
else
button.channel.send({
content: 'ERROR: Role is higher than me. `Missing Permissions`'
});
});
await button.editReply({
content: options?.reply?.add ||
`✅ Added the ${role.toString()} role to you.`
});
resolve(true);
}
else if (member.roles.cache.find((r) => r.id === role.id)) {
member.roles
.remove(role)
.catch((err) => {
resolve(false);
if (options?.strict)
throw new error_1.SimplyError({
function: 'manageBtnRole',
title: 'Role is higher than the bot. Missing Permissions',
tip: err.stack
});
else
button.channel.send({
content: 'ERROR: Role is higher than me. `Missing Permissions`'
});
});
await button.editReply({
content: options?.reply?.remove ||
`❌ Removed the ${role.toString()} role from you.`
});
resolve(true);
}
}
}
}
catch (err) {
if (options?.strict)
throw new error_1.SimplyError({
function: 'manageBtnRole',
title: 'An Error occured when running the function ',
tip: err.stack
});
else
console.log(`SimplyError - manageBtnRole | Error: ${err.stack}`);
}
}
else
return;
});
}
exports.manageBtnRole = manageBtnRole;