Generalize labels and options

This commit is contained in:
2023-02-25 00:26:04 +01:00
parent 50da918be5
commit 765e63b90e
7 changed files with 64 additions and 64 deletions

View File

@@ -1,5 +1,5 @@
const { SlashCommandBuilder, AttachmentBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } = require("discord.js"); const { SlashCommandBuilder, AttachmentBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } = require("discord.js");
const { Card, User, Band, Character } = require("../models"); const { Card, User, Group, Character } = require("../models");
const { QUALITY_VALUES, QUALITY_NAMES, CURRENCY_SYMBOLS } = require("../config/constants"); const { QUALITY_VALUES, QUALITY_NAMES, CURRENCY_SYMBOLS } = require("../config/constants");
const { UserUtils } = require("../util"); const { UserUtils } = require("../util");
const fs = require("fs"); const fs = require("fs");
@@ -26,7 +26,7 @@ module.exports = {
identifier: interaction.options.getString("id") identifier: interaction.options.getString("id")
}, },
include: [ include: [
{ model: Character, include: [{ model: Band }] }, { model: Character, include: [{ model: Group }] },
{ model: User} { model: User}
] ]
}); });
@@ -47,7 +47,7 @@ module.exports = {
.setTitle(`${interaction.member.displayName} burned ${card.identifier}`) .setTitle(`${interaction.member.displayName} burned ${card.identifier}`)
.setDescription(`+${QUALITY_VALUES[card.quality].value} ${CURRENCY_SYMBOLS[QUALITY_VALUES[card.quality].type]}`) .setDescription(`+${QUALITY_VALUES[card.quality].value} ${CURRENCY_SYMBOLS[QUALITY_VALUES[card.quality].type]}`)
.addFields( .addFields(
{ name: `${card.Character.name}`, value: `${card.Character.Band.name}` }, { name: `${card.Character.name}`, value: `${card.Character.Group.name}` },
{ name: 'Print Number', value: `${card.printNr}`, inline: true }, { name: 'Print Number', value: `${card.printNr}`, inline: true },
{ name: 'Quality', value: `${QUALITY_NAMES[card.quality]}`, inline: true } { name: 'Quality', value: `${QUALITY_NAMES[card.quality]}`, inline: true }
) )

View File

@@ -19,8 +19,8 @@ module.exports = {
) )
.addStringOption((option) => .addStringOption((option) =>
option option
.setName("band") .setName("group")
.setDescription("Band to filter by") .setDescription("Group to filter by")
.setRequired(false) .setRequired(false)
.setAutocomplete(true) .setAutocomplete(true)
) )
@@ -57,7 +57,7 @@ module.exports = {
const filter = { const filter = {
character: interaction.options.getString("character"), character: interaction.options.getString("character"),
band: interaction.options.getString("band"), group: interaction.options.getString("group"),
quality: interaction.options.getString("quality") quality: interaction.options.getString("quality")
} }
@@ -145,8 +145,8 @@ module.exports = {
filter["where"]["characterId"] = filterParam["character"]; filter["where"]["characterId"] = filterParam["character"];
} }
if (filterParam["band"]) { if (filterParam["group"]) {
filter["where"]['$Character.bandId$'] = filterParam["band"]; filter["where"]['$Character.groupId$'] = filterParam["group"];
} }
if (filterParam["quality"]) { if (filterParam["quality"]) {

View File

@@ -1,5 +1,5 @@
const { SlashCommandBuilder, ComponentType, ActionRowBuilder, ButtonBuilder, ButtonStyle, ModalBuilder, TextInputStyle, TextInputBuilder } = require("discord.js"); const { SlashCommandBuilder, ComponentType, ActionRowBuilder, ButtonBuilder, ButtonStyle, ModalBuilder, TextInputStyle, TextInputBuilder } = require("discord.js");
const { Character, Band, RecordHistory } = require("../models"); const { Character, Group, RecordHistory } = require("../models");
const { UserUtils, GeneralUtils } = require("../util"); const { UserUtils, GeneralUtils } = require("../util");
const axios = require("axios"); const axios = require("axios");
const fs = require("fs"); const fs = require("fs");
@@ -15,7 +15,7 @@ module.exports = {
.setRequired(true) .setRequired(true)
.addChoices( .addChoices(
{ name: 'character', value: 'character' }, { name: 'character', value: 'character' },
{ name: 'band', value: 'band' } { name: 'group', value: 'group' }
) )
) )
.addStringOption((option) => .addStringOption((option) =>
@@ -39,14 +39,14 @@ module.exports = {
let record; let record;
switch (interaction.options?.getString("type") ?? type) { switch (interaction.options?.getString("type") ?? type) {
case "character": case "character":
record = await Character.findByPk(interaction.options?.getString("id") ?? id, { include: Band }); record = await Character.findByPk(interaction.options?.getString("id") ?? id, { include: Group });
if (!record) { if (!record) {
interaction.editReply({ content: "Character not found" }); interaction.editReply({ content: "Character not found" });
return; return;
} }
options = [ "name", "description", "imageIdentifier" ]; options = [ "name", "description", "imageIdentifier" ];
break; break;
case "band": case "group":
options = [ "name", "description" ]; options = [ "name", "description" ];
break; break;
default: default:
@@ -102,7 +102,7 @@ module.exports = {
await m.reply({ content: "An invalid image has been attached", ephemeral: true }); await m.reply({ content: "An invalid image has been attached", ephemeral: true });
return; return;
} }
let identifier = `${record.Band.name.replace(" ", "_")}/${image.name}`; let identifier = `${record.Group.name.replace(" ", "_")}/${image.name}`;
let path = `/app/assets/cards/${identifier}`; let path = `/app/assets/cards/${identifier}`;
await this.downloadImage(image.attachment, path); await this.downloadImage(image.attachment, path);
await this.updateRecord(user, record, option, identifier); await this.updateRecord(user, record, option, identifier);

View File

@@ -1,5 +1,5 @@
const { SlashCommandBuilder, AttachmentBuilder } = require("discord.js"); const { SlashCommandBuilder, AttachmentBuilder } = require("discord.js");
const { Card, Character, Band } = require("../models"); const { Card, Character, Group } = require("../models");
//fetch all cards owned by the user and list them //fetch all cards owned by the user and list them
module.exports = { module.exports = {
@@ -11,7 +11,7 @@ module.exports = {
.setDescription('Entity type') .setDescription('Entity type')
.setRequired(true) .setRequired(true)
.addChoices( .addChoices(
{ name: 'Band', value: 'band' }, { name: 'Group', value: 'group' },
{ name: 'Character', value: 'character' }, { name: 'Character', value: 'character' },
{ name: 'DEBUG', value: 'debug' }, { name: 'DEBUG', value: 'debug' },
) )
@@ -36,38 +36,38 @@ module.exports = {
await interaction.deferReply(); await interaction.deferReply();
const type = interaction.options.getString('type'); const type = interaction.options.getString('type');
if (type === 'band') { if (type === 'group') {
let band = await Band.findOne({ let group = await Group.findOne({
where: { where: {
id: interaction.options.getInteger('id') id: interaction.options.getInteger('id')
} }
}); });
if (!band) { if (!group) {
interaction.editReply({ interaction.editReply({
content: "Band not found", content: "Group not found",
ephemeral: true ephemeral: true
}); });
return; return;
} }
await band.update({ await group.update({
enabled: interaction.options.getBoolean('enabled') enabled: interaction.options.getBoolean('enabled')
}); });
if (interaction.options.getBoolean('include_children')) { if (interaction.options.getBoolean('include_children')) {
let characters = await Character.findAll({ let characters = await Character.findAll({
where: { where: {
bandId: band.id groupId: group.id
} }
}); });
Character.update({ Character.update({
enabled: (interaction.options.getBoolean('enabled') ? 1 : 0)}, { enabled: (interaction.options.getBoolean('enabled') ? 1 : 0)}, {
where: { bandId: band.id } where: { groupId: group.id }
}); });
} }
await interaction.editReply({ await interaction.editReply({
content: `${band.name} is now ` content: `${group.name} is now `
+ `${(interaction.options.getBoolean('enabled') ? 'active' : 'inactive')} ` + `${(interaction.options.getBoolean('enabled') ? 'active' : 'inactive')} `
+ `(Including children: ${(interaction.options.getBoolean('include_children') ? 'yes' : 'no')})` + `(Including children: ${(interaction.options.getBoolean('include_children') ? 'yes' : 'no')})`
}); });

View File

@@ -1,5 +1,5 @@
const { SlashCommandBuilder, AttachmentBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } = require("discord.js"); const { SlashCommandBuilder, AttachmentBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } = require("discord.js");
const { Card, User, Band, Character } = require("../models"); const { Card, User, Group, Character } = require("../models");
const { Rendering, UserUtils } = require("../util"); const { Rendering, UserUtils } = require("../util");
const { QUALITY_NAMES } = require("../config/constants"); const { QUALITY_NAMES } = require("../config/constants");
const fs = require("fs"); const fs = require("fs");
@@ -18,7 +18,7 @@ module.exports = {
.addChoices( .addChoices(
{ name: 'card', value: 'card' }, { name: 'card', value: 'card' },
{ name: 'character', value: 'character' }, { name: 'character', value: 'character' },
{ name: 'band', value: 'band' } { name: 'group', value: 'group' }
) )
) )
.addStringOption((option) => .addStringOption((option) =>
@@ -39,8 +39,8 @@ module.exports = {
case "character": case "character":
this.viewCharacter(interaction, interaction.options.getString("id")); this.viewCharacter(interaction, interaction.options.getString("id"));
break; break;
case "band": case "group":
interaction.editReply({ content: "Band view is not yet implemented" }); interaction.editReply({ content: "Group view is not yet implemented" });
break; break;
} }
@@ -52,7 +52,7 @@ module.exports = {
identifier: cardIdentifier identifier: cardIdentifier
}, },
include: [ include: [
{ model: Character, include: [{ model: Band }] }, { model: Character, include: [{ model: Group }] },
{ model: User} { model: User}
] ]
}); });
@@ -83,10 +83,10 @@ module.exports = {
.setTitle(`${card.Character.name}`) .setTitle(`${card.Character.name}`)
.setDescription(description) .setDescription(description)
.setImage(`attachment://${filename}`) .setImage(`attachment://${filename}`)
.setThumbnail(card.Character.Band.imageURL) .setThumbnail(card.Character.Group.imageURL)
.addFields( .addFields(
{ name: "Owned by", value: `<@${card.User.discordId}>` }, { name: "Owned by", value: `<@${card.User.discordId}>` },
{ name: "Band", value: `${card.Character.Band.name}` }, { name: "Group", value: `${card.Character.Group.name}` },
{ name: "Character ID", value: `${card.Character.id}` }, { name: "Character ID", value: `${card.Character.id}` },
{ name: 'Print Number', value: `${card.printNr}`, inline: true }, { name: 'Print Number', value: `${card.printNr}`, inline: true },
{ name: 'Quality', value: `${QUALITY_NAMES[card.quality]} ${(card.quality === 6 ? '<a:sparklu:1019505245572837428>':'')}`, inline: true } { name: 'Quality', value: `${QUALITY_NAMES[card.quality]} ${(card.quality === 6 ? '<a:sparklu:1019505245572837428>':'')}`, inline: true }
@@ -105,7 +105,7 @@ module.exports = {
let isAdmin = await UserUtils.getPermissionLevel(interaction.member) == 2; let isAdmin = await UserUtils.getPermissionLevel(interaction.member) == 2;
let character = await Character.findOne({ let character = await Character.findOne({
where: { id: characterId }, where: { id: characterId },
include: [Band] include: [Group]
}); });
if (!character) { if (!character) {
interaction.editReply({ content: "Character not found" }); interaction.editReply({ content: "Character not found" });
@@ -139,9 +139,9 @@ module.exports = {
.setTitle(`${character.name}`) .setTitle(`${character.name}`)
.setDescription(description) .setDescription(description)
.setImage(`attachment://${filename}`) .setImage(`attachment://${filename}`)
.setThumbnail(character.Band.imageURL) .setThumbnail(character.Group.imageURL)
.addFields( .addFields(
{ name: "Band", value: `${character.Band.name}` }, { name: "Group", value: `${character.Group.name}` },
{ name: "Character ID", value: `${character.id}` }, { name: "Character ID", value: `${character.id}` },
) )
.setColor(0x00ff00) .setColor(0x00ff00)

View File

@@ -1,6 +1,6 @@
const { InteractionType } = require('discord.js'); const { InteractionType } = require('discord.js');
const { UserUtils } = require('../util'); const { UserUtils } = require('../util');
const { Card, Character, User, Band } = require('../models'); const { Card, Character, User, Group } = require('../models');
const Sequelize = require('sequelize'); const Sequelize = require('sequelize');
const { QUALITY_NAMES } = require('../config/constants'); const { QUALITY_NAMES } = require('../config/constants');
const { TestStore } = require('../stores'); const { TestStore } = require('../stores');
@@ -49,13 +49,13 @@ module.exports = {
}); });
} }
break; break;
case 'band': case 'group':
break; break;
} }
} }
if (interaction.commandName === 'collection') { if (interaction.commandName === 'collection') {
const character = interaction.options.getString('character'); const character = interaction.options.getString('character');
const band = interaction.options.getString('band'); const group = interaction.options.getString('group');
const quality = interaction.options.getString('quality'); const quality = interaction.options.getString('quality');
//TODO: avoid duplicate code hehe //TODO: avoid duplicate code hehe
switch (focusedOption.name) { switch (focusedOption.name) {
@@ -76,9 +76,9 @@ module.exports = {
}); });
} }
break; break;
case 'band': case 'group':
if(focusedOption.value.length < 3) break; if(focusedOption.value.length < 3) break;
const bands = await Band.findAll({ const groups = await Group.findAll({
where: { where: {
name: { name: {
[Sequelize.Op.like]: `%${focusedOption.value}%` [Sequelize.Op.like]: `%${focusedOption.value}%`
@@ -86,10 +86,10 @@ module.exports = {
}, },
limit: 10 limit: 10
}); });
for (let i = 0; i < bands.length; i++) { for (let i = 0; i < groups.length; i++) {
choices.push({ choices.push({
name: bands[i].name, name: groups[i].name,
value: `${bands[i].id}` value: `${groups[i].id}`
}); });
} }
break; break;

View File

@@ -2,7 +2,7 @@ require("dotenv").config();
const { Console } = require("console"); const { Console } = require("console");
const fs = require("fs"); const fs = require("fs");
const dbUtil = require("./util/db") const dbUtil = require("./util/db")
const { Band, Character } = require("./models"); const { Group, Character } = require("./models");
const logger = new Console({ const logger = new Console({
stdout: process.stdout, stdout: process.stdout,
@@ -13,37 +13,37 @@ const logger = new Console({
async function runImport() { async function runImport() {
dbUtil.syncDb(); dbUtil.syncDb();
db = dbUtil.getDb(); db = dbUtil.getDb();
await importBands(); await importGroups();
await importCharacters(); await importCharacters();
} }
async function importBands() { async function importGroups() {
const bandFiles = fs.readdirSync("./assets/import/bands").filter(file => file.endsWith(".json")); const groupFiles = fs.readdirSync("./assets/import/groups").filter(file => file.endsWith(".json"));
//read json file and parse it into a javascript object //read json file and parse it into a javascript object
for (const file of bandFiles) { for (const file of groupFiles) {
let band = fs.readFileSync(`./assets/import/bands/${file}`); let group = fs.readFileSync(`./assets/import/groups/${file}`);
band = JSON.parse(band); group = JSON.parse(group);
logger.log(`Importing band: ${band.name}`); logger.log(`Importing group: ${group.name}`);
//check if band exists in database //check if group exists in database
let existingBand = await db.Band.findOne({ let existingGroup = await db.Group.findOne({
where: { where: {
name: band.name name: group.name
} }
}) })
if (existingBand) { if (existingGroup) {
logger.log(`Band ${band.name} already exists in database`); logger.log(`Group ${group.name} already exists in database`);
continue; continue;
} else { } else {
//create band in database //create group in database
await db.Band.create({ await db.Group.create({
id: band.id, id: group.id,
name: band.name, name: group.name,
description: band.description, description: group.description,
imageURL: band.imageURL, imageURL: group.imageURL,
enabled: band.enabled enabled: group.enabled
}); });
logger.log(`Created band ${band.name} in database`); logger.log(`Created group ${group.name} in database`);
} }
} }
} }
@@ -67,10 +67,10 @@ async function importCharacters() {
logger.log(`Character ${character.name} already exists in database`); logger.log(`Character ${character.name} already exists in database`);
continue; continue;
} else { } else {
//create band in database //create group in database
await db.Character.create({ await db.Character.create({
id: character.id, id: character.id,
bandId: character.bandId, groupId: character.groupId,
name: character.name, name: character.name,
description: character.description, description: character.description,
imageIdentifier: character.imageIdentifier, imageIdentifier: character.imageIdentifier,