From d624c62669692e9d14f7d226e2b0078991a216b4 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Sun, 23 Oct 2022 22:32:54 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Give=20credits=20helper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/credits/modules/work/index.ts | 43 +------- .../modules/credits/modules/give/index.ts | 103 +++++------------- .../messageCreate/modules/credits/index.ts | 20 +--- src/helpers/credits/index.ts | 56 ++++++++++ 4 files changed, 88 insertions(+), 134 deletions(-) diff --git a/src/commands/credits/modules/work/index.ts b/src/commands/credits/modules/work/index.ts index bca9dc4..a4b4ba9 100644 --- a/src/commands/credits/modules/work/index.ts +++ b/src/commands/credits/modules/work/index.ts @@ -6,6 +6,7 @@ import { command as CooldownCommand } from "../../../../handlers/cooldown"; import prisma from "../../../../handlers/database"; import deferReply from "../../../../handlers/deferReply"; import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds"; +import { give as CreditsGive } from "../../../../helpers/credits"; import logger from "../../../../middlewares/logger"; // 1. Export a builder function. @@ -51,51 +52,13 @@ export const execute = async (interaction: CommandInteraction) => { max: createGuild.creditsWorkRate, }); - // 7. Upsert the guildMember in the database. - const createGuildMember = await prisma.guildMember.upsert({ - where: { - userId_guildId: { - userId: user.id, - guildId: guild.id, - }, - }, - update: { creditsEarned: { increment: creditsEarned } }, - create: { - creditsEarned, - user: { - connectOrCreate: { - create: { - id: user.id, - }, - where: { - id: user.id, - }, - }, - }, - guild: { - connectOrCreate: { - create: { - id: guild.id, - }, - where: { - id: guild.id, - }, - }, - }, - }, - include: { - user: true, - guild: true, - }, - }); - logger.silly(createGuildMember); - if (!createGuildMember) throw new Error("GuildMember not found"); + const upsertGuildMember = await CreditsGive(guild, user, creditsEarned); // 8. Send embed. await interaction.editReply({ embeds: [ EmbedSuccess.setDescription( - `You worked and earned **${creditsEarned}** credits! You now have **${createGuildMember.creditsEarned}** credits. :tada:` + `You worked and earned **${creditsEarned}** credits! You now have **${upsertGuildMember.creditsEarned}** credits. :tada:` ), ], }); diff --git a/src/commands/manage/modules/credits/modules/give/index.ts b/src/commands/manage/modules/credits/modules/give/index.ts index f2c070b..2ecc4ea 100644 --- a/src/commands/manage/modules/credits/modules/give/index.ts +++ b/src/commands/manage/modules/credits/modules/give/index.ts @@ -1,21 +1,12 @@ -// Dependencies import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; -import { - ChatInputCommandInteraction, - EmbedBuilder, - PermissionsBitField, -} from "discord.js"; -import logger from "../../../../../../middlewares/logger"; -// Configurations -import getEmbedConfig from "../../../../../../helpers/getEmbedData"; -// Helpers../../../../../../../helpers/userData -import pluralize from "../../../../../../helpers/pluralize"; -// Models -// Handlers -import prisma from "../../../../../../handlers/database"; +import { ChatInputCommandInteraction, PermissionsBitField } from "discord.js"; + import deferReply from "../../../../../../handlers/deferReply"; +import { success as baseEmbedSuccess } from "../../../../../../helpers/baseEmbeds"; import checkPermission from "../../../../../../helpers/checkPermission"; -// Function +import { give as CreditsGive } from "../../../../../../helpers/credits"; +import pluralize from "../../../../../../helpers/pluralize"; + export default { builder: (command: SlashCommandSubcommandBuilder) => { return command @@ -34,81 +25,41 @@ export default { .setRequired(true) ); }, + execute: async (interaction: ChatInputCommandInteraction) => { + // 1. Defer reply as ephemeral. await deferReply(interaction, true); + // 2. Check if the user has the MANAGE_GUILD permission. checkPermission(interaction, PermissionsBitField.Flags.ManageGuild); - const { successColor, footerText, footerIcon } = await getEmbedConfig( - interaction.guild - ); // Destructure + // 3. Destructure interaction object. const { guild, options } = interaction; + if (!guild) + throw new Error("We could not get the current guild from discord."); + if (!options) throw new Error("We could not get the options from discord."); - const discordReceiver = options?.getUser("user"); - const creditAmount = options?.getInteger("amount"); - - // If amount option is null - if (creditAmount === null) + // 4. Get the user and amount from the options. + const discordReceiver = options.getUser("user"); + const creditsAmount = options.getInteger("amount"); + if (typeof creditsAmount !== "number") throw new Error("You need to provide a credit amount."); - - // If amount is zero or below - if (creditAmount <= 0) - throw new Error("You must provide a credit amount greater than zero"); - - if (discordReceiver === null) + if (!discordReceiver) throw new Error("We could not get the receiving user from Discord"); - if (guild === null) - throw new Error("We could not get the current guild from discord."); + // 5. Create base embeds. + const embedSuccess = await baseEmbedSuccess(guild, "[:toolbox:] Give"); - const createGuildMember = await prisma.guildMember.upsert({ - where: { - userId_guildId: { - userId: discordReceiver.id, - guildId: guild.id, - }, - }, - update: { creditsEarned: { increment: creditAmount } }, - create: { - creditsEarned: creditAmount, - user: { - connectOrCreate: { - create: { - id: discordReceiver.id, - }, - where: { - id: discordReceiver.id, - }, - }, - }, - guild: { - connectOrCreate: { - create: { - id: guild.id, - }, - where: { - id: guild.id, - }, - }, - }, - }, - }); + // 6. Give the credits. + await CreditsGive(guild, discordReceiver, creditsAmount); - logger.silly(createGuildMember); - - // Save toUser - await interaction?.editReply({ + // 7. Send embed. + return await interaction.editReply({ embeds: [ - new EmbedBuilder() - .setTitle("[:toolbox:] Manage - Credits (Give)") - .setDescription( - `Successfully gave ${pluralize(creditAmount, "credit")}` - ) - .setTimestamp(new Date()) - .setColor(successColor) - .setFooter({ text: footerText, iconURL: footerIcon }), + embedSuccess.setDescription( + `Successfully gave ${pluralize(creditsAmount, "credit")}` + ), ], }); - return; }, }; diff --git a/src/events/messageCreate/modules/credits/index.ts b/src/events/messageCreate/modules/credits/index.ts index 2ae1ac8..39534d1 100644 --- a/src/events/messageCreate/modules/credits/index.ts +++ b/src/events/messageCreate/modules/credits/index.ts @@ -1,6 +1,7 @@ import { ChannelType, Message } from "discord.js"; import { message as CooldownMessage } from "../../../../handlers/cooldown"; import prisma from "../../../../handlers/database"; +import { give as CreditsGive } from "../../../../helpers/credits"; import logger from "../../../../middlewares/logger"; export default { @@ -58,23 +59,6 @@ export default { ); if (isOnCooldown) return; - const updateGuildMember = await prisma.guildMember.update({ - where: { - userId_guildId: { - userId: author.id, - guildId: guild.id, - }, - }, - data: { - creditsEarned: { - increment: createGuildMember.guild.creditsRate, - }, - }, - }); - - logger.silly(updateGuildMember); - - if (!updateGuildMember) - throw new Error("Failed to update guildMember object"); + await CreditsGive(guild, author, createGuildMember.guild.creditsRate); }, }; diff --git a/src/helpers/credits/index.ts b/src/helpers/credits/index.ts index 13bb6af..bacec5d 100644 --- a/src/helpers/credits/index.ts +++ b/src/helpers/credits/index.ts @@ -115,3 +115,59 @@ export const transfer = async ( return recipient; }); }; + +// Give to guildMember +export const give = async (guild: Guild, user: User, amount: number) => { + // 1. Verify that the amount is not above 100.000.000 credits. + if (amount > 100000000) { + throw new Error("You can't give more than 1.000.000 credits."); + } + + // 2. Verify that the amount is not below 1 credits. + if (amount <= 0) { + throw new Error("You can't give below one credit."); + } + + // 3. Verify that the user is not an bot. + if (user.bot) { + throw new Error("You can't give to an bot."); + } + + // 4. Increment the user's balance by amount. + return await prisma.guildMember.upsert({ + update: { + creditsEarned: { + increment: amount, + }, + }, + create: { + user: { + connectOrCreate: { + create: { + id: user.id, + }, + where: { + id: user.id, + }, + }, + }, + guild: { + connectOrCreate: { + create: { + id: guild.id, + }, + where: { + id: guild.id, + }, + }, + }, + creditsEarned: amount, + }, + where: { + userId_guildId: { + userId: user.id, + guildId: guild.id, + }, + }, + }); +};