More settings, update things

This commit is contained in:
Isaac 2021-04-24 20:34:09 +01:00
parent 393bdb6b79
commit fff423956d
No known key found for this signature in database
GPG Key ID: F6812DBC6719B4E3
9 changed files with 1398 additions and 1437 deletions

File diff suppressed because it is too large Load Diff

View File

@ -41,18 +41,21 @@ module.exports = class SettingsCommand extends Command {
if (c.id) {
// existing category
let category = await this.client.db.models.Category.findOne({
let cat_row = await this.client.db.models.Category.findOne({
where: {
id: c.id
}
});
category.max_per_member = c.max_per_member;
category.name = c.name;
category.name_format = c.name_format;
category.opening_message = c.opening_message;
category.require_topic = c.require_topic;
category.roles = c.roles;
category.save();
cat_row.image = c.image;
cat_row.max_per_member = c.max_per_member;
cat_row.name = c.name;
cat_row.name_format = c.name_format;
cat_row.opening_message = c.opening_message;
cat_row.opening_questions = c.opening_questions;
cat_row.require_topic = c.require_topic;
cat_row.roles = c.roles;
cat_row.survey = c.survey;
cat_row.save();
let cat_channel = await this.client.channels.fetch(c.id);
@ -95,35 +98,43 @@ module.exports = class SettingsCommand extends Command {
})
]
});
await this.client.db.models.Category.create({
id: cat_channel.id,
guild: message.guild.id,
image: c.image,
max_per_member: c.max_per_member,
name: c.name,
name_format: c.name_format,
guild: message.guild.id,
opening_message: c.opening_message,
opening_questions: c.opening_questions,
require_topic: c.require_topic,
roles: c.roles,
survey: c.survey
});
}
}
for (let survey in data.surveys) {
let survey_data = {
guild: message.guild.id,
name: survey,
};
let [s_row] = await this.client.db.models.Survey.findOrCreate({
where: survey_data,
defaults: survey_data
});
s_row.questions = data.surveys[survey];
await s_row.save();
}
this.client.log.success(`Updated guild settings for "${message.guild.name}"`);
message.channel.send(i18n('commands.settings.response.updated'));
} else {
// upload settings as json to be modified
let data = {
categories: [],
colour: settings.colour,
command_prefix: settings.command_prefix,
error_colour: settings.error_colour,
footer: settings.footer,
locale: settings.locale,
log_messages: settings.log_messages,
success_colour: settings.success_colour,
};
// upload settings as json to be edited
let categories = await this.client.db.models.Category.findAll({
where: {
@ -131,17 +142,40 @@ module.exports = class SettingsCommand extends Command {
}
});
data.categories = categories.map(c =>{
let surveys = await this.client.db.models.Survey.findAll({
where: {
guild: message.guild.id
}
});
let data = {
categories: categories.map(c => {
return {
id: c.id,
image: c.image,
max_per_member: c.max_per_member,
name: c.name,
name_format: c.name_format,
opening_message: c.opening_message,
opening_questions: c.opening_questions,
require_topic: c.require_topic,
roles: c.roles
roles: c.roles,
survey: c.survey
};
});
}),
colour: settings.colour,
command_prefix: settings.command_prefix,
error_colour: settings.error_colour,
footer: settings.footer,
locale: settings.locale,
log_messages: settings.log_messages,
success_colour: settings.success_colour,
surveys: {},
};
for (let survey in surveys) {
data.surveys[surveys[survey].name] = surveys[survey].questions;
}
let attachment = new MessageAttachment(
Buffer.from(JSON.stringify(data, null, 2)),

View File

@ -16,6 +16,10 @@ module.exports = ({ config }, sequelize) => {
},
unique: 'name-guild'
},
image: {
type: DataTypes.STRING,
allowNull: true,
},
max_per_member: {
type: DataTypes.INTEGER,
defaultValue: 1
@ -34,6 +38,10 @@ module.exports = ({ config }, sequelize) => {
type: DataTypes.STRING,
defaultValue: config.defaults.opening_message,
},
opening_questions: {
type: DataTypes.JSON,
allowNull: true,
},
require_topic: {
type: DataTypes.BOOLEAN,
defaultValue: true,
@ -42,10 +50,10 @@ module.exports = ({ config }, sequelize) => {
type: DataTypes.JSON,
allowNull: false,
},
questions: {
type: DataTypes.JSON,
survey: {
type: DataTypes.STRING,
allowNull: true,
},
}
}, {
tableName: DB_TABLE_PREFIX + 'categories'
});

View File

@ -0,0 +1,26 @@
const { DataTypes } = require('sequelize');
module.exports = (client, sequelize) => {
const { DB_TABLE_PREFIX } = process.env;
sequelize.define('Survey', {
guild: {
type: DataTypes.CHAR(18),
allowNull: false,
references: {
model: DB_TABLE_PREFIX + 'guilds',
key: 'id'
},
unique: 'name-guild'
},
name: {
type: DataTypes.STRING,
allowNull: false,
unique: 'name-guild'
},
questions: {
type: DataTypes.JSON,
allowNull: true,
},
}, {
tableName: DB_TABLE_PREFIX + 'surveys'
});
};

View File

@ -0,0 +1,29 @@
const { DataTypes } = require('sequelize');
module.exports = (client, sequelize) => {
const { DB_TABLE_PREFIX } = process.env;
sequelize.define('SurveyResponse', {
answers: {
type: DataTypes.JSON,
allowNull: true,
},
survey: {
type: DataTypes.CHAR(18),
allowNull: false,
references: {
model: DB_TABLE_PREFIX + 'surveys',
key: 'id'
},
},
ticket: {
type: DataTypes.CHAR(18),
allowNull: false,
unique: 'id-ticket',
references: {
model: DB_TABLE_PREFIX + 'tickets',
key: 'id'
},
},
}, {
tableName: DB_TABLE_PREFIX + 'survey_responses'
});
};

View File

@ -22,10 +22,12 @@
*/
process.title = 'Discord Tickets';
const node_version = Number(process.versions.node.split('.')[0]);
if (node_version < 14)
return console.log(`\x07Error: Discord Tickets does not work on Node v${node_version}. Please upgrade to v14 or above.`);
const leeks = require('leeks.js');
const fs = require('fs');
const { path } = require('./utils/fs');
@ -61,6 +63,7 @@ if (!checkFile('./.env', './example.env')) {
fs.writeFileSync(file, data);
console.log('Saved.');
console.log(leeks.colours.yellow('Warning: do not lose your ENV file or encryption key; you will lose access to data in the database.'));
console.log('\x07Please set your bot\'s "DISCORD_TOKEN" in "./.env".');
process.exit();

View File

@ -0,0 +1,17 @@
module.exports = {
event: 'guildMemberRemove',
execute: async (client, member) => {
let tickets = await client.db.models.Ticket.findAndCountAll({
where: {
creator: member.id,
guild: member.guild.id
}
});
for (let ticket of tickets.rows) {
client.tickets.close(ticket.id, null, member.guild.id, 'Member left the guild');
}
client.log.info(`Closed ${tickets.count} ticket(s) belonging to ${member.user.tag} who left "${member.guild.name}"`);
}
};

View File

@ -71,8 +71,20 @@
"name": "close",
"response": {
"closed": {
"title": "✅ Ticket closed",
"description": "This ticket has been closed.\nThe channel will be deleted in 5 seconds."
},
"closed_by_member": {
"title": "✅ Ticket closed",
"description": "This ticket has been closed by %s.\nThe channel will be deleted in 5 seconds."
},
"closed_by_member_with_reason": {
"title": "✅ Ticket closed",
"description": "This ticket has been closed by %s: `%s`\nThe channel will be deleted in 5 seconds."
},
"closed_with_reason": {
"title": "✅ Ticket closed",
"description": "This ticket has been closed: `%s`\nThe channel will be deleted in 5 seconds."
}
}
},

View File

@ -94,14 +94,21 @@ module.exports = class TicketManager extends EventEmitter {
* @param {(string|number)} ticket_id - The channel ID, or the ticket number
* @param {string?} closer_id - ID of the member who is closing the ticket, or null
* @param {string} [guild_id] - The ID of the ticket's guild (used if a ticket number is provided instead of ID)
* @param {string} [reason] - The reason for closing the ticket
*/
async close(ticket_id, closer_id, guild_id) {
async close(ticket_id, closer_id, guild_id, reason) {
let t_row = await this.resolve(ticket_id, guild_id);
if (!t_row) throw new Error(`Could not find a ticket with ID ${ticket_id}`);
ticket_id = t_row.id;
this.emit('beforeClose', ticket_id);
let guild = this.client.guilds.cache.get(t_row.guild);
let settings = await guild.settings;
const i18n = this.client.i18n.get(settings.locale);
let channel = await this.client.channels.fetch(t_row.channel);
if (closer_id) {
let u_model_data = {
user: closer_id,
ticket: ticket_id
@ -111,7 +118,6 @@ module.exports = class TicketManager extends EventEmitter {
defaults: u_model_data
});
let guild = this.client.guilds.cache.get(t_row.guild);
let member = await guild.members.fetch(closer_id);
await u_row.update({
@ -123,30 +129,49 @@ module.exports = class TicketManager extends EventEmitter {
bot: member.user.bot
});
await t_row.update({
open: false,
closed_by: closer_id
});
let channel = await this.client.channels.fetch(t_row.channel);
if (channel) {
let settings = await guild.settings;
const i18n = this.client.i18n.get(settings.locale);
let description = reason
? i18n('commands.close.response.closed_by_member_with_reason.description', member.user.toString(), reason)
: i18n('commands.close.response.closed_by_member.description', member.user.toString());
await channel.send(
new MessageEmbed()
.setColor(settings.success_colour)
.setTitle(i18n('commands.close.response.closed.title'))
.setDescription(i18n('commands.close.response.closed.description', member.user.toString()))
.setDescription(description)
);
setTimeout(async () => {
await channel.delete(`Ticket channel closed by ${member.user.tag}`);
await channel.delete(`Ticket channel closed by ${member.user.tag}${reason ? `: "${reason}"` : ''}`);
}, 5000);
}
this.client.log.info(`${member.user.tag} closed a ticket (${ticket_id})`);
this.client.log.info(`${member.user.tag} closed a ticket (${ticket_id})${reason ? `: "${reason}"` : ''}`);
} else {
if (channel) {
let description = reason
? i18n('commands.close.response.closed_with_reason.description')
: i18n('commands.close.response.closed.description');
await channel.send(
new MessageEmbed()
.setColor(settings.success_colour)
.setTitle(i18n('commands.close.response.closed.title'))
.setDescription(description)
);
setTimeout(async () => {
await channel.delete(`Ticket channel closed${reason ? `: "${reason}"` : ''}`);
}, 5000);
}
this.client.log.info(`A ticket was closed (${ticket_id})${reason ? `: "${reason}"` : ''}`);
}
await t_row.update({
open: false,
closed_by: closer_id,
reason: reason
});
this.emit('close', ticket_id);
return t_row;