Add card dropping and collection command
This commit is contained in:
48
commands/collection.js
Normal file
48
commands/collection.js
Normal file
@@ -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
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
37
commands/drop.js
Normal file
37
commands/drop.js
Normal file
@@ -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
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user