UNPKG

@shadow-dev/core

Version:

A modular core framework for Discord bot development, providing commands, buttons, menus, middleware, and more.

98 lines (97 loc) 3.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Command = void 0; const discord_js_1 = require("discord.js"); const util_1 = require("../util"); class Command { constructor(options) { // Object.assign(this, options) this.name = options.name; this.description = options.description; this.run = options.run; this.options = options.options; if (options.roles) { this.roles = options.roles; } if (options.permissions) { this.permissions = options.permissions; } if (options.ownerOnly) { this.ownerOnly = options.ownerOnly; } } async middleware(interaction, client, args, command) { const guild = client.guilds.cache.find((guild) => guild.id == interaction.guildId); const userCooldown = (0, util_1.checkCooldown)(interaction.user.id, command.name, 3000); if (command.ownerOnly) { if (!(0, util_1.isBotOwner)(interaction.user.id, guild?.ownerId ?? "")) { return interaction.reply({ content: "You do not have permission to run this command.", flags: [discord_js_1.MessageFlags.Ephemeral], }); } if (!userCooldown) { return interaction.reply({ content: "Please wait to use this command again.", flags: [discord_js_1.MessageFlags.Ephemeral], }); } command.run(interaction, client, args); } else if (command.roles) { // eslint-disable-next-line let roles = Array(); command.roles.forEach((role) => { const foundRole = guild?.roles.cache.find((findRole) => findRole.name == role); if (!foundRole) { return interaction.reply({ content: "It seems I could not find the roles required for this command.", flags: [discord_js_1.MessageFlags.Ephemeral], }); } roles.push(foundRole); }); let hasRoles = false; const member = guild?.members.cache.get(interaction.user.id); member?.roles.cache.forEach((role) => { roles.forEach((requiredRole) => { if (role == requiredRole) { hasRoles = true; } }); }); if (!hasRoles) { return interaction.reply({ content: "You do not have permission to run this command.", flags: [discord_js_1.MessageFlags.Ephemeral], }); } command.run(interaction, client, args); } else if (command.permissions) { if (!interaction.memberPermissions?.has(command.permissions)) { return interaction.reply({ content: "You do not have permission to run this command.", flags: [discord_js_1.MessageFlags.Ephemeral], }); } if (!userCooldown) { return interaction.reply({ content: "Please wait to use this command again.", flags: [discord_js_1.MessageFlags.Ephemeral], }); } command.run(interaction, client, args); } else { if (!userCooldown) { return interaction.reply({ content: "Please wait to use this command again.", flags: [discord_js_1.MessageFlags.Ephemeral], }); } command.run(interaction, client, args); } } } exports.Command = Command;