From 1206251ff6f3ee657f03dcb90e60b7b96b54eaee Mon Sep 17 00:00:00 2001 From: Minzkraut Date: Mon, 5 Sep 2022 23:22:36 +0200 Subject: [PATCH] Profiles: Add user profile model and command --- commands/profile.js | 18 ++++++ migrations/20220905204829-create-profile.js | 68 +++++++++++++++++++++ models/profile.js | 32 ++++++++++ models/user.js | 16 +++++ 4 files changed, 134 insertions(+) create mode 100644 commands/profile.js create mode 100644 migrations/20220905204829-create-profile.js create mode 100644 models/profile.js diff --git a/commands/profile.js b/commands/profile.js new file mode 100644 index 0000000..0179945 --- /dev/null +++ b/commands/profile.js @@ -0,0 +1,18 @@ +const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js"); +const { Card, User, Character } = require("../models"); +const UserUtils = require("../util/users"); + +const pageSize = 8; + +//fetch all cards owned by the user and list them +module.exports = { + data: new SlashCommandBuilder() + .setName("profile") + .setDescription("View your profile"), + async execute(interaction) { + let user = await UserUtils.getUserByDiscordId(interaction.member.id); + + let profile = await user.getProfile(); + await interaction.reply(`json: ${JSON.stringify(profile)}`); + } +} \ No newline at end of file diff --git a/migrations/20220905204829-create-profile.js b/migrations/20220905204829-create-profile.js new file mode 100644 index 0000000..eca16b0 --- /dev/null +++ b/migrations/20220905204829-create-profile.js @@ -0,0 +1,68 @@ +'use strict'; +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable('Profiles', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + userId: { + type: Sequelize.INTEGER, + allowNull: false, + references: { + model: 'Users', + key: 'id' + } + }, + customStatus: { + type: Sequelize.STRING, + allowNull: true + }, + slotOne: { + type: Sequelize.INTEGER, + allowNull: true, + references: { + model: 'Cards', + key: 'id' + } + }, + slotTwo: { + type: Sequelize.INTEGER, + allowNull: true, + references: { + model: 'Cards', + key: 'id' + } + }, + slotThree: { + type: Sequelize.INTEGER, + allowNull: true, + references: { + model: 'Cards', + key: 'id' + } + }, + slotFour: { + type: Sequelize.INTEGER, + allowNull: true, + references: { + model: 'Cards', + key: 'id' + } + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + async down(queryInterface, Sequelize) { + await queryInterface.dropTable('Profiles'); + } +}; \ No newline at end of file diff --git a/models/profile.js b/models/profile.js new file mode 100644 index 0000000..bbed633 --- /dev/null +++ b/models/profile.js @@ -0,0 +1,32 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class Profile 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) { + Profile.belongsTo(models.User, { foreignKey: 'userId' }); + Profile.belongsTo(models.Card, { foreignKey: 'slotOne' }); + Profile.belongsTo(models.Card, { foreignKey: 'slotTwo' }); + Profile.belongsTo(models.Card, { foreignKey: 'slotThree' }); + Profile.belongsTo(models.Card, { foreignKey: 'slotFour' }); + } + } + Profile.init({ + userId: DataTypes.INTEGER, + customStatus: DataTypes.STRING, + slotOne: DataTypes.INTEGER, + slotTwo: DataTypes.INTEGER, + slotThree: DataTypes.INTEGER, + slotFour: DataTypes.INTEGER + }, { + sequelize, + modelName: 'Profile', + }); + return Profile; +}; \ No newline at end of file diff --git a/models/user.js b/models/user.js index c838a6f..10fd486 100644 --- a/models/user.js +++ b/models/user.js @@ -13,6 +13,7 @@ module.exports = (sequelize, DataTypes) => { static associate(models) { // define association here User.hasMany(models.Card); + User.hasOne(models.Profile); } //instance methods async getCardsWithCharactersCounted() { @@ -26,6 +27,13 @@ module.exports = (sequelize, DataTypes) => { }); return cards; } + async getProfile() { + return await sequelize.models.Profile.findOne({ + where: { + userId: this.id + } + }); + } } User.init({ discordId: DataTypes.BIGINT, @@ -35,6 +43,14 @@ module.exports = (sequelize, DataTypes) => { nextPull: DataTypes.DATE, nextDaily: DataTypes.DATE }, { + hooks: { + afterCreate: async (user, options) => { + //Create new user profile + await sequelize.models.Profile.create({ + userId: user.id + }); + } + }, sequelize, modelName: 'User', });