From 7a52ebb182850f49c98d0310a0e0402a2c9a4411 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 12 Jan 2024 17:51:29 +0000 Subject: [PATCH] Add give command (#137) # 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. - Add give command - Update the whitelist for gdrive sync list to a general admins list #113 ## 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://gitea.vylpes.xyz/External/card-drop/pulls/137 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- .dev.env | 2 +- .gitignore | 2 +- .prod.env | 2 +- .stage.env | 2 +- src/bot.ts | 2 +- src/commands/gdrivesync.ts | 2 +- src/commands/give.ts | 68 +++++++++++++++++++++++++++ src/commands/resync.ts | 2 +- src/helpers/CardDropHelperMetadata.ts | 10 +++- src/registry.ts | 2 + 10 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 src/commands/give.ts diff --git a/.dev.env b/.dev.env index 46544de..28da9df 100644 --- a/.dev.env +++ b/.dev.env @@ -12,6 +12,7 @@ BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=682942374040961060 BOT_ENV=4 +BOT_ADMINS=147392775707426816,887272961504071690 ABOUT_FUNDING= ABOUT_REPO= @@ -30,5 +31,4 @@ DB_CARD_FILE=:memory: EXPRESS_PORT=3303 -GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690 GDRIVESYNC_AUTO=true diff --git a/.gitignore b/.gitignore index a354170..aecb458 100644 --- a/.gitignore +++ b/.gitignore @@ -107,5 +107,5 @@ config.json .DS_Store ormconfig.json gdrive-credentials.json -cards/ +data/ *.db \ No newline at end of file diff --git a/.prod.env b/.prod.env index d252f1f..79e98fb 100644 --- a/.prod.env +++ b/.prod.env @@ -12,6 +12,7 @@ BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=1093810443589529631 BOT_ENV=1 +BOT_ADMINS=147392775707426816,887272961504071690 ABOUT_FUNDING= ABOUT_REPO= @@ -30,5 +31,4 @@ DB_CARD_FILE=:memory: EXPRESS_PORT=3323 -GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690 GDRIVESYNC_AUTO=false diff --git a/.stage.env b/.stage.env index 3ffed38..8db9eb5 100644 --- a/.stage.env +++ b/.stage.env @@ -12,6 +12,7 @@ BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=1147976642942214235 BOT_ENV=2 +BOT_ADMINS=147392775707426816,887272961504071690 ABOUT_FUNDING= ABOUT_REPO= @@ -30,5 +31,4 @@ DB_CARD_FILE=:memory: EXPRESS_PORT=3313 -GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690 GDRIVESYNC_AUTO=false diff --git a/src/bot.ts b/src/bot.ts index 912fa3e..87008c5 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -14,6 +14,7 @@ const requiredConfigs: string[] = [ "BOT_OWNERID", "BOT_CLIENTID", "BOT_ENV", + "BOT_ADMINS", "DATA_DIR", "DB_HOST", "DB_PORT", @@ -22,7 +23,6 @@ const requiredConfigs: string[] = [ "DB_SYNC", "DB_LOGGING", "EXPRESS_PORT", - "GDRIVESYNC_WHITELIST", ]; requiredConfigs.forEach(config => { diff --git a/src/commands/gdrivesync.ts b/src/commands/gdrivesync.ts index 2429b4d..be36fc1 100644 --- a/src/commands/gdrivesync.ts +++ b/src/commands/gdrivesync.ts @@ -18,7 +18,7 @@ export default class Gdrivesync extends Command { public override async execute(interaction: CommandInteraction) { if (!interaction.isChatInputCommand()) return; - const whitelistedUsers = process.env.GDRIVESYNC_WHITELIST!.split(","); + 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."); diff --git a/src/commands/give.ts b/src/commands/give.ts new file mode 100644 index 0000000..ad360a9 --- /dev/null +++ b/src/commands/give.ts @@ -0,0 +1,68 @@ +import { CacheType, CommandInteraction, PermissionsBitField, SlashCommandBuilder } from "discord.js"; +import { Command } from "../type/command"; +import { CoreClient } from "../client/client"; +import Config from "../database/entities/app/Config"; +import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata"; +import Inventory from "../database/entities/app/Inventory"; + +export default class Give extends Command { + constructor() { + super(); + + this.CommandBuilder = new SlashCommandBuilder() + .setName("give") + .setDescription("Give a user a card manually, in case bot breaks") + .setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator) + .addStringOption(x => + x + .setName("cardnumber") + .setDescription("G") + .setRequired(true)) + .addUserOption(x => + x + .setName("user") + .setDescription("The user to give the card to") + .setRequired(true)); + } + + public override async execute(interaction: CommandInteraction) { + if (!CoreClient.AllowDrops) { + await interaction.reply("Bot is currently syncing, please wait until its done."); + return; + } + + if (await Config.GetValue("safemode") == "true") { + await interaction.reply("Safe Mode has been activated, please resync to continue."); + 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); + + const card = CardDropHelperMetadata.GetCardByCardNumber(cardNumber.value!.toString()); + + if (!card) { + await interaction.reply("Unable to fetch card, please try again."); + return; + } + + let inventory = await Inventory.FetchOneByCardNumberAndUserId(user.id, card.id); + + if (!inventory) { + inventory = new Inventory(user.id, card.id, 1); + } else { + inventory.SetQuantity(inventory.Quantity + 1); + } + + await inventory.Save(Inventory, inventory); + + await interaction.reply(`${card.name} given to ${interaction.user}, they now have ${inventory.Quantity}`); + } +} \ No newline at end of file diff --git a/src/commands/resync.ts b/src/commands/resync.ts index ace4c39..7040d52 100644 --- a/src/commands/resync.ts +++ b/src/commands/resync.ts @@ -16,7 +16,7 @@ export default class Resync extends Command { public override async execute(interaction: CommandInteraction) { if (!interaction.isChatInputCommand()) return; - const whitelistedUsers = process.env.GDRIVESYNC_WHITELIST!.split(","); + 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."); diff --git a/src/helpers/CardDropHelperMetadata.ts b/src/helpers/CardDropHelperMetadata.ts index 38d8304..27eebb8 100644 --- a/src/helpers/CardDropHelperMetadata.ts +++ b/src/helpers/CardDropHelperMetadata.ts @@ -1,7 +1,7 @@ import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js"; import { CardRarity, CardRarityToColour, CardRarityToString } from "../constants/CardRarity"; import CardRarityChances from "../constants/CardRarityChances"; -import { DropResult } from "../contracts/SeriesMetadata"; +import { CardMetadata, DropResult } from "../contracts/SeriesMetadata"; import { CoreClient } from "../client/client"; export default class CardDropHelperMetadata { @@ -47,6 +47,14 @@ export default class CardDropHelperMetadata { }; } + public static GetCardByCardNumber(cardNumber: string): CardMetadata | undefined { + const card = CoreClient.Cards + .flatMap(x => x.cards) + .find(x => x.id == cardNumber); + + return card; + } + public static GenerateDropEmbed(drop: DropResult, quantityClaimed: number, imageFileName: string): EmbedBuilder { let description = ""; description += `Series: ${drop.series.name}\n`; diff --git a/src/registry.ts b/src/registry.ts index ab4dfad..a7cf494 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -5,6 +5,7 @@ import { Environment } from "./constants/Environment"; import About from "./commands/about"; import Drop from "./commands/drop"; import Gdrivesync from "./commands/gdrivesync"; +import Give from "./commands/give"; import Inventory from "./commands/inventory"; import Resync from "./commands/resync"; @@ -23,6 +24,7 @@ export default class Registry { CoreClient.RegisterCommand("about", new About()); CoreClient.RegisterCommand("drop", new Drop()); CoreClient.RegisterCommand("gdrivesync", new Gdrivesync()); + CoreClient.RegisterCommand("give", new Give()); CoreClient.RegisterCommand("inventory", new Inventory()); CoreClient.RegisterCommand("resync", new Resync());