From fb5faf727e65e32d8afe6138b735cf68cd2a171a Mon Sep 17 00:00:00 2001 From: Minzkraut Date: Wed, 7 Sep 2022 20:00:15 +0200 Subject: [PATCH] Drop: Use random sorting to pick dropped characters this fixes the problem of choosing disabled cards or IDs which are not assigned to any character. Fixes #7 --- commands/drop.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/commands/drop.js b/commands/drop.js index ce16cc8..ca0d17e 100644 --- a/commands/drop.js +++ b/commands/drop.js @@ -1,5 +1,5 @@ const { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType, AttachmentBuilder } = require("discord.js"); -const { Card, User, Character, DropHistory } = require("../models"); +const { Card, User, Character, DropHistory, sequelize } = require("../models"); const { customAlphabet } = require("nanoid"); const { CardUtils, UserUtils, ReplyUtils, GeneralUtils, Rendering } = require("../util"); const card = require("../models/card"); @@ -24,9 +24,17 @@ module.exports = { //Generate 3 cards, each is persisted with an initial userId of NULL const cards = []; + let characters = await Character.findAll({ + where: { + enabled: true + }, + order: sequelize.random(), + limit: 3 + }); + for (let i = 0; i < 3; i++) { //get number of characters in database - const characterId = Math.floor(Math.random() * await CardUtils.getCharacterCount()) + 1; + const characterId = characters[i].id console.log(`characterId: ${characterId}`); //random number between 1 and 6 let newCard = await Card.create({