UNPKG

kakashi-self-handler

Version:
237 lines (215 loc) 8.67 kB
const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, messageLink } = require("discord.js"); async function handleInteraction(interaction, kakashi) { const { commands, buttons, selectMenus, modals } = kakashi; const { commandName, customId } = interaction; const cooldownKey = `${interaction.user.id}-${commandName || customId}`; const userCooldown = kakashi.cooldowns.get(cooldownKey); if (userCooldown && userCooldown > Date.now()) { return sendCooldownEmbed(interaction, kakashi, userCooldown).catch(handleError); } const { command, cooldownDuration, voteOnly } = findCommandAndCooldown(commands, buttons, selectMenus, modals, interaction, commandName, customId); if (!command) return; setCooldown(kakashi, cooldownKey, cooldownDuration); if (!checkTopGG(kakashi, commandName)) { return sendTopGGError(interaction).catch(handleError); } if (voteOnly && !await kakashi.topgg.hasVoted(interaction.user.id)) { return sendVoteEmbed(interaction, kakashi).catch(handleError); } executeCommand(command, interaction, kakashi).catch(handleError); } function findCommandAndCooldown(commands, buttons, selectMenus, modals, interaction, commandName, customId) { let command = null; let cooldownDuration = 0; let voteOnly = null; if (interaction.isChatInputCommand()) { command = commands.get(commandName); } else if (interaction.isButton()) { command = buttons.get(customId); } else if (interaction.isStringSelectMenu()) { command = selectMenus.get(customId); } else if (interaction.isModalSubmit()) { command = modals.get(customId); } if (interaction.isChatInputCommand()) { cooldownDuration = parseInt(command?.cooldown, 10) || 0; voteOnly = command?.voteonly; } else { cooldownDuration = parseInt(command?.data.cooldown, 10) || 0; voteOnly = command?.data.voteonly; } return { command, cooldownDuration, voteOnly }; } function setCooldown(kakashi, key, duration) { const expirationTime = Date.now() + duration * 1000; kakashi.cooldowns.set(key, expirationTime); setTimeout(() => { kakashi.cooldowns.delete(key); }, duration * 1000); } function checkTopGG(kakashi, commandName) { if (kakashi.vote && !kakashi.topgg) { console.error(`Error: The ${commandName} command has vote enabled but the connection to TopGG has not been set`); return false; } return true; } async function sendVoteEmbed(interaction, kakashi) { let voteEmbed; let row; let emoji; let demoji; if (kakashi.support.button.emoji) { emoji = kakashi.support.button.emoji } else { emoji = '1158760255476486225' } if (kakashi.vote.button.emoji) { demoji = kakashi.vote.button.emoji } else { demoji = '1230589784733257789' } const voteButton = new ButtonBuilder() .setLabel('Vote To Unlock') .setStyle(ButtonStyle.Link) .setEmoji(`${demoji}`) .setURL(kakashi.vote.url); const supportButton = new ButtonBuilder() .setLabel('Support Server') .setStyle(ButtonStyle.Link) .setEmoji(`${emoji}`) .setURL(`${kakashi.support.server}`); let eemoji if (kakashi.vote.embed.emoji) { eemoji = kakashi.vote.embed.emoji } else { eemoji = `<:error:1230860807252873306>` } const description = kakashi.vote.embed.message || `${eemoji} **Vote:** Please vote first before using this command`; if (kakashi.support.button.enabled) { row = new ActionRowBuilder().addComponents(voteButton, supportButton); } else { row = new ActionRowBuilder().addComponents(voteButton); } if (kakashi.vote.embed && kakashi.vote.button) { voteEmbed = new EmbedBuilder() .setColor('Red') .setFooter({ text: 'Note: Don\'t worry, the vote will help us and it\'s free', iconURL: 'https://cdn.discordapp.com/emojis/1159095895640522822.gif' }) .setDescription(description); return interaction.reply({ embeds: [voteEmbed], components: [row], ephemeral: true, }); } else if (kakashi.vote.embed.enabled) { const voteEmbed = new EmbedBuilder() .setColor('Red') .setFooter({ text: 'Note: Don\'t worry, the vote will help us and it\'s free', iconURL: 'https://cdn.discordapp.com/emojis/1159095895640522822.gif' }) .setDescription(description); return interaction.reply({ embeds: [voteEmbed], ephemeral: true, }); } else if (kakashi.vote.button) { return interaction.reply({ content: description, components: [row], ephemeral: true, }); } else { return interaction.reply({ content: description, ephemeral: true, }); } } async function sendCooldownEmbed(interaction, kakashi, userCooldown) { const remainingTime = (userCooldown - Date.now()) / 1000; let m; let d = kakashi.cooldown.embed.message if (d && d.includes('remainingtime')) { m = d.replace('remainingtime', remainingTime.toFixed(1)) } else { m = d; } const description = m || `<:error:1230860807252873306> **Cooldown:** You are on cooldown please wait ${remainingTime.toFixed(1)} seconds`; let embed; if (kakashi.cooldown.embed.enabled) { embed = new EmbedBuilder() .setDescription(`${description}`) .setColor('Red') } if (kakashi.cooldown.embed.enabled && kakashi.cooldown.embed.footer) { embed.setFooter({ text: `Info : The cooldown is for preventing spam usage of commands/buttons`, iconURL: 'https://images-ext-1.discordapp.net/external/cpRhajDvGdN-7vpA4v_auFKPbBX9emkfZw0F1i3MLl4/https/cdn.discordapp.com/emojis/1159095895640522822.gif' }) } if (kakashi.cooldown.embed.enabled) { if (kakashi.cooldown.button) { if (kakashi.support.button.emoji) { emoji = kakashi.support.button.emoji } else { emoji = '1158760255476486225' } const supportButton = new ButtonBuilder() .setLabel('Support Server') .setStyle(ButtonStyle.Link) .setEmoji(`${emoji}`) .setURL(`${kakashi.support.server}`); const row = new ActionRowBuilder().addComponents(supportButton); return interaction.reply({ embeds: [embed], components: [row], ephemeral: true }) } else { return interaction.reply({ embeds: [embed], ephemeral: true }) } } else { if (kakashi.support.button) { if (kakashi.support.button.emoji) { emoji = kakashi.support.button.emoji } else { emoji = '1158760255476486225' } const supportButton = new ButtonBuilder() .setLabel('Support Server') .setStyle(ButtonStyle.Link) .setEmoji(`${emoji}`) .setURL(`${kakashi.support.server}`); const row = new ActionRowBuilder().addComponents(supportButton); return interaction.reply({ content: `${description}`, components: [row], ephemeral: true }) } else { return interaction.reply({ content: `${description}`, ephemeral: true }) } } } async function sendTopGGError(interaction) { return interaction.reply({ content: `Something went wrong with topgg. If you are not an admin, please contact them.`, ephemeral: true }); } async function executeCommand(command, interaction, kakashi) { try { await command.execute(interaction, kakashi); } catch (error) { console.error('Error executing command:', error); await interaction.reply({ content: 'Something went wrong while executing this command.', ephemeral: true, }); } } function handleError(error) { console.error('Error replying to interaction:', error); } module.exports = handleInteraction;