From cfc9e2ba6b164377d35d874127f474d121687a42 Mon Sep 17 00:00:00 2001 From: Minzkraut Date: Sun, 17 Apr 2022 19:39:03 +0200 Subject: [PATCH] Add card dropping and collection command --- commands/collection.js | 48 +++++++++++++++++++++++++++++++++++++ commands/drop.js | 37 ++++++++++++++++++++++++++++ events/interactionCreate.js | 16 ++++++++++++- 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 commands/collection.js create mode 100644 commands/drop.js diff --git a/commands/collection.js b/commands/collection.js new file mode 100644 index 0000000..28d8c20 --- /dev/null +++ b/commands/collection.js @@ -0,0 +1,48 @@ +const { SlashCommandBuilder } = require("@discordjs/builders"); +const { Card, User, Character } = require("../models"); + +//fetch all cards owned by the user and list them +module.exports = { + data: new SlashCommandBuilder() + .setName("collection") + .setDescription("List all cards in your collection"), + async execute(interaction) { + //fetch the user given the userID + const user = await User.findOne({ + where: { + userID: interaction.member.id + } + }); + + //fetch all cards give the user id + const cards = await Card.findAll({ + where: { + ownerID: user.id + }, + include: [{ + model: Character, + as: "character" + }] + }); + + //if the user has no cards, tell him + if (cards.length === 0) { + interaction.reply({ + content: "You have no cards in your collection", + ephemeral: true + }); + return; + } + + //if the user has cards, list them + let message = ""; + for (let i = 0; i < cards.length; i++) { + message += `${cards[i].id} - ${cards[i].characterID} ${cards[i].character.name} \n`; + } + interaction.reply({ + content: message, + ephemeral: false + }); + + } +} \ No newline at end of file diff --git a/commands/drop.js b/commands/drop.js new file mode 100644 index 0000000..68304bc --- /dev/null +++ b/commands/drop.js @@ -0,0 +1,37 @@ +const { SlashCommandBuilder } = require("@discordjs/builders"); +const { Card, User } = require("../models"); + +module.exports = { + data: new SlashCommandBuilder() + .setName("drop") + .setDescription("Drop a card") + .addIntegerOption((option) => + option + .setName("id") + .setDescription("The id of the character to drop") + .setRequired(true) + ), + + async execute(interaction) { + //get user id from database given the userID + const user = await User.findOne({ + where: { + userID: interaction.member.id + } + }); + + //create new card with the given character id, and the user id + const card = await Card.create({ + characterID: interaction.options.getInteger("id"), + identifier: "00000", + ownerID: user.id, + }); + + //reply with the new card id + interaction.reply({ + content: `Dropped card ${card.id}`, + ephemeral: false + }); + + } +} \ No newline at end of file diff --git a/events/interactionCreate.js b/events/interactionCreate.js index 546b644..d2e4465 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -1,7 +1,7 @@ require("dotenv").config(); const { REST } = require("@discordjs/rest"); const { Routes } = require("discord-api-types/v9") -const { Guild } = require("../models"); +const { Guild, User } = require("../models"); module.exports = { name: "interactionCreate", @@ -29,6 +29,20 @@ module.exports = { }); } + //check if the user exists in the database, if not tell him to use the /register command + let user = await User.findOne({ + where: { + userID: interaction.member.id + } + }); + if (!user && interaction.commandName !== "register") { + interaction.reply({ + content: `You are not registered, use the /register command`, + ephemeral: false + }); + return; + } + const command = interaction.client.commands.get(interaction.commandName); if (!command) return;