UNPKG

sheweny

Version:

The powerful framework for create discord bots

119 lines 6.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const discord_js_1 = require("discord.js"); const constants_js_1 = require("../constants/constants.js"); const index_js_1 = require("../helpers/index.js"); async function run(client, interaction) { try { if (!client.managers.commands) return; /* -----------------COMMAND----------------- */ const commands = client.collections.commands?.get(interaction.commandName); if (!commands || (commands && !commands.length)) return; for (const command of commands) { await (async () => { // eslint-disable-next-line // @ts-ignore if (!command || (command && ![constants_js_1.COMMAND_TYPE.cmdSlash, constants_js_1.COMMAND_TYPE.ctxUser, constants_js_1.COMMAND_TYPE.ctxMsg].includes(command.type))) { return; } if (!command.enabled) return; if (command.before) await command.before(interaction); /** * Handle inhibitors */ const inhibitorsCollection = client.collections.inhibitors?.filter((is) => { for (const i of is) { return i.type.includes(constants_js_1.INHIBITOR_TYPE.appCommand) || i.type.includes(constants_js_1.INHIBITOR_TYPE.all); } return false; }); const inhibitorsArray = []; for (const [, inhibitors] of inhibitorsCollection) { if (inhibitors && inhibitors.length) { for (const inhibitor of inhibitors) { inhibitorsArray.push(inhibitor); } } } if (inhibitorsArray && inhibitorsArray.length) { const sorted = inhibitorsArray.sort((a, b) => b.priority - a.priority); for (const i of sorted) { if (!(await i.execute(command, interaction))) return await i.onFailure(command, interaction); } } /* ---------------PERMISSIONS--------------- */ const userMissingPerms = []; const clientMissingPerms = []; if (command.adminsOnly && !client.admins?.includes(interaction.user.id)) { userMissingPerms.push(constants_js_1.COMMAND_PERMISSIONS.admin); } /* ---------------IN-GUILD--------------- */ if (interaction.guild) { if (command.channel === constants_js_1.COMMAND_CHANNEL.dm) { return client.managers.commands?.emit(constants_js_1.COMMAND_EVENTS.invalidChannel, command, interaction); } let member = interaction.guild.members.cache.get(interaction.user.id); if (!member) member = await interaction.guild.members.fetch(interaction.user.id); if (command.userPermissions.length > 0) { for (const permission of command.userPermissions) { if (!member.permissions.has(permission)) { userMissingPerms.push(permission.toString()); } } } if (userMissingPerms.length) { return client.managers.commands?.emit(constants_js_1.COMMAND_EVENTS.userMissingPerm, interaction, userMissingPerms, command); } if (command.clientPermissions.length > 0) { for (const permission of command.clientPermissions) { if (!interaction.guild.members.me?.permissions.has(permission)) { clientMissingPerms.push(permission.toString()); } } } if (clientMissingPerms.length) { return client.managers.commands?.emit(constants_js_1.COMMAND_EVENTS.clientMissingPerm, interaction, clientMissingPerms, command); } } else if (command.channel === constants_js_1.COMMAND_CHANNEL.guild) { return client.managers.commands?.emit(constants_js_1.COMMAND_EVENTS.invalidChannel, command, interaction); } /* ---------------COOLDOWNS--------------- */ if (!client.disableCooldownsForAdmins || (client.disableCooldownsForAdmins && !client.admins?.includes(interaction.user.id))) { if (!client.cooldowns.commands.has(command.name)) { client.cooldowns.commands.set(command.name, new discord_js_1.Collection()); } const timeNow = Date.now(); const tStamps = client.cooldowns.commands.get(command.name); if (tStamps) { const cdAmount = (command.cooldown || 0) * 1000; if (tStamps.has(interaction.user.id)) { const cdExpirationTime = (tStamps.get(interaction.user.id) || 0) + cdAmount; if (timeNow < cdExpirationTime) { // const timeLeft = (cdExpirationTime - timeNow) / 1000; return client.managers.commands?.emit(constants_js_1.COMMAND_EVENTS.cooldownLimit, interaction, cdExpirationTime - timeNow); } } tStamps.set(interaction.user.id, timeNow); setTimeout(() => tStamps.delete(interaction.user.id), cdAmount); } } /* ---------------COMMAND--------------- */ await command.execute(interaction); })(); } } catch (err) { const e = err; new index_js_1.ShewenyError(client, e); } } exports.default = run; //# sourceMappingURL=interactionCommandCreate.js.map