From 58461db7c2b33daf72d935679b9dd0d7a14a0514 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 11 Jan 2024 18:03:30 +0000 Subject: [PATCH] Add give command --- .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 d880244..1aafb49 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 169e63c..90f55b6 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..cbc5c13 --- /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 f1684b6..a498e25 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 9d26609..70fee6a 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 679c70e..3537503 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());