From 088079cc0ebbd5acdf5d17baad6bae2e98f5555e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Gro=C3=9F?= Date: Mon, 5 Jun 2023 17:56:48 +0200 Subject: [PATCH] Commands: Add core missing command --- commands/missing.js | 96 +++++++++++++++++++++++++++++++++++ events/autocompleteRequest.js | 6 +++ models/character.js | 1 + 3 files changed, 103 insertions(+) create mode 100644 commands/missing.js diff --git a/commands/missing.js b/commands/missing.js new file mode 100644 index 0000000..2270062 --- /dev/null +++ b/commands/missing.js @@ -0,0 +1,96 @@ +const { SlashCommandBuilder, AttachmentBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } = require("discord.js"); +const { Card, User, Group, Character, Badge } = require("../models"); +const { Rendering, UserUtils } = require("../util"); +const { QUALITY_NAMES } = require("../config/constants"); +const Sequelize = require("sequelize"); +const fs = require("fs"); +const edit = require("./edit"); + +//fetch all cards owned by the user and list them +module.exports = { + data: new SlashCommandBuilder() + .setName("missing") + .setDescription("View missing things") + .addStringOption((option) => + option + .setName("group_id") + .setDescription("Thing identifier") + .setRequired(false) + .setAutocomplete(true) + ), + permissionLevel: 0, + async execute(interaction) { + await interaction.deferReply(); + + let groupId = interaction.options.getString("group_id"); + let user = await UserUtils.getUserByDiscordId(interaction.member.user.id); + + if (groupId) { + try { + let missingCards = await this.FindMissingFromGroup(user.id, groupId); + await interaction.editReply(JSON.stringify(missingCards)); + return; + } catch (error) { + console.error('Error:', error); + throw error; + } + } + + let missingCount = await this.CountMissingFromGroup(user.id); + await interaction.editReply(JSON.stringify(missingCount)); + return; + }, + async CountMissingFromGroup(userId) { + try { + const groups = await Group.findAll({ + where: { enabled: 1 } + }); + + const missingCountByGroup = await Promise.all( + groups.map(async (group) => { + const characters = await Character.findAll({ + where: { groupId: group.id, enabled: 1 }, + include: [ + { + model: Card, + where: { userId: userId }, + required: false, + }, + ], + }); + + const missingCount = characters.reduce((total, character) => { + return total + (character.Cards.length === 0 ? 1 : 0); + }, 0); + + return { + groupId: group.id, + groupName: group.name, + missingCount: missingCount, + }; + }) + ); + + return missingCountByGroup; + } catch (error) { + console.error('Error:', error); + throw error; + } + }, + async FindMissingFromGroup(userId, groupId) { + const missingCards = await Character.findAll({ + attributes: ['groupId', 'id', 'name'], + include: [{ + model: Card, + attributes: [], + required: false, + where: { userId: userId, burned: 0 }, + }], + where: { groupId: groupId }, + having: Sequelize.literal('COUNT(Cards.id) = 0'), + group: ['Character.id'], + }); + + return missingCards; + } +} \ No newline at end of file diff --git a/events/autocompleteRequest.js b/events/autocompleteRequest.js index 8c4b4e8..abd729e 100644 --- a/events/autocompleteRequest.js +++ b/events/autocompleteRequest.js @@ -40,6 +40,12 @@ module.exports = { } } + if (interaction.commandName === 'missing') { + + choices = (await SearchUtils.findByName(Group, focusedOption.value))["choices"]; + + } + if (interaction.commandName === 'wishlist') { choices = (await SearchUtils.findByName(Character, focusedOption.value))["choices"]; } diff --git a/models/character.js b/models/character.js index e2dbb58..0507a23 100644 --- a/models/character.js +++ b/models/character.js @@ -13,6 +13,7 @@ module.exports = (sequelize, DataTypes) => { Character.belongsTo(models.Group, { foreignKey: 'groupId', }); // A character can belong to many badges Character.belongsToMany(models.Badge, { through: 'BadgeCharacter' }); + Character.hasMany(models.Card, { foreignKey: 'characterId' }); } } Character.init({