From 8e260391f669ea72ed5c3bacac1778c9aaaab079 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Sun, 16 Oct 2022 15:17:13 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Fixed?= =?UTF-8?q?=20some=20more=20code=20smells?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/commands/dns/index.ts | 3 +- .../commands/fun/modules/meme/index.ts | 3 +- .../commands/manage/modules/counters/index.ts | 26 +++--- .../modules/counters/modules/add/index.ts | 6 +- .../modules/counters/modules/remove/index.ts | 26 +++--- .../modules/credits/modules/give/index.ts | 87 +++---------------- 6 files changed, 39 insertions(+), 112 deletions(-) diff --git a/src/plugins/commands/dns/index.ts b/src/plugins/commands/dns/index.ts index abfb718..6b88d04 100644 --- a/src/plugins/commands/dns/index.ts +++ b/src/plugins/commands/dns/index.ts @@ -13,7 +13,8 @@ export const builder = new SlashCommandBuilder() export const execute = async (interaction: ChatInputCommandInteraction) => { switch (interaction.options.getSubcommand()) { case "lookup": - return modules.lookup.execute(interaction); + await modules.lookup.execute(interaction); + break; default: throw new Error( `Unknown subcommand: ${interaction.options.getSubcommand()}` diff --git a/src/plugins/commands/fun/modules/meme/index.ts b/src/plugins/commands/fun/modules/meme/index.ts index 9709014..452f900 100644 --- a/src/plugins/commands/fun/modules/meme/index.ts +++ b/src/plugins/commands/fun/modules/meme/index.ts @@ -49,7 +49,8 @@ export default { }) .setColor(embedConfig.successColor); - return interaction.editReply({ embeds: [embed] }); + await interaction.editReply({ embeds: [embed] }); + return; }) .catch((error) => { throw new Error(error.message); diff --git a/src/plugins/commands/manage/modules/counters/index.ts b/src/plugins/commands/manage/modules/counters/index.ts index 04580e5..2c19d7d 100644 --- a/src/plugins/commands/manage/modules/counters/index.ts +++ b/src/plugins/commands/manage/modules/counters/index.ts @@ -2,8 +2,6 @@ import { SlashCommandSubcommandGroupBuilder } from "@discordjs/builders"; import { ChatInputCommandInteraction } from "discord.js"; -import logger from "../../../../../middlewares/logger"; - // Modules import modules from "./modules"; @@ -21,17 +19,17 @@ export const builder = (group: SlashCommandSubcommandGroupBuilder) => { export const execute = async (interaction: ChatInputCommandInteraction) => { const { options } = interaction; - if (options?.getSubcommand() === "add") { - logger?.silly(`Executing create subcommand`); - - return modules.add.execute(interaction); + switch (options.getSubcommand()) { + case "add": { + await modules.add.execute(interaction); + break; + } + case "remove": { + await modules.remove.execute(interaction); + break; + } + default: { + throw new Error("Could not found a module for that command."); + } } - - if (options?.getSubcommand() === "remove") { - logger?.silly(`Executing delete subcommand`); - - return modules.remove.execute(interaction); - } - - logger?.silly(`Unknown subcommand ${options?.getSubcommand()}`); }; diff --git a/src/plugins/commands/manage/modules/counters/modules/add/index.ts b/src/plugins/commands/manage/modules/counters/modules/add/index.ts index f73d6e5..0be235c 100644 --- a/src/plugins/commands/manage/modules/counters/modules/add/index.ts +++ b/src/plugins/commands/manage/modules/counters/modules/add/index.ts @@ -8,7 +8,6 @@ import { } from "discord.js"; // Configurations import getEmbedConfig from "../../../../../../../helpers/getEmbedData"; -import logger from "../../../../../../../middlewares/logger"; // Models import counterSchema from "../../../../../../../models/counter"; @@ -80,9 +79,7 @@ export default { counter: startValue || 0, }) .then(async () => { - logger?.silly(`Created counter`); - - return interaction?.editReply({ + await interaction?.editReply({ embeds: [ embed .setDescription( @@ -91,6 +88,7 @@ export default { .setColor(successColor), ], }); + return; }); }, }; diff --git a/src/plugins/commands/manage/modules/counters/modules/remove/index.ts b/src/plugins/commands/manage/modules/counters/modules/remove/index.ts index 8b21059..4a14d34 100644 --- a/src/plugins/commands/manage/modules/counters/modules/remove/index.ts +++ b/src/plugins/commands/manage/modules/counters/modules/remove/index.ts @@ -34,8 +34,9 @@ export default { ); }, execute: async (interaction: ChatInputCommandInteraction) => { - const { errorColor, successColor, footerText, footerIcon } = - await getEmbedConfig(interaction.guild); + const { successColor, footerText, footerIcon } = await getEmbedConfig( + interaction.guild + ); const { options, guild } = interaction; const discordChannel = options?.getChannel("channel"); @@ -51,17 +52,9 @@ export default { }); if (counter === null) { - logger?.silly(`Counter is null`); - - return interaction?.editReply({ - embeds: [ - embed - .setDescription( - ":x: There is no counter in this channel. Please add a counter first." - ) - .setColor(errorColor), - ], - }); + throw new Error( + "There is no counters in this channel, please add one first." + ); } await counterSchema @@ -72,7 +65,7 @@ export default { ?.then(async () => { logger?.silly(`Counter deleted`); - return interaction?.editReply({ + await interaction?.editReply({ embeds: [ embed .setDescription( @@ -81,9 +74,10 @@ export default { .setColor(successColor), ], }); + return; }) - .catch(async (error) => { - logger?.error(`Error deleting counter: ${error}`); + .catch(() => { + throw new Error("Failed deleting counter from database."); }); }, }; diff --git a/src/plugins/commands/manage/modules/credits/modules/give/index.ts b/src/plugins/commands/manage/modules/credits/modules/give/index.ts index c59bb5a..cab7050 100644 --- a/src/plugins/commands/manage/modules/credits/modules/give/index.ts +++ b/src/plugins/commands/manage/modules/credits/modules/give/index.ts @@ -12,7 +12,6 @@ import pluralize from "../../../../../../../helpers/pluralize"; // Models import fetchUser from "../../../../../../../helpers/userData"; // Handlers -import logger from "../../../../../../../middlewares/logger"; // Function export default { @@ -40,8 +39,9 @@ export default { ); }, execute: async (interaction: ChatInputCommandInteraction) => { - const { errorColor, successColor, footerText, footerIcon } = - await getEmbedConfig(interaction.guild); // Destructure + const { successColor, footerText, footerIcon } = await getEmbedConfig( + interaction.guild + ); // Destructure const { guild, options } = interaction; const discordReceiver = options?.getUser("user"); @@ -49,93 +49,29 @@ export default { // If amount option is null if (creditAmount === null) { - logger?.silly(`Amount is null`); - - return interaction?.editReply({ - embeds: [ - new EmbedBuilder() - .setTitle("[:toolbox:] Manage - Credits (Give)") - .setDescription(`You must provide an amount.`) - .setTimestamp(new Date()) - .setColor(errorColor) - .setFooter({ text: footerText, iconURL: footerIcon }), - ], - }); + throw new Error("You need to provide a credit amount."); } // If amount is zero or below if (creditAmount <= 0) { - logger?.silly(`Amount is zero or below`); - - return interaction?.editReply({ - embeds: [ - new EmbedBuilder() - .setTitle("[:toolbox:] Manage - Credits (Give)") - .setDescription(`You must provide an amount greater than zero.`) - .setTimestamp(new Date()) - .setColor(errorColor) - .setFooter({ text: footerText, iconURL: footerIcon }), - ], - }); + throw new Error("You must provide a credit amount greater than zero"); } if (discordReceiver === null) { - logger?.silly(`Discord receiver is null`); - - return interaction?.editReply({ - embeds: [ - new EmbedBuilder() - .setTitle("[:toolbox:] Manage - Credits (Give)") - .setDescription(`You must provide a user.`) - .setTimestamp(new Date()) - .setColor(errorColor) - .setFooter({ text: footerText, iconURL: footerIcon }), - ], - }); + throw new Error("We could not get the receiving user from Discord"); } if (guild === null) { - logger?.silly(`Guild is null`); - - return interaction?.editReply({ - embeds: [ - new EmbedBuilder() - .setTitle("[:toolbox:] Manage - Credits (Give)") - .setDescription(`You must be in a guild.`) - .setTimestamp(new Date()) - .setColor(errorColor) - .setFooter({ text: footerText, iconURL: footerIcon }), - ], - }); + throw new Error("We could not get the current guild from discord."); } const toUser = await fetchUser(discordReceiver, guild); if (toUser === null) { - logger?.silly(`To user is null`); - - return interaction?.editReply({ - embeds: [ - new EmbedBuilder() - .setTitle("[:toolbox:] Manage - Credits (Give)") - .setDescription(`The user you provided could not be found.`) - .setTimestamp(new Date()) - .setColor(errorColor) - .setFooter({ text: footerText, iconURL: footerIcon }), - ], - }); + throw new Error("The receiving user is not found."); } if (toUser?.credits === null) { - return interaction?.editReply({ - embeds: [ - new EmbedBuilder() - .setTitle("[:toolbox:] Manage - Credits (Give)") - .setDescription(`The user you provided does not have any credits.`) - .setTimestamp(new Date()) - .setColor(errorColor) - .setFooter({ text: footerText, iconURL: footerIcon }), - ], - }); + throw new Error("The receiving user's credits value could not found."); } // Deposit amount to toUser @@ -143,9 +79,7 @@ export default { // Save toUser await toUser?.save()?.then(async () => { - logger?.silly(`Saved toUser`); - - return interaction?.editReply({ + await interaction?.editReply({ embeds: [ new EmbedBuilder() .setTitle("[:toolbox:] Manage - Credits (Give)") @@ -157,6 +91,7 @@ export default { .setFooter({ text: footerText, iconURL: footerIcon }), ], }); + return; }); }, }; From 586caab1079594ca801c5d2eff153e465173ed47 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Sun, 16 Oct 2022 15:19:39 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=92=A1=20Added=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/commands/dns/index.ts | 1 + src/plugins/commands/manage/modules/counters/index.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/plugins/commands/dns/index.ts b/src/plugins/commands/dns/index.ts index 6b88d04..551814b 100644 --- a/src/plugins/commands/dns/index.ts +++ b/src/plugins/commands/dns/index.ts @@ -10,6 +10,7 @@ export const builder = new SlashCommandBuilder() .addSubcommand(modules.lookup.builder); +// Execute the command export const execute = async (interaction: ChatInputCommandInteraction) => { switch (interaction.options.getSubcommand()) { case "lookup": diff --git a/src/plugins/commands/manage/modules/counters/index.ts b/src/plugins/commands/manage/modules/counters/index.ts index 2c19d7d..941d672 100644 --- a/src/plugins/commands/manage/modules/counters/index.ts +++ b/src/plugins/commands/manage/modules/counters/index.ts @@ -8,6 +8,7 @@ import modules from "./modules"; // Function export const moduleData = modules; +// Create a discord builder export const builder = (group: SlashCommandSubcommandGroupBuilder) => { return group .setName("counters") @@ -16,6 +17,7 @@ export const builder = (group: SlashCommandSubcommandGroupBuilder) => { .addSubcommand(modules.remove.builder); }; +// Execute the command export const execute = async (interaction: ChatInputCommandInteraction) => { const { options } = interaction;