Improve archiving

* All users have a role field rather than just a colour (for members sidebar)
This commit is contained in:
Isaac 2021-05-10 14:50:34 +01:00
parent a7248d88ea
commit 718101bfa2
No known key found for this signature in database
GPG Key ID: F6812DBC6719B4E3
5 changed files with 99 additions and 126 deletions

View File

@ -5,13 +5,13 @@ module.exports = (client, sequelize) => {
channel: { channel: {
type: DataTypes.CHAR(19), type: DataTypes.CHAR(19),
allowNull: false, allowNull: false,
unique: 'id-ticket' unique: 'channel-ticket'
}, },
name: DataTypes.TEXT, name: DataTypes.TEXT,
ticket: { ticket: {
type: DataTypes.CHAR(19), type: DataTypes.CHAR(19),
allowNull: false, allowNull: false,
unique: 'id-ticket', unique: 'channel-ticket',
references: { references: {
model: DB_TABLE_PREFIX + 'tickets', model: DB_TABLE_PREFIX + 'tickets',
key: 'id' key: 'id'

View File

@ -10,12 +10,12 @@ module.exports = (client, sequelize) => {
role: { role: {
type: DataTypes.CHAR(19), type: DataTypes.CHAR(19),
allowNull: false, allowNull: false,
unique: 'id-ticket' unique: 'role-ticket'
}, },
ticket: { ticket: {
type: DataTypes.CHAR(19), type: DataTypes.CHAR(19),
allowNull: false, allowNull: false,
unique: 'id-ticket', unique: 'role-ticket',
references: { references: {
model: DB_TABLE_PREFIX + 'tickets', model: DB_TABLE_PREFIX + 'tickets',
key: 'id' key: 'id'

View File

@ -4,13 +4,12 @@ module.exports = (client, sequelize) => {
sequelize.define('UserEntity', { sequelize.define('UserEntity', {
avatar: DataTypes.STRING, avatar: DataTypes.STRING,
bot: DataTypes.BOOLEAN, bot: DataTypes.BOOLEAN,
colour: DataTypes.CHAR(6),
discriminator: DataTypes.STRING, discriminator: DataTypes.STRING,
display_name: DataTypes.TEXT, display_name: DataTypes.TEXT,
ticket: { ticket: {
type: DataTypes.CHAR(19), type: DataTypes.CHAR(19),
allowNull: false, allowNull: false,
unique: 'id-ticket', unique: 'user-ticket',
references: { references: {
model: DB_TABLE_PREFIX + 'tickets', model: DB_TABLE_PREFIX + 'tickets',
key: 'id' key: 'id'
@ -19,9 +18,17 @@ module.exports = (client, sequelize) => {
user: { user: {
type: DataTypes.CHAR(19), type: DataTypes.CHAR(19),
allowNull: false, allowNull: false,
unique: 'id-ticket' unique: 'user-ticket'
}, },
username: DataTypes.TEXT, 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' tableName: DB_TABLE_PREFIX + 'user_entities'
}); });

View File

@ -41,7 +41,7 @@ module.exports = class TicketArchives {
createdAt: new Date(message.createdTimestamp) createdAt: new Date(message.createdTimestamp)
}, { transaction: t }); }, { transaction: t });
this.updateEntities(message); await this.updateEntities(message);
} }
}); });
} catch (e) { } catch (e) {
@ -71,7 +71,7 @@ module.exports = class TicketArchives {
if (message.editedTimestamp) { if (message.editedTimestamp) {
m_row.edited = true; m_row.edited = true;
this.updateEntities(message); await this.updateEntities(message);
} }
await m_row.save({ transaction: t }); // save changes await m_row.save({ transaction: t }); // save changes
@ -105,37 +105,52 @@ module.exports = class TicketArchives {
} }
async updateEntities(message) { async updateEntities(message) {
// message author
await this.updateMember(message.channel.id, message.member);
let m_row = await this.client.db.models.Message.findOne({ // mentioned members
where: { message.mentions.members.forEach(async member => {
id: message.id 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 { try {
await this.client.db.transaction(async t => { await this.client.db.transaction(async t => {
let u_model_data = { let u_model_data = {
user: message.author.id, user: member.user.id,
ticket: message.channel.id ticket: ticket_id
}; };
let [u_row] = await this.client.db.models.UserEntity.findOrCreate({ let [u_row] = await this.client.db.models.UserEntity.findOrCreate({
where: u_model_data, where: u_model_data,
defaults: u_model_data, defaults: {
...u_model_data,
role: member.roles.highest.id
},
transaction: t transaction: t
}); });
await u_row.update({ await u_row.update({
avatar: message.author.displayAvatarURL(), avatar: member.user.displayAvatarURL(),
username: this.encrypt(message.author.username), username: this.encrypt(member.user.username),
discriminator: message.author.discriminator, discriminator: member.user.discriminator,
display_name: this.encrypt(message.member.displayName), display_name: this.encrypt(member.displayName),
colour: message.member.displayColor === 0 ? null : int2hex(message.member.displayColor), role: member.roles.highest.id,
bot: message.author.bot bot: member.user.bot
}, { transaction: t }); }, { transaction: t });
return u_row; 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.warn('Failed to update message author entity in ticket archive');
this.client.log.error(e); this.client.log.error(e);
} }
}
// mentioned members async updateChannel(ticket_id, channel) {
message.mentions.members.forEach(async member => { try {
try { await this.client.db.transaction(async t => {
await this.client.db.transaction(async t => { let c_model_data = {
let m_model_data = { channel: channel.id,
user: member.user.id, ticket: ticket_id
ticket: message.channel.id };
}; let [c_row] = await this.client.db.models.ChannelEntity.findOrCreate({
let [m_row] = await this.client.db.models.UserEntity.findOrCreate({ where: c_model_data,
where: m_model_data, defaults: c_model_data,
defaults: m_model_data, transaction: t
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;
}); });
} catch (e) {
this.client.log.warn('Failed to update mentioned members entities in ticket archive');
this.client.log.error(e);
}
});
// mentioned channels await c_row.update({
message.mentions.channels.forEach(async channel => { name: this.encrypt(channel.name)
try { }, { transaction: t });
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 });
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 await r_row.update({
message.mentions.roles.forEach(async role => { name: this.encrypt(role.name),
try { colour: role.color === 0 ? '7289DA' : int2hex(role.color) // 7289DA = 7506394
await this.client.db.transaction(async t => { }, { transaction: 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 });
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);
}
} }
}; };

View File

@ -1,7 +1,6 @@
const EventEmitter = require('events'); const EventEmitter = require('events');
const TicketArchives = require('./archives'); const TicketArchives = require('./archives');
const { MessageEmbed } = require('discord.js'); const { MessageEmbed } = require('discord.js');
const { int2hex } = require('../../utils');
const { footer } = require('../../utils/discord'); const { footer } = require('../../utils/discord');
/** Manages tickets */ /** Manages tickets */
@ -51,7 +50,7 @@ module.exports = class TicketManager extends EventEmitter {
.setColor(settings.colour) .setColor(settings.colour)
.setAuthor(member.user.username, member.user.displayAvatarURL()) .setAuthor(member.user.username, member.user.displayAvatarURL())
.setDescription(description) .setDescription(description)
.setFooter(settings.footer); .setFooter(settings.footer, guild.iconURL());
if (topic) embed.addField(i18n('commands.new.opening_message.fields.topic'), topic); 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()) .setAuthor(member.user.username, member.user.displayAvatarURL())
.setDescription(description) .setDescription(description)
.addField(i18n('commands.new.opening_message.fields.topic'), topic) .addField(i18n('commands.new.opening_message.fields.topic'), topic)
.setFooter(settings.footer) .setFooter(settings.footer, guild.iconURL())
); );
await message.react('✅'); await message.react('✅');
collector.stop(); collector.stop();
@ -123,7 +122,7 @@ module.exports = class TicketManager extends EventEmitter {
new MessageEmbed() new MessageEmbed()
.setColor(settings.colour) .setColor(settings.colour)
.setDescription(i18n('commands.new.questions', questions)) .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() new MessageEmbed()
.setColor(settings.colour) .setColor(settings.colour)
.setDescription(i18n('commands.new.questions', questions)) .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); let channel = await this.client.channels.fetch(t_row.channel);
if (closer_id) { 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); let member = await guild.members.fetch(closer_id);
await u_row.update({ await this.archives.updateMember(ticket_id, member);
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
});
if (channel) { if (channel) {
let description = reason let description = reason
@ -261,7 +244,7 @@ module.exports = class TicketManager extends EventEmitter {
.setAuthor(member.user.username, member.user.displayAvatarURL()) .setAuthor(member.user.username, member.user.displayAvatarURL())
.setTitle(i18n('commands.close.response.closed.title')) .setTitle(i18n('commands.close.response.closed.title'))
.setDescription(description) .setDescription(description)
.setFooter(settings.footer) .setFooter(settings.footer, guild.iconURL())
); );
setTimeout(async () => { setTimeout(async () => {
@ -280,7 +263,7 @@ module.exports = class TicketManager extends EventEmitter {
.setColor(settings.success_colour) .setColor(settings.success_colour)
.setTitle(i18n('commands.close.response.closed.title')) .setTitle(i18n('commands.close.response.closed.title'))
.setDescription(description) .setDescription(description)
.setFooter(settings.footer) .setFooter(settings.footer, guild.iconURL())
); );
setTimeout(async () => { setTimeout(async () => {