🧑‍💻 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) => {
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()}`

View file

@ -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);

View file

@ -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()}`);
};

View file

@ -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;
});
},
};

View file

@ -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.");
});
},
};

View file

@ -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;
});
},
};