Add give command (#137)
All checks were successful
continuous-integration/drone/push Build is passing

# 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 <tester@vylpes.com>
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
Ethan Lane 2024-01-12 17:51:29 +00:00 committed by Vylpes
parent 8a236d01b5
commit 7a52ebb182
10 changed files with 86 additions and 8 deletions

View file

@ -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

2
.gitignore vendored
View file

@ -107,5 +107,5 @@ config.json
.DS_Store
ormconfig.json
gdrive-credentials.json
cards/
data/
*.db

View file

@ -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

View file

@ -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

View file

@ -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 => {

View file

@ -18,7 +18,7 @@ export default class Gdrivesync extends Command {
public override async execute(interaction: CommandInteraction<CacheType>) {
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.");

68
src/commands/give.ts Normal file
View file

@ -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<CacheType>) {
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}`);
}
}

View file

@ -16,7 +16,7 @@ export default class Resync extends Command {
public override async execute(interaction: CommandInteraction<CacheType>) {
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.");

View file

@ -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`;

View file

@ -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());