♻️ Give credits helper
This commit is contained in:
parent
45549f41d7
commit
d624c62669
4 changed files with 88 additions and 134 deletions
|
@ -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:`
|
||||
),
|
||||
],
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue