Commands: Add core missing command

This commit is contained in:
2023-06-05 17:56:48 +02:00
parent 5a47d0695f
commit 088079cc0e
3 changed files with 103 additions and 0 deletions

96
commands/missing.js Normal file
View File

@@ -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;
}
}

View File

@@ -40,6 +40,12 @@ module.exports = {
} }
} }
if (interaction.commandName === 'missing') {
choices = (await SearchUtils.findByName(Group, focusedOption.value))["choices"];
}
if (interaction.commandName === 'wishlist') { if (interaction.commandName === 'wishlist') {
choices = (await SearchUtils.findByName(Character, focusedOption.value))["choices"]; choices = (await SearchUtils.findByName(Character, focusedOption.value))["choices"];
} }

View File

@@ -13,6 +13,7 @@ module.exports = (sequelize, DataTypes) => {
Character.belongsTo(models.Group, { foreignKey: 'groupId', }); Character.belongsTo(models.Group, { foreignKey: 'groupId', });
// A character can belong to many badges // A character can belong to many badges
Character.belongsToMany(models.Badge, { through: 'BadgeCharacter' }); Character.belongsToMany(models.Badge, { through: 'BadgeCharacter' });
Character.hasMany(models.Card, { foreignKey: 'characterId' });
} }
} }
Character.init({ Character.init({