Updated models and migrations to new spec

This commit is contained in:
2022-08-17 19:36:47 +02:00
parent bc075805e2
commit 05804cb78a
22 changed files with 336 additions and 86 deletions

View File

@@ -7,26 +7,19 @@ module.exports = {
.setName("collection")
.setDescription("List all cards in your collection"),
async execute(interaction) {
//fetch the user given the userID
//fetch the user given the userID and include his cards
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
discordId: interaction.member.id
},
include: [{
model: Character,
as: "character"
include: [{
model: Card,
include: [Character]
}]
});
//if the user has no cards, tell him
if (cards.length === 0) {
if (user.Cards.length === 0) {
interaction.reply({
content: "You have no cards in your collection",
ephemeral: true
@@ -36,8 +29,14 @@ module.exports = {
//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`;
for (let i = 0; i < user.Cards.length; i++) {
message += `${user.Cards[i].id} - ${user.Cards[i].characterId} \n`;
message += `Identifier: ${user.Cards[i].identifier} \n`;
message += `Name: ${user.Cards[i].Character.name} \n`;
message += `Image: ${user.Cards[i].Character.imageURL} \n`;
message += `Quality: ${user.Cards[i].quality} \n`;
message += `Print number: ${user.Cards[i].printNr} \n`;
message += `------------------------ \n`;
}
interaction.reply({
content: message,

View File

@@ -1,5 +1,6 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { Card, User } = require("../models");
const { customAlphabet } = require("nanoid");
module.exports = {
data: new SlashCommandBuilder()
@@ -16,15 +17,25 @@ module.exports = {
//get user id from database given the userID
const user = await User.findOne({
where: {
userID: interaction.member.id
discordId: interaction.member.id
}
});
//create new card with the given character id, and the user id
const nanoid = customAlphabet('23456789ABCDEFGHJKLMNPRSTUVWXYZ',6); //Up to 887.503.681
const identifier = nanoid();
const existingCharacterCount = await Card.count({
where: {
characterId: interaction.options.getInteger("id")
}
});
const card = await Card.create({
characterID: interaction.options.getInteger("id"),
identifier: "00000",
ownerID: user.id,
characterId: interaction.options.getInteger("id"),
identifier: identifier,
quality: 1,
printNr: existingCharacterCount + 1,
userId: user.id
});
//reply with the new card id

View File

@@ -9,7 +9,7 @@ module.exports = {
async execute(interaction) {
let user = await User.findOne({
where: {
userID: interaction.user.id
discordId: interaction.user.id
}
});
if (user) {
@@ -19,9 +19,8 @@ module.exports = {
});
} else {
await User.create({
userID: interaction.user.id,
banned: false,
registredAt: new Date()
discordId: interaction.user.id,
active: 1,
});
interaction.reply({
content: "You are now registered",

View File

@@ -10,7 +10,7 @@ module.exports = {
let users = await User.findAll();
let userList = "";
for (let i = 0; i < users.length; i++) {
let username = await interaction.client.users.fetch(users[i].userID);
let username = await interaction.client.users.fetch(users[i].discordId);
userList += `${username.username}#${username.discriminator}`;
}
if (userList === "") {