From 1b9a78430f18e46f13dd8541f08e94d2c77ee1f9 Mon Sep 17 00:00:00 2001 From: ForGetFulSkyBro Date: Sat, 6 Jul 2024 09:13:02 -0400 Subject: [PATCH] remove: try catch --- src/buttons/index.ts | 224 +++++++++++++++++++++---------------------- 1 file changed, 110 insertions(+), 114 deletions(-) diff --git a/src/buttons/index.ts b/src/buttons/index.ts index 7720d904..fd71f561 100644 --- a/src/buttons/index.ts +++ b/src/buttons/index.ts @@ -8,79 +8,124 @@ import WouldYou from "../util/wouldYou"; const buttonInteractionEvent: Event = { event: "interactionCreate", execute: async (client: WouldYou, interaction: ButtonInteraction) => { - try { - console.log( - `[INFO] INTERACTION ${interaction.id} RUN BY (${interaction.user.id}, ${interaction.user.username}) CLICKED ${interaction.customId}`, - ); + console.log( + `[INFO] INTERACTION ${interaction.id} RUN BY (${interaction.user.id}, ${interaction.user.username}) CLICKED ${interaction.customId}`, + ); - let guildDb; + let guildDb; - if (interaction.guildId !== null) { - guildDb = await client.database.getGuild(interaction.guildId, true); - } - let button = client.buttons.get(interaction.customId); - - const customId = interaction.customId.split("_"); - - if (customId[3]) { - if ( - customId[2] === "neverhaveiever" || - customId[2] === "wouldyourather" - ) { - const action = Number(customId[3]) == 0 ? "yes" : "no"; - const buttonName = customId[2]; - const fieldName = `${buttonName}.${action}`; - // Increment the specified field using $inc - await UserModel.updateOne( - { userID: interaction.user?.id }, - { $inc: { [fieldName]: 1 } }, - ); - } - } + if (interaction.guildId !== null) { + guildDb = await client.database.getGuild(interaction.guildId, true); + } + let button = client.buttons.get(interaction.customId); - const replyMap: Record = { - wouldyourather: "wouldyourather.used.replay", - neverhaveiever: "neverhaveiever.used.replay", - higherlower: "higherlower.used.replay", - wwyd: "whatwouldyoudo.used.replay", - truth: "truth.used.replay", - dare: "dare.used.replay", - random: "random.used.replay", - }; - - // Get the field path based on the command name - const fieldPath = replyMap[interaction.customId]; - if (fieldPath) { + const customId = interaction.customId.split("_"); + + if (customId[3]) { + if ( + customId[2] === "neverhaveiever" || + customId[2] === "wouldyourather" + ) { + const action = Number(customId[3]) == 0 ? "yes" : "no"; + const buttonName = customId[2]; + const fieldName = `${buttonName}.${action}`; // Increment the specified field using $inc await UserModel.updateOne( - { userID: interaction.user?.id }, // Specify the query to find the user - { $inc: { [fieldPath]: 1 } }, // Use computed fieldPath + { userID: interaction.user?.id }, + { $inc: { [fieldName]: 1 } }, ); } + } - // Button identification logic - let bypass = false; - const buttonMap: Record = { - wycustom_add: "wycustom_add", - wycustom_remove: "wycustom_remove", - voting_: "voting", - result_: "result", - higher_: "higher", - lower_: "lower", - }; - - for (const [key, value] of Object.entries(buttonMap)) { - if (interaction.customId.startsWith(key)) { - bypass = true; - button = client.buttons.get(value); - break; - } + const replyMap: Record = { + wouldyourather: "wouldyourather.used.replay", + neverhaveiever: "neverhaveiever.used.replay", + higherlower: "higherlower.used.replay", + wwyd: "whatwouldyoudo.used.replay", + truth: "truth.used.replay", + dare: "dare.used.replay", + random: "random.used.replay", + }; + + // Get the field path based on the command name + const fieldPath = replyMap[interaction.customId]; + if (fieldPath) { + // Increment the specified field using $inc + await UserModel.updateOne( + { userID: interaction.user?.id }, // Specify the query to find the user + { $inc: { [fieldPath]: 1 } }, // Use computed fieldPath + ); + } + + // Button identification logic + let bypass = false; + const buttonMap: Record = { + wycustom_add: "wycustom_add", + wycustom_remove: "wycustom_remove", + voting_: "voting", + result_: "result", + higher_: "higher", + lower_: "lower", + }; + + for (const [key, value] of Object.entries(buttonMap)) { + if (interaction.customId.startsWith(key)) { + bypass = true; + button = client.buttons.get(value); + break; } + } + + if (!button) { + interaction + .reply({ + content: "Please use the command again.", + ephemeral: true, + }) + .catch((err) => { + captureException(err); + }); + return; + } + + // Cooldown handling + let cooldownKey: string | undefined; + let cooldown: number; + + const excludedButtons = await client.buttons + .filter((button) => button.cooldown === false) + .map((button) => button.name); + + const isExcludedButton = excludedButtons.includes(interaction.customId); + if ( + guildDb != null && + !bypass && + guildDb.replayType === "Channels" && + guildDb.replayChannels.find((x) => x.id === interaction.channelId) && + !isExcludedButton + ) { + cooldownKey = `${interaction.user?.id}-${interaction.channelId}`; + cooldown = Number( + guildDb.replayChannels.find((x) => x.id === interaction.channelId) + ?.cooldown, + ); + } else if (!isExcludedButton && !bypass) { + cooldownKey = interaction.user?.id; + cooldown = Number( + guildDb?.replayCooldown != null ? guildDb.replayCooldown : 0, + ); + } - if (!button) { + if (cooldownKey && cooldown!) { + if ( + client.used.has(cooldownKey) && + client.used.get(cooldownKey)! > Date.now() + ) { interaction .reply({ - content: "Please use the command again.", + content: `You can use this button again !`, ephemeral: true, }) .catch((err) => { @@ -88,60 +133,11 @@ const buttonInteractionEvent: Event = { }); return; } - - // Cooldown handling - let cooldownKey: string | undefined; - let cooldown: number; - - const excludedButtons = await client.buttons - .filter((button) => button.cooldown === false) - .map((button) => button.name); - - const isExcludedButton = excludedButtons.includes(interaction.customId); - if ( - guildDb != null && - !bypass && - guildDb.replayType === "Channels" && - guildDb.replayChannels.find((x) => x.id === interaction.channelId) && - !isExcludedButton - ) { - cooldownKey = `${interaction.user?.id}-${interaction.channelId}`; - cooldown = Number( - guildDb.replayChannels.find((x) => x.id === interaction.channelId) - ?.cooldown, - ); - } else if (!isExcludedButton && !bypass) { - cooldownKey = interaction.user?.id; - cooldown = Number( - guildDb?.replayCooldown != null ? guildDb.replayCooldown : 0, - ); - } - - if (cooldownKey && cooldown!) { - if ( - client.used.has(cooldownKey) && - client.used.get(cooldownKey)! > Date.now() - ) { - interaction - .reply({ - content: `You can use this button again !`, - ephemeral: true, - }) - .catch((err) => { - captureException(err); - }); - return; - } - client.used.set(cooldownKey, Date.now() + cooldown); - setTimeout(() => client.used.delete(cooldownKey!), cooldown); - } - - return button.execute(interaction, client, guildDb as IGuildModel); - } catch (e) { - console.log(e); + client.used.set(cooldownKey, Date.now() + cooldown); + setTimeout(() => client.used.delete(cooldownKey!), cooldown); } + + return button.execute(interaction, client, guildDb as IGuildModel); }, };