Badges: Add view/list commands and search util
This commit is contained in:
48
commands/badges.js
Normal file
48
commands/badges.js
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");
|
||||||
|
const { Card, User, Character } = require("../models");
|
||||||
|
const { QUALITY, QUALITY_NAMES } = require('../config/constants');
|
||||||
|
const { UserUtils, SearchUtils } = require("../util");
|
||||||
|
|
||||||
|
const pageSize = 8;
|
||||||
|
|
||||||
|
//fetch all cards owned by the user and list them
|
||||||
|
module.exports = {
|
||||||
|
data: new SlashCommandBuilder()
|
||||||
|
.setName("badges")
|
||||||
|
.setDescription("List badges")
|
||||||
|
.addStringOption((option) =>
|
||||||
|
option
|
||||||
|
.setName("filter")
|
||||||
|
.setDescription("Name or ID")
|
||||||
|
.setRequired(false)
|
||||||
|
.setAutocomplete(false)
|
||||||
|
)
|
||||||
|
.addBooleanOption(option =>
|
||||||
|
option.setName('owned')
|
||||||
|
.setDescription('Only show owned badges')
|
||||||
|
.setRequired(false)
|
||||||
|
),
|
||||||
|
permissionLevel: 0,
|
||||||
|
async execute(interaction) {
|
||||||
|
await interaction.deferReply();
|
||||||
|
let user = await UserUtils.getUserByDiscordId(interaction.member.id);
|
||||||
|
let filter = interaction.options.getString("filter");
|
||||||
|
let badges = await SearchUtils.findBadges(filter? filter:"", options={ 'user': user});
|
||||||
|
let badgesStr = "Badges";
|
||||||
|
|
||||||
|
badges['rows'].forEach(badge => {
|
||||||
|
badgesStr += `${badge.name} \n`;
|
||||||
|
});
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle(`This is where badges would show up`)
|
||||||
|
.setDescription(`if it was implemented`)
|
||||||
|
.addFields(
|
||||||
|
{ name: "Placeholder:", value: `${badgesStr}` },
|
||||||
|
)
|
||||||
|
.setColor(0x00ff00)
|
||||||
|
.setFooter({ text: `Badges listed by ${interaction.member.displayName}` });
|
||||||
|
|
||||||
|
let replyPayload = { embeds: [embed], fetchReply: true }
|
||||||
|
const message = await interaction.editReply(replyPayload);
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -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, Group, Character } = require("../models");
|
const { Card, User, Group, Character, Badge } = 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");
|
||||||
@@ -9,13 +9,14 @@ const edit = require("./edit");
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName("view")
|
.setName("view")
|
||||||
.setDescription("View a specific card")
|
.setDescription("View a specific thing")
|
||||||
.addStringOption((option) =>
|
.addStringOption((option) =>
|
||||||
option
|
option
|
||||||
.setName("type")
|
.setName("type")
|
||||||
.setDescription("The thing to view")
|
.setDescription("The thing to view")
|
||||||
.setRequired(true)
|
.setRequired(true)
|
||||||
.addChoices(
|
.addChoices(
|
||||||
|
{ name: 'badge', value: 'badge' },
|
||||||
{ name: 'card', value: 'card' },
|
{ name: 'card', value: 'card' },
|
||||||
{ name: 'character', value: 'character' },
|
{ name: 'character', value: 'character' },
|
||||||
{ name: 'group', value: 'group' }
|
{ name: 'group', value: 'group' }
|
||||||
@@ -33,6 +34,9 @@ module.exports = {
|
|||||||
await interaction.deferReply();
|
await interaction.deferReply();
|
||||||
|
|
||||||
switch (interaction.options.getString("type")) {
|
switch (interaction.options.getString("type")) {
|
||||||
|
case "badge":
|
||||||
|
this.viewBadge(interaction, interaction.options.getString("id"));
|
||||||
|
break;
|
||||||
case "card":
|
case "card":
|
||||||
this.viewCard(interaction, interaction.options.getString("id"));
|
this.viewCard(interaction, interaction.options.getString("id"));
|
||||||
break;
|
break;
|
||||||
@@ -168,5 +172,30 @@ module.exports = {
|
|||||||
await edit.execute(m, 'character', character.id);
|
await edit.execute(m, 'character', character.id);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
async viewBadge(interaction, badgeId) {
|
||||||
|
let badge = await Badge.findOne({
|
||||||
|
where: { id: badgeId },
|
||||||
|
include: [Character]
|
||||||
|
});
|
||||||
|
|
||||||
|
let required = "";
|
||||||
|
badge.Characters.forEach(character => {
|
||||||
|
required += `_[${character.id}]_ ${character.name} \n`;
|
||||||
|
});
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle(`${badge.name}`)
|
||||||
|
.setDescription(badge.description)
|
||||||
|
.setImage(badge.image)
|
||||||
|
.addFields(
|
||||||
|
{ name: "Required characters:", value: `${required}` },
|
||||||
|
)
|
||||||
|
.setColor(0x00ff00)
|
||||||
|
.setFooter({ text: `Badge viewed by ${interaction.member.displayName}` });
|
||||||
|
|
||||||
|
let replyPayload = { embeds: [embed], fetchReply: true }
|
||||||
|
const message = await interaction.editReply(replyPayload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
const Sequelize = require('sequelize');
|
const Sequelize = require('sequelize');
|
||||||
const { Card, Character, User } = require('../models');
|
const { Card, Character, User, Badge } = require('../models');
|
||||||
const { QUALITY_NAMES } = require('../config/constants');
|
const { QUALITY_NAMES } = require('../config/constants');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@@ -54,5 +54,34 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
return { "rows": cards, "choices": choices };
|
return { "rows": cards, "choices": choices };
|
||||||
|
},
|
||||||
|
|
||||||
|
findBadges: async function(search, options={}, lim=0) {
|
||||||
|
let choices = [];
|
||||||
|
let condition = {
|
||||||
|
where: {
|
||||||
|
[Sequelize.Op.or]: [
|
||||||
|
{name: { [Sequelize.Op.like]: `%${search}%` }},
|
||||||
|
{id: { [Sequelize.Op.like]: `%${search}%` }}
|
||||||
|
],
|
||||||
|
},
|
||||||
|
include: [{ model: Character }, { model: User }],
|
||||||
|
}
|
||||||
|
if (options.ownedOnly) {
|
||||||
|
condition.where.userId = { [Sequelize.Op.eq]: options.user.id };
|
||||||
|
}
|
||||||
|
|
||||||
|
const badges = await Badge.findAll(condition);
|
||||||
|
for (let i = 0; i < badges.length; i++) {
|
||||||
|
let owned = "";
|
||||||
|
if (options.user) {
|
||||||
|
owned = badges[i].userId === options.user.id ? " (owned)" : "";
|
||||||
|
}
|
||||||
|
choices.push({
|
||||||
|
name: `${badges[i].id} - ${badges[i].name} ${owned}`,
|
||||||
|
value: badges[i].id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return { "rows": badges, "choices": choices };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user