Fixes and improvements for database and other things

Also continued to add to ticket creation stuff - now includes opening questions
This commit is contained in:
Isaac 2021-04-25 15:40:07 +01:00
parent 674f29b403
commit db94bf6969
No known key found for this signature in database
GPG Key ID: F6812DBC6719B4E3
16 changed files with 245 additions and 189 deletions

View File

@ -3,12 +3,12 @@ module.exports = ({ config }, sequelize) => {
const { DB_TABLE_PREFIX } = process.env;
sequelize.define('Category', {
id: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
primaryKey: true,
allowNull: false,
},
guild: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
references: {
model: DB_TABLE_PREFIX + 'guilds',

View File

@ -3,13 +3,13 @@ module.exports = (client, sequelize) => {
const { DB_TABLE_PREFIX } = process.env;
sequelize.define('ChannelEntity', {
channel: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
unique: 'id-ticket'
},
name: DataTypes.TEXT,
ticket: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
unique: 'id-ticket',
references: {

View File

@ -3,7 +3,7 @@ module.exports = ({ config }, sequelize) => {
const { DB_TABLE_PREFIX } = process.env;
sequelize.define('Guild', {
id: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
primaryKey: true,
allowNull: false,
},

View File

@ -3,12 +3,12 @@ module.exports = (client, sequelize) => {
const { DB_TABLE_PREFIX } = process.env;
sequelize.define('Message', {
id: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
primaryKey: true,
allowNull: false,
},
author: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
},
data: {
@ -24,7 +24,7 @@ module.exports = (client, sequelize) => {
defaultValue: false,
},
ticket: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
references: {
model: DB_TABLE_PREFIX + 'tickets',

View File

@ -8,12 +8,12 @@ module.exports = (client, sequelize) => {
},
name: DataTypes.TEXT,
role: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
unique: 'id-ticket'
},
ticket: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
unique: 'id-ticket',
references: {

View File

@ -3,7 +3,7 @@ module.exports = (client, sequelize) => {
const { DB_TABLE_PREFIX } = process.env;
sequelize.define('Survey', {
guild: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
references: {
model: DB_TABLE_PREFIX + 'guilds',

View File

@ -7,17 +7,18 @@ module.exports = (client, sequelize) => {
allowNull: true,
},
survey: {
type: DataTypes.CHAR(18),
type: DataTypes.INTEGER,
allowNull: false,
unique: 'survey-ticket',
references: {
model: DB_TABLE_PREFIX + 'surveys',
key: 'id'
},
},
ticket: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
unique: 'id-ticket',
unique: 'survey-ticket',
references: {
model: DB_TABLE_PREFIX + 'tickets',
key: 'id'

View File

@ -3,12 +3,12 @@ module.exports = (client, sequelize) => {
const { DB_TABLE_PREFIX } = process.env;
sequelize.define('Ticket', {
id: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
primaryKey: true,
allowNull: false,
},
category: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
references: {
model: DB_TABLE_PREFIX + 'categories',
@ -16,7 +16,7 @@ module.exports = (client, sequelize) => {
},
},
closed_by: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: true,
},
closed_reason: {
@ -24,7 +24,7 @@ module.exports = (client, sequelize) => {
allowNull: true,
},
creator: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
},
first_response: {
@ -32,7 +32,7 @@ module.exports = (client, sequelize) => {
allowNull: true,
},
guild: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
references: {
model: DB_TABLE_PREFIX + 'guilds',
@ -50,7 +50,7 @@ module.exports = (client, sequelize) => {
defaultValue: true
},
opening_message: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: true,
},
topic: {

View File

@ -8,7 +8,7 @@ module.exports = (client, sequelize) => {
discriminator: DataTypes.STRING,
display_name: DataTypes.TEXT,
ticket: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
unique: 'id-ticket',
references: {
@ -17,7 +17,7 @@ module.exports = (client, sequelize) => {
},
},
user: {
type: DataTypes.CHAR(18),
type: DataTypes.CHAR(19),
allowNull: false,
unique: 'id-ticket'
},

View File

@ -6,41 +6,24 @@ module.exports = {
let settings = await message.guild.settings;
if (!settings) settings = await message.guild.createSettings();
let is_blacklisted = false;
if (settings.blacklist.includes(message.author.id)) {
is_blacklisted = true;
client.log.info(`Ignoring blacklisted member ${message.author.tag}`);
} else {
settings.blacklist.forEach(element => {
if (message.guild.roles.cache.has(element) && message.member.roles.cache.has(element)) {
is_blacklisted = true;
client.log.info(`Ignoring member ${message.author.tag} with blacklisted role`);
}
});
}
if (is_blacklisted) {
try {
return message.react('❌');
} catch (error) {
return client.log.debug('Failed to react to a message');
}
}
if (settings.log_messages && !message.system) client.tickets.archives.addMessage(message); // add the message to the archives (if it is in a ticket channel)
let t_row = await client.db.Ticket.findOne({
let t_row = await client.db.models.Ticket.findOne({
where: {
id: message.channel.id
}
});
if (t_row) {
if (settings.log_messages && !message.system) {
client.tickets.archives.addMessage(message); // add the message to the archives (if it is in a ticket channel)
}
const ignore = [client.user.id, t_row.creator];
if (t_row && !t_row.first_response && !ignore.includes(message.author.id)) {
if (!t_row.first_response && !ignore.includes(message.author.id)) {
t_row.update({
first_response: new Date()
});
}
}
client.commands.handle(message); // pass the message to the command handler
}

View File

@ -1 +0,0 @@
{}

View File

@ -102,6 +102,12 @@
},
"description": "Create a new ticket",
"name": "new",
"opening_message": {
"fields": {
"topic": "Topic"
}
},
"questions": "Please answer the following questions:\n\n%s",
"response": {
"created": {
"title": "✅ Ticket created",
@ -128,14 +134,9 @@
"description": "You took too long to select the ticket category."
}
},
"opening_message": {
"fields": {
"topic": "Topic"
}
},
"request_topic": {
"title": "⚠️ Ticket topic",
"description": "Please briefly state what this ticket is about in a short sentence."
"title": "Ticket topic",
"description": "Please briefly state what this ticket is about in a a few words."
}
},
"settings": {

View File

@ -61,6 +61,27 @@ module.exports = class CommandManager {
* @param {Message} message - Command message
*/
async handle(message) {
let is_blacklisted = false;
if (settings.blacklist.includes(message.author.id)) {
is_blacklisted = true;
this.client.log.info(`Ignoring blacklisted member ${message.author.tag}`);
} else {
settings.blacklist.forEach(element => {
if (message.guild.roles.cache.has(element) && message.member.roles.cache.has(element)) {
is_blacklisted = true;
this.client.log.info(`Ignoring member ${message.author.tag} with blacklisted role`);
}
});
}
if (is_blacklisted) {
try {
return message.react('❌');
} catch (error) {
return this.client.log.warn('Failed to react to a message');
}
}
let settings = await message.guild.settings;
const i18n = this.client.i18n.get(settings.locale);

View File

@ -17,10 +17,13 @@ module.exports = class TicketArchives {
}
async addMessage(message) {
try {
await this.client.db.transaction(async t => {
let t_row = await this.client.db.models.Ticket.findOne({
where: {
id: message.channel.id
}
},
transaction: t
});
if (t_row) {
@ -30,23 +33,31 @@ module.exports = class TicketArchives {
author: message.author.id,
data: this.encrypt(JSON.stringify({
content: message.content,
// time: message.createdTimestamp,
embeds: message.embeds.map(embed => {
return { embed };
}),
attachments: [...message.attachments.values()]
}))
});
})),
createdAt: new Date(message.createdTimestamp)
}, { transaction: t });
this.updateEntities(message);
}
});
} catch (e) {
this.client.log.warn('Failed to add a message to the ticket archive');
this.client.log.error(e);
}
}
async updateMessage(message) {
try {
await this.client.db.transaction(async t => {
let m_row = await this.client.db.models.Message.findOne({
where: {
id: message.id
}
},
transaction: t
});
if (m_row) {
@ -63,29 +74,44 @@ module.exports = class TicketArchives {
this.updateEntities(message);
}
await m_row.save(); // save changes
await m_row.save({ transaction: t }); // save changes
}
});
} catch (e) {
this.client.log.warn('Failed to update message in the ticket archive');
this.client.log.error(e);
}
}
async deleteMessage(message) {
try {
await this.client.db.transaction(async t => {
let msg = await this.client.db.models.Message.findOne({
where: {
id: message.id
}
},
transaction: t
});
if (msg) {
msg.deleted = true;
await msg.save(); // save changes to message row
await msg.save({ transaction: t }); // save changes to message row
}
});
} catch (e) {
this.client.log.warn('Failed to delete message in ticket archive');
this.client.log.error(e);
}
}
async updateEntities(message) {
try {
await this.client.db.transaction(async t => {
let m_row = await this.client.db.models.Message.findOne({
where: {
id: message.id
}
},
transaction: t
});
if (!m_row) return;
@ -97,8 +123,10 @@ module.exports = class TicketArchives {
};
let [u_row] = await this.client.db.models.UserEntity.findOrCreate({
where: u_model_data,
defaults: u_model_data
defaults: u_model_data,
transaction: t
});
await u_row.update({
avatar: message.author.displayAvatarURL(),
username: this.encrypt(message.author.username),
@ -106,7 +134,7 @@ module.exports = class TicketArchives {
display_name: this.encrypt(message.member.displayName),
colour: message.member.displayColor === 0 ? null : int2hex(message.member.displayColor),
bot: message.author.bot
});
}, { transaction: t });
// mentioned members
message.mentions.members.forEach(async member => {
@ -116,7 +144,8 @@ module.exports = class TicketArchives {
};
let [m_row] = await this.client.db.models.UserEntity.findOrCreate({
where: m_model_data,
defaults: m_model_data
defaults: m_model_data,
transaction: t
});
await m_row.update({
@ -126,7 +155,7 @@ module.exports = class TicketArchives {
display_name: this.encrypt(member.displayName),
colour: member.displayColor === 0 ? null : int2hex(member.displayColor),
bot: member.user.bot
});
}, { transaction: t });
});
// mentioned channels
@ -137,11 +166,12 @@ module.exports = class TicketArchives {
};
let [c_row] = await this.client.db.models.ChannelEntity.findOrCreate({
where: c_model_data,
defaults: c_model_data
defaults: c_model_data,
transaction: t
});
await c_row.update({
name: this.encrypt(channel.name)
});
}, { transaction: t });
});
// mentioned roles
@ -152,15 +182,19 @@ module.exports = class TicketArchives {
};
let [r_row] = await this.client.db.models.RoleEntity.findOrCreate({
where: r_model_data,
defaults: 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 });
});
});
} catch (e) {
this.client.log.warn('Failed to update message entities in ticket archive');
this.client.log.error(e);
}
}
};

View File

@ -2,6 +2,7 @@ const EventEmitter = require('events');
const TicketArchives = require('./archives');
const { MessageEmbed } = require('discord.js');
const { int2hex } = require('../../utils');
const { footer } = require('../../utils/discord');
/** Manages tickets */
module.exports = class TicketManager extends EventEmitter {
@ -69,13 +70,17 @@ module.exports = class TicketManager extends EventEmitter {
.catch(() => this.client.log.warn('Failed to delete system pin message'));
}
let questions = cat_row.opening_questions
.map((q, index) => `**${index + 1}.** ${q}`)
.join('\n\n');
if (cat_row.require_topic && topic.length === 0) {
let collector_message = await t_channel.send(
new MessageEmbed()
.setColor(settings.colour)
.setTitle(i18n('commands.new.request_topic.title'))
.setTitle('⚠️ ' + i18n('commands.new.request_topic.title'))
.setDescription(i18n('commands.new.request_topic.description'))
.setFooter(settings.footer)
.setFooter(footer(settings.footer, i18n('collector_expires_in', 120)), guild.iconURL())
);
const collector_filter = (message) => message.author.id === t_row.creator;
@ -99,17 +104,29 @@ module.exports = class TicketManager extends EventEmitter {
.setFooter(settings.footer)
);
await message.react('✅');
collector.stop();
});
collector.on('end', async (collected) => {
if (collected.size === 0) {
collector.on('end', async () => {
collector_message
.delete()
.catch(() => this.client.log.warn('Failed to delete topic collector message'));
}
await t_channel.send(
new MessageEmbed()
.setColor(settings.colour)
.setDescription(i18n('commands.new.questions', questions))
.setFooter(settings.footer)
);
});
} else {
if (cat_row.opening_questions.length > 0) {
await t_channel.send(
new MessageEmbed()
.setColor(settings.colour)
.setDescription(i18n('commands.new.questions', questions))
.setFooter(settings.footer)
);
}
}
}

View File

@ -11,7 +11,7 @@ module.exports = {
*/
footer: (text, additional) => {
if (text && additional) return `${text} | ${additional}`;
else return text || additional || '';
else return additional || text || '';
},
/**
* Select a presence from the config