🧑‍💻 Fixed some more code smells

This commit is contained in:
Axel Olausson Holtenäs 2022-10-16 15:17:13 +02:00
parent 30e992b5b9
commit 8e260391f6
6 changed files with 39 additions and 112 deletions

View file

@ -13,7 +13,8 @@ export const builder = new SlashCommandBuilder()
export const execute = async (interaction: ChatInputCommandInteraction) => { export const execute = async (interaction: ChatInputCommandInteraction) => {
switch (interaction.options.getSubcommand()) { switch (interaction.options.getSubcommand()) {
case "lookup": case "lookup":
return modules.lookup.execute(interaction); await modules.lookup.execute(interaction);
break;
default: default:
throw new Error( throw new Error(
`Unknown subcommand: ${interaction.options.getSubcommand()}` `Unknown subcommand: ${interaction.options.getSubcommand()}`

View file

@ -49,7 +49,8 @@ export default {
}) })
.setColor(embedConfig.successColor); .setColor(embedConfig.successColor);
return interaction.editReply({ embeds: [embed] }); await interaction.editReply({ embeds: [embed] });
return;
}) })
.catch((error) => { .catch((error) => {
throw new Error(error.message); throw new Error(error.message);

View file

@ -2,8 +2,6 @@
import { SlashCommandSubcommandGroupBuilder } from "@discordjs/builders"; import { SlashCommandSubcommandGroupBuilder } from "@discordjs/builders";
import { ChatInputCommandInteraction } from "discord.js"; import { ChatInputCommandInteraction } from "discord.js";
import logger from "../../../../../middlewares/logger";
// Modules // Modules
import modules from "./modules"; import modules from "./modules";
@ -21,17 +19,17 @@ export const builder = (group: SlashCommandSubcommandGroupBuilder) => {
export const execute = async (interaction: ChatInputCommandInteraction) => { export const execute = async (interaction: ChatInputCommandInteraction) => {
const { options } = interaction; const { options } = interaction;
if (options?.getSubcommand() === "add") { switch (options.getSubcommand()) {
logger?.silly(`Executing create subcommand`); case "add": {
await modules.add.execute(interaction);
return 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()}`);
}; };

View file

@ -8,7 +8,6 @@ import {
} from "discord.js"; } from "discord.js";
// Configurations // Configurations
import getEmbedConfig from "../../../../../../../helpers/getEmbedData"; import getEmbedConfig from "../../../../../../../helpers/getEmbedData";
import logger from "../../../../../../../middlewares/logger";
// Models // Models
import counterSchema from "../../../../../../../models/counter"; import counterSchema from "../../../../../../../models/counter";
@ -80,9 +79,7 @@ export default {
counter: startValue || 0, counter: startValue || 0,
}) })
.then(async () => { .then(async () => {
logger?.silly(`Created counter`); await interaction?.editReply({
return interaction?.editReply({
embeds: [ embeds: [
embed embed
.setDescription( .setDescription(
@ -91,6 +88,7 @@ export default {
.setColor(successColor), .setColor(successColor),
], ],
}); });
return;
}); });
}, },
}; };

View file

@ -34,8 +34,9 @@ export default {
); );
}, },
execute: async (interaction: ChatInputCommandInteraction) => { execute: async (interaction: ChatInputCommandInteraction) => {
const { errorColor, successColor, footerText, footerIcon } = const { successColor, footerText, footerIcon } = await getEmbedConfig(
await getEmbedConfig(interaction.guild); interaction.guild
);
const { options, guild } = interaction; const { options, guild } = interaction;
const discordChannel = options?.getChannel("channel"); const discordChannel = options?.getChannel("channel");
@ -51,17 +52,9 @@ export default {
}); });
if (counter === null) { if (counter === null) {
logger?.silly(`Counter is null`); throw new Error(
"There is no counters in this channel, please add one first."
return interaction?.editReply({ );
embeds: [
embed
.setDescription(
":x: There is no counter in this channel. Please add a counter first."
)
.setColor(errorColor),
],
});
} }
await counterSchema await counterSchema
@ -72,7 +65,7 @@ export default {
?.then(async () => { ?.then(async () => {
logger?.silly(`Counter deleted`); logger?.silly(`Counter deleted`);
return interaction?.editReply({ await interaction?.editReply({
embeds: [ embeds: [
embed embed
.setDescription( .setDescription(
@ -81,9 +74,10 @@ export default {
.setColor(successColor), .setColor(successColor),
], ],
}); });
return;
}) })
.catch(async (error) => { .catch(() => {
logger?.error(`Error deleting counter: ${error}`); throw new Error("Failed deleting counter from database.");
}); });
}, },
}; };

View file

@ -12,7 +12,6 @@ import pluralize from "../../../../../../../helpers/pluralize";
// Models // Models
import fetchUser from "../../../../../../../helpers/userData"; import fetchUser from "../../../../../../../helpers/userData";
// Handlers // Handlers
import logger from "../../../../../../../middlewares/logger";
// Function // Function
export default { export default {
@ -40,8 +39,9 @@ export default {
); );
}, },
execute: async (interaction: ChatInputCommandInteraction) => { execute: async (interaction: ChatInputCommandInteraction) => {
const { errorColor, successColor, footerText, footerIcon } = const { successColor, footerText, footerIcon } = await getEmbedConfig(
await getEmbedConfig(interaction.guild); // Destructure interaction.guild
); // Destructure
const { guild, options } = interaction; const { guild, options } = interaction;
const discordReceiver = options?.getUser("user"); const discordReceiver = options?.getUser("user");
@ -49,93 +49,29 @@ export default {
// If amount option is null // If amount option is null
if (creditAmount === null) { if (creditAmount === null) {
logger?.silly(`Amount is null`); throw new Error("You need to provide a credit amount.");
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 }),
],
});
} }
// If amount is zero or below // If amount is zero or below
if (creditAmount <= 0) { if (creditAmount <= 0) {
logger?.silly(`Amount is zero or below`); throw new Error("You must provide a credit amount greater than zero");
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 }),
],
});
} }
if (discordReceiver === null) { if (discordReceiver === null) {
logger?.silly(`Discord receiver is null`); throw new Error("We could not get the receiving user from Discord");
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 }),
],
});
} }
if (guild === null) { if (guild === null) {
logger?.silly(`Guild is null`); throw new Error("We could not get the current guild from discord.");
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 }),
],
});
} }
const toUser = await fetchUser(discordReceiver, guild); const toUser = await fetchUser(discordReceiver, guild);
if (toUser === null) { if (toUser === null) {
logger?.silly(`To user is null`); throw new Error("The receiving user is not found.");
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 }),
],
});
} }
if (toUser?.credits === null) { if (toUser?.credits === null) {
return interaction?.editReply({ throw new Error("The receiving user's credits value could not found.");
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 }),
],
});
} }
// Deposit amount to toUser // Deposit amount to toUser
@ -143,9 +79,7 @@ export default {
// Save toUser // Save toUser
await toUser?.save()?.then(async () => { await toUser?.save()?.then(async () => {
logger?.silly(`Saved toUser`); await interaction?.editReply({
return interaction?.editReply({
embeds: [ embeds: [
new EmbedBuilder() new EmbedBuilder()
.setTitle("[:toolbox:] Manage - Credits (Give)") .setTitle("[:toolbox:] Manage - Credits (Give)")
@ -157,6 +91,7 @@ export default {
.setFooter({ text: footerText, iconURL: footerIcon }), .setFooter({ text: footerText, iconURL: footerIcon }),
], ],
}); });
return;
}); });
}, },
}; };