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
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
const { REST } = require("@discordjs/rest");
|
const { REST } = require("@discordjs/rest");
|
||||||
const { Routes } = require("discord-api-types/v9")
|
const { Routes } = require("discord-api-types/v9")
|
||||||
const { Guild } = require("../models");
|
const { Guild, User } = require("../models");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: "interactionCreate",
|
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);
|
const command = interaction.client.commands.get(interaction.commandName);
|
||||||
|
|
||||||
if (!command) return;
|
if (!command) return;
|
||||||
|
|||||||
Reference in New Issue
Block a user