chore(ux): 🚸 Improved /credits gift

I improved UX with better messages for /credits gift
This commit is contained in:
Axel Olausson Holtenäs 2022-12-04 19:05:02 +01:00
parent 7b34763660
commit 7398223b53
No known key found for this signature in database
GPG key ID: E67293FB0288F128

View file

@ -2,32 +2,36 @@ import {
ChatInputCommandInteraction, ChatInputCommandInteraction,
SlashCommandSubcommandBuilder, SlashCommandSubcommandBuilder,
} from "discord.js"; } from "discord.js";
import prisma from "../../../../handlers/database";
import deferReply from "../../../../handlers/deferReply"; import deferReply from "../../../../handlers/deferReply";
import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds"; import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds";
import creditsTransfer from "../../../../helpers/credits/transfer"; import creditsTransfer from "../../../../helpers/credits/transfer";
import logger from "../../../../middlewares/logger";
// 1. Export a builder function. // 1. Export a builder function.
export const builder = (command: SlashCommandSubcommandBuilder) => { export const builder = (command: SlashCommandSubcommandBuilder) => {
return command return command
.setName("gift") .setName("gift")
.setDescription(`Gift a user credits`) .setDescription(`Gift credits to an account`)
.addUserOption((option) => .addUserOption((option) =>
option option
.setName("target") .setName("account")
.setDescription("The user you want to gift credits to.") .setDescription("The account you gift to")
.setRequired(true) .setRequired(true)
) )
.addIntegerOption((option) => .addIntegerOption((option) =>
option option
.setName("credits") .setName("credits")
.setDescription("The amount of credits you want to gift.") .setDescription("How much you gift")
.setRequired(true) .setRequired(true)
.setMinValue(1) .setMinValue(1)
.setMaxValue(100000000) .setMaxValue(100000000)
) )
.addStringOption((option) => .addStringOption((option) =>
option.setName("reason").setDescription("Your reason.") option
.setName("message")
.setDescription("Your personalized message to the account")
); );
}; };
@ -37,44 +41,124 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
await deferReply(interaction, true); await deferReply(interaction, true);
// 2. Destructure interaction object. // 2. Destructure interaction object.
const { options, user, guild, client } = interaction; const { options, user, guild } = interaction;
if (!guild) throw new Error("Guild not found"); if (!guild) throw new Error("Server unavailable");
if (!user) throw new Error("User not found"); if (!user) throw new Error("User unavailable");
if (!client) throw new Error("Client not found");
if (!options) throw new Error("Options not found");
// 3. Get options from interaction. // 3. Get options from interaction.
const target = options.getUser("target"); const account = options.getUser("account");
const credits = options.getInteger("credits"); const credits = options.getInteger("credits");
const reason = options.getString("reason"); const message = options.getString("message");
if (!target) throw new Error("Target user not found"); if (!account) throw new Error("Account unavailable");
if (typeof credits !== "number") if (typeof credits !== "number")
throw new Error("You need to specify a number of credits you want to gift"); throw new Error("You need to enter a valid number of credits to gift");
// 4. Create base embeds. // 4. Create base embeds.
const EmbedSuccess = await BaseEmbedSuccess(guild, "[:dollar:] Gift"); const receiverEmbed = await BaseEmbedSuccess(
guild,
`:credit_card:︱You received a gift from ${user.username}`
);
// 5. Start an transaction of the credits. // 5. Start an transaction of the credits.
await creditsTransfer(guild, user, target, credits); await creditsTransfer(guild, user, account, credits);
const receiverGuildMember = await prisma.guildMember.upsert({
where: {
userId_guildId: {
userId: account.id,
guildId: guild.id,
},
},
update: {},
create: {
user: {
connectOrCreate: {
create: {
id: account.id,
},
where: {
id: account.id,
},
},
},
guild: {
connectOrCreate: {
create: {
id: guild.id,
},
where: {
id: guild.id,
},
},
},
},
include: {
user: true,
guild: true,
},
});
logger.silly(receiverGuildMember);
if (message) receiverEmbed.setFields({ name: "Message", value: message });
// 6. Tell the target that they have been gifted credits. // 6. Tell the target that they have been gifted credits.
await target.send({ await account.send({
embeds: [ embeds: [
EmbedSuccess.setDescription( receiverEmbed.setDescription(
reason `You received a gift containing ${credits} coins from ${user}! You now have ${receiverGuildMember.creditsEarned} coins in balance!`
? `You received ${credits} credits from ${user} for the reason: ${reason}.`
: `You received ${credits} credits from ${user}.`
), ),
], ],
}); });
const senderGuildMember = await prisma.guildMember.upsert({
where: {
userId_guildId: {
userId: user.id,
guildId: guild.id,
},
},
update: {},
create: {
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(senderGuildMember);
const senderEmbed = await BaseEmbedSuccess(
guild,
":credit_card:︱Send a gift"
);
if (message) senderEmbed.setFields({ name: "Message", value: message });
// 7. Tell the sender that they have gifted the credits. // 7. Tell the sender that they have gifted the credits.
await interaction.editReply({ await interaction.editReply({
embeds: [ embeds: [
EmbedSuccess.setDescription( senderEmbed.setDescription(
reason `Your gift has been sent to ${account}. You now have ${senderGuildMember.creditsEarned} coins in balance!`
? `You have successfully gifted ${credits} credits to ${target} with reason: ${reason}.`
: `You have successfully gifted ${credits} credits to ${target}.`
), ),
], ],
}); });