From 6a5d4804385897233c65daa57b54ea06f09db000 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Sun, 16 Oct 2022 14:50:59 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Fixed=20som?= =?UTF-8?q?e=20more=20code=20smells?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/commands/manage/index.ts | 18 +++++++---- .../commands/shop/modules/cpgg/index.ts | 32 +++---------------- .../shop/modules/roles/modules/buy/index.ts | 6 ++-- .../messageCreate/modules/counters/index.ts | 8 ++--- 4 files changed, 22 insertions(+), 42 deletions(-) diff --git a/src/plugins/commands/manage/index.ts b/src/plugins/commands/manage/index.ts index 01b6508..9de5997 100644 --- a/src/plugins/commands/manage/index.ts +++ b/src/plugins/commands/manage/index.ts @@ -18,11 +18,17 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { // Destructure const { options } = interaction; - if (options?.getSubcommandGroup() === "credits") { - return modules.credits.execute(interaction); - } - - if (options?.getSubcommandGroup() === "counters") { - return modules.counters.execute(interaction); + switch (options.getSubcommandGroup()) { + case "credits": { + await modules.credits.execute(interaction); + break; + } + case "counters": { + await modules.counters.execute(interaction); + break; + } + default: { + throw new Error("Could not find an module for the command."); + } } }; diff --git a/src/plugins/commands/shop/modules/cpgg/index.ts b/src/plugins/commands/shop/modules/cpgg/index.ts index 124d12b..da45af1 100644 --- a/src/plugins/commands/shop/modules/cpgg/index.ts +++ b/src/plugins/commands/shop/modules/cpgg/index.ts @@ -6,7 +6,7 @@ import { ButtonStyle, ChatInputCommandInteraction, EmbedBuilder, - Message, + Message } from "discord.js"; import { v4 as uuidv4 } from "uuid"; import encryption from "../../../../../helpers/encryption"; @@ -206,35 +206,13 @@ export default { }); }) - .catch(async (error) => { - logger?.silly(`Error saving new credits. - ${error}`); - - const interactionEmbed = new EmbedBuilder() - .setTitle("[:shopping_cart:] CPGG") - .setDescription(`Something went wrong.`) - .setTimestamp() - .setColor(errorColor) - .setFooter({ text: footerText, iconURL: footerIcon }); - - return interaction?.editReply({ - embeds: [interactionEmbed], - }); + .catch(() => { + throw new Error("Failed to update credits for user.") }); }) - .catch(async (error) => { - logger?.silly(`Error creating voucher. - ${error}`); - - const interactionEmbed = new EmbedBuilder() - .setTitle("[:shopping_cart:] CPGG") - .setDescription(`Something went wrong.`) - .setTimestamp() - .setColor(errorColor) - .setFooter({ text: footerText, iconURL: footerIcon }); - - return interaction?.editReply({ - embeds: [interactionEmbed], - }); + .catch(() => { + throw new Error("Failed generating an voucher.") }); }, }; diff --git a/src/plugins/commands/shop/modules/roles/modules/buy/index.ts b/src/plugins/commands/shop/modules/roles/modules/buy/index.ts index 194ccb7..22ac40f 100644 --- a/src/plugins/commands/shop/modules/roles/modules/buy/index.ts +++ b/src/plugins/commands/shop/modules/roles/modules/buy/index.ts @@ -5,7 +5,7 @@ import { ChatInputCommandInteraction, ColorResolvable, EmbedBuilder, - GuildMemberRoleManager, + GuildMemberRoleManager } from "discord.js"; // Configurations import getEmbedConfig from "../../../../../../../helpers/getEmbedData"; @@ -110,8 +110,8 @@ export default { embeds: [interactionEmbed], }); }) - .catch(async (error) => { - return logger?.silly(`Role could not be created. ${error}`); + .catch(() => { + throw new Error("Failed creating role.") }); }, }; diff --git a/src/plugins/events/messageCreate/modules/counters/index.ts b/src/plugins/events/messageCreate/modules/counters/index.ts index 35695c0..341d0c0 100644 --- a/src/plugins/events/messageCreate/modules/counters/index.ts +++ b/src/plugins/events/messageCreate/modules/counters/index.ts @@ -1,7 +1,6 @@ import { ChannelType, Message } from "discord.js"; import logger from "../../../../../middlewares/logger"; import counterSchema from "../../../../../models/counter"; -import logger from "../../../../../middlewares/logger"; export default { execute: async (message: Message) => { @@ -23,10 +22,7 @@ export default { }); if (counter === null) { - logger.silly( - `No counter found for guild ${guildId} and channel ${channelId}` - ); - return; + throw new Error("No counter found in database."); } if ( @@ -57,7 +53,7 @@ export default { `Counter for guild ${guildId} and channel ${channelId} is now ${counter.counter}` ); }) - .catch((err) => { + .catch(() => { throw new Error(`Error saving counter to database.`); });