From de14723df03f7246e4287b57a9ae68470ed1f786 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 11 May 2024 12:48:48 +0100 Subject: [PATCH] Update give command to allow currency to be given (#220) # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Update the give command to have the existing card functionality moved to a subcommand - Add a subcommand to the give command to allow currency to be given #206 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. # Checklist - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://git.vylpes.xyz/External/card-drop/pulls/220 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- src/buttonEvents/Claim.ts | 2 +- src/commands/give.ts | 81 ++++++++++++++++++++++++------- src/database/entities/app/User.ts | 4 ++ src/type/command.ts | 5 +- 4 files changed, 72 insertions(+), 20 deletions(-) diff --git a/src/buttonEvents/Claim.ts b/src/buttonEvents/Claim.ts index 9d29f7b..f15e1ae 100644 --- a/src/buttonEvents/Claim.ts +++ b/src/buttonEvents/Claim.ts @@ -42,7 +42,7 @@ export default class Claim extends ButtonEvent { await inventory.Save(Inventory, inventory); - let user = await User.FetchOneById(User, userId) || new User(userId, 300); + const user = await User.FetchOneById(User, userId) || new User(userId, 300); AppLogger.LogSilly("Button/Claim", `${user.Id} has ${user.Currency} currency`); diff --git a/src/commands/give.ts b/src/commands/give.ts index 3656dc5..ebdebe4 100644 --- a/src/commands/give.ts +++ b/src/commands/give.ts @@ -5,6 +5,7 @@ import Config from "../database/entities/app/Config"; import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata"; import Inventory from "../database/entities/app/Inventory"; import AppLogger from "../client/appLogger"; +import User from "../database/entities/app/User"; export default class Give extends Command { constructor() { @@ -14,19 +15,57 @@ export default class Give extends Command { .setName("give") .setDescription("Give a user a card manually, in case bot breaks") .setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator) - .addStringOption(x => + .addSubcommand(x => x - .setName("cardnumber") - .setDescription("G") - .setRequired(true)) - .addUserOption(x => + .setName("card") + .setDescription("Give a user a card manually") + .addStringOption(x => + x + .setName("cardnumber") + .setDescription("The card to give") + .setRequired(true)) + .addUserOption(x => + x + .setName("user") + .setDescription("The user to give the card to") + .setRequired(true))) + .addSubcommand(x => x - .setName("user") - .setDescription("The user to give the card to") - .setRequired(true)); + .setName("currency") + .setDescription("Give a user currency manually") + .addNumberOption(x => + x + .setName("amount") + .setDescription("The amount to give") + .setRequired(true)) + .addUserOption(x => + x + .setName("user") + .setDescription("The user to give the currency to") + .setRequired(true))); } public override async execute(interaction: CommandInteraction) { + if (!interaction.isChatInputCommand()) return; + + const whitelistedUsers = process.env.BOT_ADMINS!.split(","); + + if (!whitelistedUsers.find(x => x == interaction.user.id)) { + await interaction.reply("Only whitelisted users can use this command."); + return; + } + + switch (interaction.options.getSubcommand()) { + case "card": + await this.GiveCard(interaction); + break; + case "currency": + await this.GiveCurrency(interaction); + break; + } + } + + private async GiveCard(interaction: CommandInteraction) { if (!CoreClient.AllowDrops) { await interaction.reply("Bot is currently syncing, please wait until its done."); return; @@ -37,17 +76,10 @@ export default class Give extends Command { return; } - const whitelistedUsers = process.env.BOT_ADMINS!.split(","); - - if (!whitelistedUsers.find(x => x == interaction.user.id)) { - await interaction.reply("Only whitelisted users can use this command."); - return; - } - const cardNumber = interaction.options.get("cardnumber", true); const user = interaction.options.getUser("user", true); - AppLogger.LogSilly("Commands/Give", `Parameters: cardNumber=${cardNumber.value}, user=${user.id}`); + AppLogger.LogSilly("Commands/Give/GiveCard", `Parameters: cardNumber=${cardNumber.value}, user=${user.id}`); const card = CardDropHelperMetadata.GetCardByCardNumber(cardNumber.value!.toString()); @@ -66,6 +98,21 @@ export default class Give extends Command { await inventory.Save(Inventory, inventory); - await interaction.reply(`${card.card.name} given to ${user.username}, they now have ${inventory.Quantity}`); + await interaction.reply(`Card ${card.card.name} given to ${user.username}, they now have ${inventory.Quantity}`); + } + + private async GiveCurrency(interaction: CommandInteraction) { + const amount = interaction.options.get("amount", true); + const user = interaction.options.getUser("user", true); + + AppLogger.LogSilly("Commands/Give/GiveCurrency", `Parameters: amount=${amount.value} user=${user.id}`); + + const userEntity = await User.FetchOneById(User, user.id) || new User(user.id, 300); + + userEntity.AddCurrency(amount.value! as number); + + await userEntity.Save(User, userEntity); + + await interaction.reply(`${amount.value} currency ${amount.value! as number >= 0 ? "given to" : "taken from"} ${user.username}, they now have ${userEntity.Currency}`); } } \ No newline at end of file diff --git a/src/database/entities/app/User.ts b/src/database/entities/app/User.ts index 649c1c1..4c5798d 100644 --- a/src/database/entities/app/User.ts +++ b/src/database/entities/app/User.ts @@ -17,6 +17,10 @@ export default class User extends AppBaseEntity { this.Currency = currency; } + public AddCurrency(amount: number) { + this.Currency += amount; + } + public RemoveCurrency(amount: number): boolean { if (this.Currency < amount) return false; diff --git a/src/type/command.ts b/src/type/command.ts index 20f5e3a..458a81e 100644 --- a/src/type/command.ts +++ b/src/type/command.ts @@ -1,7 +1,8 @@ -import { CommandInteraction, SlashCommandBuilder } from "discord.js"; +import { CommandInteraction } from "discord.js"; export abstract class Command { - public CommandBuilder: Omit; + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- CommandBuilder type is dynamic depending on options and can't be strictly typed + public CommandBuilder: any; abstract execute(interaction: CommandInteraction): Promise; }