diff --git a/src/commands/manage/groups/counters/modules/create.ts b/src/commands/manage/groups/counters/modules/create.ts index ef18141..5a4b161 100644 --- a/src/commands/manage/groups/counters/modules/create.ts +++ b/src/commands/manage/groups/counters/modules/create.ts @@ -53,7 +53,7 @@ export default { return interaction?.editReply({ embeds: [ new MessageEmbed() - .setTitle("[:toolbox:] Admin - Counters (Create)") + .setTitle("[:toolbox:] Manage - Counters (Create)") .setDescription( `${discordChannel} is already a counting channel, currently it's counting ${counter.word}!` ) @@ -79,7 +79,7 @@ export default { return interaction?.editReply({ embeds: [ new MessageEmbed() - .setTitle("[:toolbox:] Admin - Counters (Create)") + .setTitle("[:toolbox:] Manage - Counters (Create)") .setDescription( `${discordChannel} is now counting when hearing word ${countingWord} and it starts at number ${ startValue || 0 diff --git a/src/commands/manage/groups/counters/modules/delete.ts b/src/commands/manage/groups/counters/modules/delete.ts index b61fe5f..9c1978b 100644 --- a/src/commands/manage/groups/counters/modules/delete.ts +++ b/src/commands/manage/groups/counters/modules/delete.ts @@ -40,7 +40,7 @@ export default { return interaction?.editReply({ embeds: [ new MessageEmbed() - .setTitle("[:toolbox:] Admin - Counters (Delete)") + .setTitle("[:toolbox:] Manage - Counters (Delete)") .setDescription(`${discordChannel} is not a counting channel!`) .setTimestamp(new Date()) .setColor(colors?.error as ColorResolvable) @@ -58,7 +58,7 @@ export default { return interaction?.editReply({ embeds: [ new MessageEmbed() - .setTitle("[:toolbox:] Admin - Counters (Delete)") + .setTitle("[:toolbox:] Manage - Counters (Delete)") .setDescription( `${discordChannel} is no longer an counting channel.` ) diff --git a/src/commands/manage/groups/credits/index.ts b/src/commands/manage/groups/credits/index.ts index a92ea84..024d2fc 100644 --- a/src/commands/manage/groups/credits/index.ts +++ b/src/commands/manage/groups/credits/index.ts @@ -3,91 +3,21 @@ import { CommandInteraction } from "discord.js"; import { SlashCommandSubcommandGroupBuilder } from "@discordjs/builders"; // Modules -import give from "./modules/give"; -import take from "./modules/take"; -import set from "./modules/set"; -import transfer from "./modules/transfer"; +import moduleGive from "./modules/give"; +import moduleSet from "./modules/set"; +import moduleTake from "./modules/take"; +import moduleTransfer from "./modules/transfer"; // Function export default { data: (group: SlashCommandSubcommandGroupBuilder) => { return group .setName("credits") - .setDescription("Manage credits.") - .addSubcommand((command) => - command - .setName("give") - .setDescription("Give credits to a user") - .addUserOption((option) => - option - .setName("user") - .setDescription("The user you want to pay.") - .setRequired(true) - ) - .addIntegerOption((option) => - option - .setName("amount") - .setDescription("The amount you will pay.") - .setRequired(true) - ) - ) - .addSubcommand((command) => - command - .setName("set") - .setDescription("Set credits to a user") - .addUserOption((option) => - option - .setName("user") - .setDescription("The user you want to set credits on.") - .setRequired(true) - ) - .addIntegerOption((option) => - option - .setName("amount") - .setDescription("The amount you will set.") - .setRequired(true) - ) - ) - .addSubcommand((command) => - command - .setName("take") - .setDescription("Take credits from a user") - .addUserOption((option) => - option - .setName("user") - .setDescription("The user you want to take credits from.") - .setRequired(true) - ) - .addIntegerOption((option) => - option - .setName("amount") - .setDescription("The amount you will take.") - .setRequired(true) - ) - ) - .addSubcommand((command) => - command - .setName("transfer") - .setDescription("Transfer credits from a user to another user.") - .addUserOption((option) => - option - .setName("from") - .setDescription("The user you want to take credits from.") - .setRequired(true) - ) - .addUserOption((option) => - option - .setName("to") - .setDescription("The user you want to give credits to.") - .setRequired(true) - ) - .addIntegerOption((option) => - option - .setName("amount") - .setDescription("The amount you will transfer.") - .setRequired(true) - ) - ); + .setDescription("Manage guild credits.") + .addSubcommand(moduleGive.data) + .addSubcommand(moduleSet.data) + .addSubcommand(moduleTake.data) + .addSubcommand(moduleTransfer.data); }, execute: async (interaction: CommandInteraction) => { // Destructure @@ -96,25 +26,25 @@ export default { // Module - Give if (options?.getSubcommand() === "give") { // Execute Module - Give - return give(interaction); - } - - // Module - Take - else if (options?.getSubcommand() === "take") { - // Execute Module - Take - return take(interaction); + return moduleGive.execute(interaction); } // Module - Set else if (options?.getSubcommand() === "set") { // Execute Module - Set - return set(interaction); + return moduleSet.execute(interaction); + } + + // Module - Take + else if (options?.getSubcommand() === "take") { + // Execute Module - Take + return moduleTake.execute(interaction); } // Module - Transfer else if (options?.getSubcommand() === "transfer") { // Execute Module - Transfer - return transfer(interaction); + return moduleTransfer.execute(interaction); } }, }; diff --git a/src/commands/manage/groups/credits/modules/give.ts b/src/commands/manage/groups/credits/modules/give.ts deleted file mode 100644 index ccddaf7..0000000 --- a/src/commands/manage/groups/credits/modules/give.ts +++ /dev/null @@ -1,136 +0,0 @@ -// Dependencies -import { CommandInteraction, ColorResolvable } from "discord.js"; - -// Configurations -import config from "../../../../../../config.json"; - -// Handlers -import logger from "../../../../../handlers/logger"; - -// Helpers -import pluralize from "../../../../../helpers/pluralize"; - -// Models -import fetchUser from "../../../../../helpers/fetchUser"; - -// Function -export default async (interaction: CommandInteraction) => { - // Destructure - const { guild, user, options } = interaction; - - // User option - const optionUser = options?.getUser("user"); - - // Amount option - const optionAmount = options?.getInteger("amount"); - - // If amount option is null - if (optionAmount === null) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Give]" as string, - description: "We could not read your requested amount." as string, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // If amount is zero or below - if (optionAmount <= 0) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Give]" as string, - description: "You can not give zero credits or below." as string, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - if (optionUser === null) return; - if (guild === null) return; - - // toUser Information - const toUser = await fetchUser(optionUser, guild); - - // If toUser does not exist - if (!toUser) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Give]" as string, - description: `We could not find ${optionUser} in our database.`, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // If toUser.credits does not exist - if (toUser?.credits === null) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Give]" as string, - description: `We could not find credits for ${optionUser} in our database.`, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // Deposit amount to toUser - toUser.credits += optionAmount; - - // Save toUser - await toUser?.save()?.then(async () => { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Give]" as string, - description: `We have given ${optionUser}, ${pluralize( - optionAmount, - "credit" - )}.`, - color: config?.colors?.success as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Log debug message - logger?.debug( - `Guild: ${guild?.id} User: ${user?.id} gave ${optionUser?.id} ${pluralize( - optionAmount, - "credit" - )}.` - ); - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - }); -}; diff --git a/src/commands/manage/groups/credits/modules/give/index.ts b/src/commands/manage/groups/credits/modules/give/index.ts new file mode 100644 index 0000000..a945928 --- /dev/null +++ b/src/commands/manage/groups/credits/modules/give/index.ts @@ -0,0 +1,156 @@ +// Dependencies +import { CommandInteraction, ColorResolvable, MessageEmbed } from "discord.js"; + +// Configurations +import { colors, footer } from "../../../../../../../config.json"; + +// Handlers +import logger from "../../../../../../logger"; + +// Helpers +import pluralize from "../../../../../../helpers/pluralize"; + +// Models +import fetchUser from "../../../../../../helpers/fetchUser"; +import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; + +// Function +export default { + data: (command: SlashCommandSubcommandBuilder) => { + return command + .setName("give") + .setDescription("Give credits to a user") + .addUserOption((option) => + option + .setName("user") + .setDescription("The user you want to pay.") + .setRequired(true) + ) + .addIntegerOption((option) => + option + .setName("amount") + .setDescription("The amount you will pay.") + .setRequired(true) + ); + }, + execute: async (interaction: CommandInteraction) => { + // Destructure + const { guild, user, options } = interaction; + + const discordReceiver = options?.getUser("user"); + const creditAmount = options?.getInteger("amount"); + + // If amount option is null + if (creditAmount === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Give)") + .setDescription(`We could not read your requested amount!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // If amount is zero or below + if (creditAmount <= 0) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Give)") + .setDescription(`You can not give zero credits or below!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + if (discordReceiver === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Give)") + .setDescription(`We could not read receiver user!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + if (guild === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Give)") + .setDescription(`We could not read your guild!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + const toUser = await fetchUser(discordReceiver, guild); + + if (toUser === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Give)") + .setDescription( + `We could not read your receiver user from our database!` + ) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + if (toUser?.credits === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Give)") + .setDescription( + `We could not find credits for ${discordReceiver} in our database!` + ) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // Deposit amount to toUser + toUser.credits += creditAmount; + + // Save toUser + await toUser?.save()?.then(async () => { + logger?.verbose( + `Guild: ${guild?.id} User: ${user?.id} gave ${ + discordReceiver?.id + } ${pluralize(creditAmount, "credit")}.` + ); + + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Give)") + .setDescription( + `We have given ${discordReceiver}, ${pluralize( + creditAmount, + "credit" + )}.` + ) + .setTimestamp(new Date()) + .setColor(colors?.success as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + }); + }, +}; diff --git a/src/commands/manage/groups/credits/modules/set.ts b/src/commands/manage/groups/credits/modules/set.ts deleted file mode 100644 index 92e5c5a..0000000 --- a/src/commands/manage/groups/credits/modules/set.ts +++ /dev/null @@ -1,117 +0,0 @@ -// Dependencies -import { CommandInteraction, ColorResolvable } from "discord.js"; - -// Configurations -import config from "../../../../../../config.json"; - -// Handlers -import logger from "../../../../../handlers/logger"; - -// Helpers -import pluralize from "../../../../../helpers/pluralize"; - -// Models -import fetchUser from "../../../../../helpers/fetchUser"; - -// Function -export default async (interaction: CommandInteraction) => { - // Destructure - const { options, user, guild } = interaction; - - // User Option - const optionUser = options.getUser("user"); - - // Amount Option - const optionAmount = options.getInteger("amount"); - - // If amount is null - if (optionAmount === null) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Set]" as string, - description: "We could not read your requested amount." as string, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Send interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - if (optionUser === null) return; - if (guild === null) return; - - // toUser Information - const toUser = await fetchUser(optionUser, guild); - - // If toUser does not exist - if (!toUser) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Set]" as string, - description: `We could not find ${optionUser} in our database.`, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // If toUser.credits does not exist - if (toUser?.credits === null) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Set]" as string, - description: `We could not find credits for ${optionUser} in our database.`, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // Set toUser with amount - toUser.credits = optionAmount; - - // Save toUser - await toUser?.save()?.then(async () => { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Set]" as string, - description: `We have set ${optionUser} to ${pluralize( - optionAmount, - "credit" - )}`, - color: config?.colors?.success as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Log debug message - logger?.debug( - `Guild: ${guild?.id} User: ${user?.id} set ${ - optionUser?.id - } to ${pluralize(optionAmount, "credit")}.` - ); - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - }); -}; diff --git a/src/commands/manage/groups/credits/modules/set/index.ts b/src/commands/manage/groups/credits/modules/set/index.ts new file mode 100644 index 0000000..8bda707 --- /dev/null +++ b/src/commands/manage/groups/credits/modules/set/index.ts @@ -0,0 +1,144 @@ +// Dependencies +import { CommandInteraction, ColorResolvable, MessageEmbed } from "discord.js"; + +// Configurations +import { colors, footer } from "../../../../../../../config.json"; + +// Handlers +import logger from "../../../../../../logger"; + +// Helpers +import pluralize from "../../../../../../helpers/pluralize"; + +// Models +import fetchUser from "../../../../../../helpers/fetchUser"; +import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; + +// Function +export default { + data: (command: SlashCommandSubcommandBuilder) => { + return command + .setName("set") + .setDescription("Set credits to a user") + .addUserOption((option) => + option + .setName("user") + .setDescription("The user you want to set credits on.") + .setRequired(true) + ) + .addIntegerOption((option) => + option + .setName("amount") + .setDescription("The amount you will set.") + .setRequired(true) + ); + }, + execute: async (interaction: CommandInteraction) => { + const { options, user, guild } = interaction; + + const discordUser = options.getUser("user"); + const creditAmount = options.getInteger("amount"); + + // If amount is null + if (creditAmount === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Set)") + .setDescription(`We could not read your requested amount!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + if (discordUser === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Set)") + .setDescription(`We could not read your requested user!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + if (guild === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Set)") + .setDescription(`We could not read your guild!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // toUser Information + const toUser = await fetchUser(discordUser, guild); + + // If toUser does not exist + if (toUser === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Set)") + .setDescription( + `We could not read your requested user from our database!` + ) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // If toUser.credits does not exist + if (toUser?.credits === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Set)") + .setDescription( + `We could not find credits for ${discordUser} in our database!` + ) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // Set toUser with amount + toUser.credits = creditAmount; + + // Save toUser + await toUser?.save()?.then(async () => { + logger?.verbose( + `Guild: ${guild?.id} User: ${user?.id} set ${ + discordUser?.id + } to ${pluralize(creditAmount, "credit")}.` + ); + + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Set)") + .setDescription( + `We have set ${discordUser} to ${pluralize( + creditAmount, + "credit" + )}.` + ) + .setTimestamp(new Date()) + .setColor(colors?.success as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + }); + }, +}; diff --git a/src/commands/manage/groups/credits/modules/take.ts b/src/commands/manage/groups/credits/modules/take.ts deleted file mode 100644 index 49dd6c7..0000000 --- a/src/commands/manage/groups/credits/modules/take.ts +++ /dev/null @@ -1,135 +0,0 @@ -// Dependencies -import { CommandInteraction, ColorResolvable } from "discord.js"; - -// Configurations -import config from "../../../../../../config.json"; - -// Handlers -import logger from "../../../../../handlers/logger"; - -// Helpers -import pluralize from "../../../../../helpers/pluralize"; - -// Models -import fetchUser from "../../../../../helpers/fetchUser"; - -// Function -export default async (interaction: CommandInteraction) => { - // Destructure - const { guild, user, options } = interaction; - - // User option - const optionUser = options?.getUser("user"); - - // Amount option - const optionAmount = options?.getInteger("amount"); - - // If amount is null - if (optionAmount === null) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Take]" as string, - description: "We could not read your requested amount." as string, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Send interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // If amount is zero or below - if (optionAmount <= 0) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Take]" as string, - description: "You can not take zero credits or below." as string, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - if (optionUser === null) return; - if (guild === null) return; - - // toUser Information - const toUser = await fetchUser(optionUser, guild); - - // If toUser does not exist - if (!toUser) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Take]" as string, - description: `We could not find ${optionUser} in our database.`, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // If toUser.credits does not exist - if (toUser?.credits === null) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Take]" as string, - description: `We could not find credits for ${optionUser} in our database.`, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // Withdraw amount from toUser - toUser.credits -= optionAmount; - - // Save toUser - await toUser?.save()?.then(async () => { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Set]" as string, - description: `We have taken ${pluralize( - optionAmount, - "credit" - )} from ${optionUser}`, - color: config?.colors?.success as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Log debug message - logger?.debug( - `Guild: ${guild?.id} User: ${user?.id} set ${ - optionUser?.id - } to ${pluralize(optionAmount, "credit")}.` - ); - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - }); -}; diff --git a/src/commands/manage/groups/credits/modules/take/index.ts b/src/commands/manage/groups/credits/modules/take/index.ts new file mode 100644 index 0000000..9baf0fb --- /dev/null +++ b/src/commands/manage/groups/credits/modules/take/index.ts @@ -0,0 +1,162 @@ +// Dependencies +import { CommandInteraction, ColorResolvable, MessageEmbed } from "discord.js"; + +// Configurations +import { colors, footer } from "../../../../../../../config.json"; + +// Handlers +import logger from "../../../../../../logger"; + +// Helpers +import pluralize from "../../../../../../helpers/pluralize"; + +// Models +import fetchUser from "../../../../../../helpers/fetchUser"; +import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; + +// Function +export default { + data: (command: SlashCommandSubcommandBuilder) => { + return command + .setName("take") + .setDescription("Take credits from a user") + .addUserOption((option) => + option + .setName("user") + .setDescription("The user you want to take credits from.") + .setRequired(true) + ) + .addIntegerOption((option) => + option + .setName("amount") + .setDescription("The amount you will take.") + .setRequired(true) + ); + }, + execute: async (interaction: CommandInteraction) => { + // Destructure + const { guild, user, options } = interaction; + + // User option + const optionUser = options?.getUser("user"); + + // Amount option + const optionAmount = options?.getInteger("amount"); + + // If amount is null + if (optionAmount === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Take)") + .setDescription(`We could not read your requested amount!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // If amount is zero or below + if (optionAmount <= 0) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Take)") + .setDescription(`We could not take zero credits or below!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + if (optionUser === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Take)") + .setDescription(`We could not read your requested user!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + if (guild === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Take)") + .setDescription(`We could not read your guild!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // toUser Information + const toUser = await fetchUser(optionUser, guild); + + // If toUser does not exist + if (toUser === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Take)") + .setDescription( + `We could not read your requested user from our database!` + ) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // If toUser.credits does not exist + if (toUser?.credits === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Take)") + .setDescription( + `We could not find credits for ${optionUser} in our database!` + ) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // Withdraw amount from toUser + toUser.credits -= optionAmount; + + // Save toUser + await toUser?.save()?.then(async () => { + logger?.verbose( + `Guild: ${guild?.id} User: ${user?.id} set ${ + optionUser?.id + } to ${pluralize(optionAmount, "credit")}.` + ); + + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Take)") + .setDescription( + `We have taken ${pluralize( + optionAmount, + "credit" + )} from ${optionUser}.` + ) + .setTimestamp(new Date()) + .setColor(colors?.success as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + }); + }, +}; diff --git a/src/commands/manage/groups/credits/modules/transfer.ts b/src/commands/manage/groups/credits/modules/transfer.ts deleted file mode 100644 index 8722acc..0000000 --- a/src/commands/manage/groups/credits/modules/transfer.ts +++ /dev/null @@ -1,173 +0,0 @@ -// Dependencies -import { CommandInteraction, ColorResolvable } from "discord.js"; - -// Configurations -import config from "../../../../../../config.json"; - -// Handlers -import logger from "../../../../../handlers/logger"; - -// Helpers -import pluralize from "../../../../../helpers/pluralize"; -import saveUser from "../../../../../helpers/saveUser"; - -// Models -import fetchUser from "../../../../../helpers/fetchUser"; - -// Function -export default async (interaction: CommandInteraction) => { - // Destructure member - const { guild, options, user } = interaction; - - // Get options - const optionFromUser = options?.getUser("from"); - const optionToUser = options?.getUser("to"); - const optionAmount = options?.getInteger("amount"); - - // If amount is null - if (optionAmount === null) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Transfer]" as string, - description: "We could not read your requested amount." as string, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Send interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - if (guild === null) return; - if (optionFromUser === null) return; - if (optionToUser === null) return; - - // Get fromUser object - const fromUser = await fetchUser(optionFromUser, guild); - - // Get toUser object - const toUser = await fetchUser(optionToUser, guild); - - // If toUser does not exist - if (!fromUser) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Transfer]" as string, - description: `We could not find ${optionFromUser} in our database.`, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // If toUser.credits does not exist - if (!fromUser?.credits) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Transfer]" as string, - description: `We could not find credits for ${optionFromUser} in our database.`, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // If toUser does not exist - if (!toUser) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Transfer]" as string, - description: `We could not find ${optionToUser} in our database.`, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // If toUser.credits does not exist - if (toUser?.credits === null) { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Transfer]" as string, - description: `We could not find credits for ${optionToUser} in our database.`, - color: config?.colors?.error as ColorResolvable, - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - } - - // Withdraw amount from fromUser - fromUser.credits -= optionAmount; - - // Deposit amount to toUser - toUser.credits += optionAmount; - - // Save users - await saveUser(fromUser, toUser)?.then(async () => { - // Embed object - const embed = { - title: ":toolbox: Admin - Credits [Transfer]" as string, - description: `You sent ${pluralize( - optionAmount, - "credit" - )} from ${optionFromUser} to ${optionToUser}.`, - color: config?.colors?.success as ColorResolvable, - fields: [ - { - name: `${optionFromUser?.username} Balance`, - value: `${fromUser?.credits}`, - inline: true, - }, - { - name: `${optionToUser?.username} Balance`, - value: `${toUser?.credits}`, - inline: true, - }, - ], - timestamp: new Date(), - footer: { - iconURL: config?.footer?.icon as string, - text: config?.footer?.text as string, - }, - }; - - // Log debug message - logger?.debug( - `Guild: ${guild?.id} User: ${user?.id} transferred ${pluralize( - optionAmount, - "credit" - )} from ${optionFromUser?.id} to ${optionToUser?.id}.` - ); - - // Return interaction reply - return interaction?.editReply({ embeds: [embed] }); - }); -}; diff --git a/src/commands/manage/groups/credits/modules/transfer/index.ts b/src/commands/manage/groups/credits/modules/transfer/index.ts new file mode 100644 index 0000000..a2054f4 --- /dev/null +++ b/src/commands/manage/groups/credits/modules/transfer/index.ts @@ -0,0 +1,217 @@ +// Dependencies +import { CommandInteraction, ColorResolvable, MessageEmbed } from "discord.js"; + +// Configurations +import { colors, footer } from "../../../../../../../config.json"; + +// Handlers +import logger from "../../../../../../logger"; + +// Helpers +import pluralize from "../../../../../../helpers/pluralize"; +import saveUser from "../../../../../../helpers/saveUser"; + +// Models +import fetchUser from "../../../../../../helpers/fetchUser"; +import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; + +// Function +export default { + data: (command: SlashCommandSubcommandBuilder) => { + return command + .setName("transfer") + .setDescription("Transfer credits from a user to another user.") + .addUserOption((option) => + option + .setName("from") + .setDescription("The user you want to take credits from.") + .setRequired(true) + ) + .addUserOption((option) => + option + .setName("to") + .setDescription("The user you want to give credits to.") + .setRequired(true) + ) + .addIntegerOption((option) => + option + .setName("amount") + .setDescription("The amount you will transfer.") + .setRequired(true) + ); + }, + execute: async (interaction: CommandInteraction) => { + // Destructure member + const { guild, options, user } = interaction; + + // Get options + const optionFromUser = options?.getUser("from"); + const optionToUser = options?.getUser("to"); + const optionAmount = options?.getInteger("amount"); + + // If amount is null + if (optionAmount === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Transfer)") + .setDescription(`We could not read your requested amount!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + if (guild === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Transfer)") + .setDescription(`We could not read your guild!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + if (optionFromUser === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Transfer)") + .setDescription(`We could not read your requested from user!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + if (optionToUser === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Transfer)") + .setDescription(`We could not read your requested to user!`) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // Get fromUser object + const fromUser = await fetchUser(optionFromUser, guild); + + // Get toUser object + const toUser = await fetchUser(optionToUser, guild); + + // If toUser does not exist + if (fromUser === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Transfer)") + .setDescription( + `We could not read your requested from user from our database!` + ) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // If toUser.credits does not exist + if (!fromUser?.credits) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Transfer)") + .setDescription( + `We could not find credits for ${optionFromUser} in our database!` + ) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // If toUser does not exist + if (toUser === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Transfer)") + .setDescription( + `We could not read your requested to user from our database!` + ) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // If toUser.credits does not exist + if (toUser?.credits === null) { + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Transfer)") + .setDescription( + `We could not find credits for ${optionToUser} in our database!` + ) + .setTimestamp(new Date()) + .setColor(colors?.error as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + } + + // Withdraw amount from fromUser + fromUser.credits -= optionAmount; + + // Deposit amount to toUser + toUser.credits += optionAmount; + + // Save users + await saveUser(fromUser, toUser)?.then(async () => { + logger?.debug( + `Guild: ${guild?.id} User: ${user?.id} transferred ${pluralize( + optionAmount, + "credit" + )} from ${optionFromUser?.id} to ${optionToUser?.id}.` + ); + + return interaction?.editReply({ + embeds: [ + new MessageEmbed() + .setTitle("[:toolbox:] Manage - Credits (Transfer)") + .setDescription( + `We have sent ${pluralize( + optionAmount, + "credit" + )} from ${optionFromUser} to ${optionToUser}.` + ) + .addFields( + { + name: `${optionFromUser?.username} Balance`, + value: `${fromUser?.credits}`, + inline: true, + }, + { + name: `${optionToUser?.username} Balance`, + value: `${toUser?.credits}`, + inline: true, + } + ) + .setTimestamp(new Date()) + .setColor(colors?.success as ColorResolvable) + .setFooter({ text: footer?.text, iconURL: footer?.icon }), + ], + }); + }); + }, +};