card-drop/src/commands/give.ts
Ethan Lane 7a52ebb182
All checks were successful
continuous-integration/drone/push Build is passing
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 <tester@vylpes.com>
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
2024-01-12 17:51:29 +00:00

68 lines
2.5 KiB
TypeScript

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