diff --git a/src/database/models/channel_entity.model.js b/src/database/models/channel_entity.model.js index 0a4f124..b795b60 100644 --- a/src/database/models/channel_entity.model.js +++ b/src/database/models/channel_entity.model.js @@ -5,13 +5,13 @@ module.exports = (client, sequelize) => { channel: { type: DataTypes.CHAR(19), allowNull: false, - unique: 'id-ticket' + unique: 'channel-ticket' }, name: DataTypes.TEXT, ticket: { type: DataTypes.CHAR(19), allowNull: false, - unique: 'id-ticket', + unique: 'channel-ticket', references: { model: DB_TABLE_PREFIX + 'tickets', key: 'id' diff --git a/src/database/models/role_entity.model.js b/src/database/models/role_entity.model.js index 0fbb9e6..d0a9d7c 100644 --- a/src/database/models/role_entity.model.js +++ b/src/database/models/role_entity.model.js @@ -10,12 +10,12 @@ module.exports = (client, sequelize) => { role: { type: DataTypes.CHAR(19), allowNull: false, - unique: 'id-ticket' + unique: 'role-ticket' }, ticket: { type: DataTypes.CHAR(19), allowNull: false, - unique: 'id-ticket', + unique: 'role-ticket', references: { model: DB_TABLE_PREFIX + 'tickets', key: 'id' diff --git a/src/database/models/user_entity.model.js b/src/database/models/user_entity.model.js index 5dfe940..72c8962 100644 --- a/src/database/models/user_entity.model.js +++ b/src/database/models/user_entity.model.js @@ -4,13 +4,12 @@ module.exports = (client, sequelize) => { sequelize.define('UserEntity', { avatar: DataTypes.STRING, bot: DataTypes.BOOLEAN, - colour: DataTypes.CHAR(6), discriminator: DataTypes.STRING, display_name: DataTypes.TEXT, ticket: { type: DataTypes.CHAR(19), allowNull: false, - unique: 'id-ticket', + unique: 'user-ticket', references: { model: DB_TABLE_PREFIX + 'tickets', key: 'id' @@ -19,9 +18,17 @@ module.exports = (client, sequelize) => { user: { type: DataTypes.CHAR(19), allowNull: false, - unique: 'id-ticket' + unique: 'user-ticket' }, username: DataTypes.TEXT, + role: { + type: DataTypes.CHAR(19), + allowNull: false, + references: { + model: DB_TABLE_PREFIX + 'role_entities', + key: 'role' + }, + }, }, { tableName: DB_TABLE_PREFIX + 'user_entities' }); diff --git a/src/modules/tickets/archives.js b/src/modules/tickets/archives.js index 159930d..d8df6b7 100644 --- a/src/modules/tickets/archives.js +++ b/src/modules/tickets/archives.js @@ -41,7 +41,7 @@ module.exports = class TicketArchives { createdAt: new Date(message.createdTimestamp) }, { transaction: t }); - this.updateEntities(message); + await this.updateEntities(message); } }); } catch (e) { @@ -71,7 +71,7 @@ module.exports = class TicketArchives { if (message.editedTimestamp) { m_row.edited = true; - this.updateEntities(message); + await this.updateEntities(message); } await m_row.save({ transaction: t }); // save changes @@ -105,37 +105,52 @@ module.exports = class TicketArchives { } async updateEntities(message) { + // message author + await this.updateMember(message.channel.id, message.member); - let m_row = await this.client.db.models.Message.findOne({ - where: { - id: message.id - } + // mentioned members + message.mentions.members.forEach(async member => { + await this.updateMember(message.channel.id, member); }); - if (!m_row) return; + // mentioned channels + message.mentions.channels.forEach(async channel => { + await this.updateChannel(message.channel.id, channel); + }); + + // mentioned roles + message.mentions.roles.forEach(async role => { + await this.updateRole(message.channel.id, role); + }); + } + + async updateMember(ticket_id, member) { + await this.updateRole(ticket_id, member.roles.highest); - // message author try { - await this.client.db.transaction(async t => { + await this.client.db.transaction(async t => { let u_model_data = { - user: message.author.id, - ticket: message.channel.id + user: member.user.id, + ticket: ticket_id }; let [u_row] = await this.client.db.models.UserEntity.findOrCreate({ where: u_model_data, - defaults: u_model_data, + defaults: { + ...u_model_data, + role: member.roles.highest.id + }, transaction: t }); await u_row.update({ - avatar: message.author.displayAvatarURL(), - username: this.encrypt(message.author.username), - discriminator: message.author.discriminator, - display_name: this.encrypt(message.member.displayName), - colour: message.member.displayColor === 0 ? null : int2hex(message.member.displayColor), - bot: message.author.bot - }, { transaction: t }); + avatar: member.user.displayAvatarURL(), + username: this.encrypt(member.user.username), + discriminator: member.user.discriminator, + display_name: this.encrypt(member.displayName), + role: member.roles.highest.id, + bot: member.user.bot + }, { transaction: t }); return u_row; }); @@ -143,89 +158,57 @@ module.exports = class TicketArchives { this.client.log.warn('Failed to update message author entity in ticket archive'); this.client.log.error(e); } + } - // mentioned members - message.mentions.members.forEach(async member => { - try { - await this.client.db.transaction(async t => { - let m_model_data = { - user: member.user.id, - ticket: message.channel.id - }; - let [m_row] = await this.client.db.models.UserEntity.findOrCreate({ - where: m_model_data, - defaults: m_model_data, - transaction: t - }); - - await m_row.update({ - avatar: member.user.displayAvatarURL(), - username: this.encrypt(member.user.username), - discriminator: member.user.discriminator, - display_name: this.encrypt(member.displayName), - colour: member.displayColor === 0 ? null : int2hex(member.displayColor), - bot: member.user.bot - }, { transaction: t }); - - return m_row; + async updateChannel(ticket_id, channel) { + try { + await this.client.db.transaction(async t => { + let c_model_data = { + channel: channel.id, + ticket: ticket_id + }; + let [c_row] = await this.client.db.models.ChannelEntity.findOrCreate({ + where: c_model_data, + defaults: c_model_data, + transaction: t }); - } catch (e) { - this.client.log.warn('Failed to update mentioned members entities in ticket archive'); - this.client.log.error(e); - } - }); - // mentioned channels - message.mentions.channels.forEach(async channel => { - try { - await this.client.db.transaction(async t => { - let c_model_data = { - channel: channel.id, - ticket: message.channel.id - }; - let [c_row] = await this.client.db.models.ChannelEntity.findOrCreate({ - where: c_model_data, - defaults: c_model_data, - transaction: t - }); - await c_row.update({ - name: this.encrypt(channel.name) - }, { transaction: t }); + await c_row.update({ + name: this.encrypt(channel.name) + }, { transaction: t }); - return c_row; + return c_row; + }); + } catch (e) { + this.client.log.warn('Failed to update mentioned channels entities in ticket archive'); + this.client.log.error(e); + } + } + + async updateRole(ticket_id, role) { + try { + await this.client.db.transaction(async t => { + let r_model_data = { + role: role.id, + ticket: ticket_id + }; + let [r_row] = await this.client.db.models.RoleEntity.findOrCreate({ + where: r_model_data, + defaults: r_model_data, + transaction: t }); - } catch (e) { - this.client.log.warn('Failed to update mentioned channels entities in ticket archive'); - this.client.log.error(e); - } - }); - // mentioned roles - message.mentions.roles.forEach(async role => { - try { - await this.client.db.transaction(async t => { - let r_model_data = { - role: role.id, - ticket: message.channel.id - }; - let [r_row] = await this.client.db.models.RoleEntity.findOrCreate({ - where: r_model_data, - defaults: r_model_data, - transaction: t - }); - await r_row.update({ - name: this.encrypt(role.name), - colour: role.color === 0 ? '7289DA' : int2hex(role.color) // 7289DA = 7506394 - }, { transaction: t }); + await r_row.update({ + name: this.encrypt(role.name), + colour: role.color === 0 ? '7289DA' : int2hex(role.color) // 7289DA = 7506394 + }, { transaction: t }); - return r_row; - }); - } catch (e) { - this.client.log.warn('Failed to update mentioned roles entities in ticket archive'); - this.client.log.error(e); - } - }); - + return r_row; + }); + } catch (e) { + this.client.log.warn('Failed to update mentioned roles entities in ticket archive'); + this.client.log.error(e); + } } }; \ No newline at end of file diff --git a/src/modules/tickets/manager.js b/src/modules/tickets/manager.js index 32e556f..bf55d71 100644 --- a/src/modules/tickets/manager.js +++ b/src/modules/tickets/manager.js @@ -1,7 +1,6 @@ const EventEmitter = require('events'); const TicketArchives = require('./archives'); const { MessageEmbed } = require('discord.js'); -const { int2hex } = require('../../utils'); const { footer } = require('../../utils/discord'); /** Manages tickets */ @@ -51,7 +50,7 @@ module.exports = class TicketManager extends EventEmitter { .setColor(settings.colour) .setAuthor(member.user.username, member.user.displayAvatarURL()) .setDescription(description) - .setFooter(settings.footer); + .setFooter(settings.footer, guild.iconURL()); if (topic) embed.addField(i18n('commands.new.opening_message.fields.topic'), topic); @@ -108,7 +107,7 @@ module.exports = class TicketManager extends EventEmitter { .setAuthor(member.user.username, member.user.displayAvatarURL()) .setDescription(description) .addField(i18n('commands.new.opening_message.fields.topic'), topic) - .setFooter(settings.footer) + .setFooter(settings.footer, guild.iconURL()) ); await message.react('✅'); collector.stop(); @@ -123,7 +122,7 @@ module.exports = class TicketManager extends EventEmitter { new MessageEmbed() .setColor(settings.colour) .setDescription(i18n('commands.new.questions', questions)) - .setFooter(settings.footer) + .setFooter(settings.footer, guild.iconURL()) ); } }); @@ -133,7 +132,7 @@ module.exports = class TicketManager extends EventEmitter { new MessageEmbed() .setColor(settings.colour) .setDescription(i18n('commands.new.questions', questions)) - .setFooter(settings.footer) + .setFooter(settings.footer, guild.iconURL()) ); } } @@ -231,25 +230,9 @@ module.exports = class TicketManager extends EventEmitter { let channel = await this.client.channels.fetch(t_row.channel); if (closer_id) { - let u_model_data = { - user: closer_id, - ticket: ticket_id - }; - let [u_row] = await this.client.db.models.UserEntity.findOrCreate({ - where: u_model_data, - defaults: u_model_data - }); - let member = await guild.members.fetch(closer_id); - await u_row.update({ - avatar: member.user.displayAvatarURL(), - username: member.user.username, - discriminator: member.user.discriminator, - display_name: member.displayName, - colour: member.displayColor === 0 ? null : int2hex(member.displayColor), - bot: member.user.bot - }); + await this.archives.updateMember(ticket_id, member); if (channel) { let description = reason @@ -261,7 +244,7 @@ module.exports = class TicketManager extends EventEmitter { .setAuthor(member.user.username, member.user.displayAvatarURL()) .setTitle(i18n('commands.close.response.closed.title')) .setDescription(description) - .setFooter(settings.footer) + .setFooter(settings.footer, guild.iconURL()) ); setTimeout(async () => { @@ -280,7 +263,7 @@ module.exports = class TicketManager extends EventEmitter { .setColor(settings.success_colour) .setTitle(i18n('commands.close.response.closed.title')) .setDescription(description) - .setFooter(settings.footer) + .setFooter(settings.footer, guild.iconURL()) ); setTimeout(async () => {