Wishlist: Add model and command

This commit is contained in:
2023-03-16 00:29:53 +01:00
parent b8db85e71c
commit 77e09ca5ce
2 changed files with 61 additions and 4 deletions

View File

@@ -1,4 +1,6 @@
const { SlashCommandBuilder } = require("discord.js");
const { Wishlist, Character } = require("../models");
const UserUtils = require("../util/users");
module.exports = {
data: new SlashCommandBuilder()
@@ -30,19 +32,47 @@ module.exports = {
.setAutocomplete(true)
)
),
permissionLevel: 1,
permissionLevel: 0,
async execute(interaction) {
await interaction.deferReply();
let user = await UserUtils.getUserByDiscordId(interaction.member.id);
let wishlist = await Wishlist.findOne({
where: { UserId: user.id },
include: [{ model: Character }]
})
if(!wishlist) {
wishlist = await Wishlist.create({
ping: false,
UserId: user.id
});
interaction.channel.send("Created new wishlist");
}
switch (interaction.options.getSubcommand()) {
case "view":
await interaction.editReply("Viewing your wishlist");
let reply = `Wishlist entries (${wishlist.Characters.length}/5 used):\n`;
wishlist.Characters.forEach(character => {
reply += `${character.name} \n`;
});
await interaction.editReply(reply);
break;
case "compare":
await interaction.editReply("Comparing your wishlist");
break;
case "edit":
await interaction.editReply("Editing your wishlist");
let character = await Character.findOne({
where: { id: interaction.options.getString("character") }});
if (await wishlist.hasCharacter(character)) {
await wishlist.removeCharacter(character)
await interaction.editReply(`Removed ${character.name} from your wishlist! ${wishlist.Characters.length-1}/5 used`);
} else {
if (wishlist.Characters.length < 5) {
await wishlist.addCharacter(character);
await interaction.editReply(`Added ${character.name} to your wishlist! ${wishlist.Characters.length+1}/5 used`);
} else {
await interaction.editReply(`You have no remaining wishlist slots! ${wishlist.Characters.length}`);
}
}
break;
default:
await interaction.editReply("hmmm");

27
models/wishlist.js Normal file
View File

@@ -0,0 +1,27 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Wishlist extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
models.User.hasOne(Wishlist);
Wishlist.belongsTo(models.User);
Wishlist.belongsToMany(models.Character, { through: 'WishlistCharacter' });
models.Character.belongsToMany(Wishlist, { through: 'WishlistCharacter' });
}
}
Wishlist.init({
ping: DataTypes.BOOLEAN,
}, {
sequelize,
modelName: 'Wishlist',
});
return Wishlist;
};