Ticket creation

This commit is contained in:
Isaac
2021-03-28 23:49:36 +01:00
parent 91c53f38e6
commit 82c3175f37
8 changed files with 135 additions and 55 deletions

View File

@@ -183,7 +183,7 @@ module.exports = class CommandManager {
const cmd = this.commands.get(cmd_name);
if (cmd.slash && !slash) {
this.client.log.commands(`Blocking command execution for the "${cmd_name}" command as it was invoked by a message, not a slash command interaction_or_message.`);
this.client.log.commands(`Blocking command execution for the "${cmd_name}" command as it was invoked by a message, not a slash command interaction.`);
try {
data.channel.send(i18n('must_be_slash', cmd_name)); // interaction_or_message.reply
} catch (err) {

View File

@@ -19,56 +19,110 @@ module.exports = class TicketManager extends EventEmitter {
/**
* Create a new ticket
* @param {string} guild - ID of the guild to create the ticket in
* @param {string} creator - ID of the ticket creator (user)
* @param {string} category - ID of the ticket category
* @param {string} guild_id - ID of the guild to create the ticket in
* @param {string} creator_id - ID of the ticket creator (user)
* @param {string} category_id - ID of the ticket category
* @param {string} [topic] - The ticket topic
*/
async create(guild, creator, category, topic) {
async create(guild_id, creator_id, category_id, topic) {
if (!topic) topic = '';
let cat_row = await this.client.db.models.Category.findOne({
where: {
id: category_id
}
});
if (!cat_row)
throw new Error('Ticket category does not exist');
let number = (await this.client.db.models.Ticket.count({
where: {
guild: guild_id
}
})) + 1;
let guild = await this.client.guilds.cache.get(guild_id);
let member = await guild.members.fetch(creator_id);
let name = cat_row.name_format
.replace(/{+\s?(user)?name\s?}+/gi, member.displayName)
.replace(/{+\s?number\s?}+/gi, number);
let t_channel = await guild.channels.create(name, {
type: 'text',
topic: `${member}${topic.length > 0 ? ` | ${topic}` : ''}`,
parent: category_id,
reason: `${member.tag} requested a new ticket channel`
});
let t_row = await this.client.db.models.Ticket.create({
id: t_channel.id,
number,
guild: guild_id,
category: category_id,
creator: creator_id,
topic
});
this.emit('create', t_row.id, creator_id);
}
/**
* Get a ticket
* @param {string} ticket - The channel ID, or the ticket number
* @param {(string|number)} ticket - The channel ID, or the ticket number
* @param {string} guild_id - The ID of the ticket's guild (used if a ticket number is provided instead of ID)
*/
async get(ticket) {
async get(ticket, guild_id) {
let row = await this.resolveTicket(ticket, guild_id);
if (!row) throw new Error(`Could not find a ticket with ID ${ticket}`);
}
/**
* Close a ticket
* @param {string} ticket - The channel ID, or the ticket number
* @param {string} [closer] - ID of the member who is closing the ticket
* @param {(string|number)} ticket - The channel ID, or the ticket number
* @param {(string|null)} 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)
*/
async close(ticket, closer) {
async close(ticket, closer_id, guild_id) {
let row = await this.resolveTicket(ticket, guild_id);
if (!row) throw new Error(`Could not find a ticket with ID ${ticket}`);
this.emit('beforeClose', ticket, closer_id);
/**
*
*
* for each message of ticket, create entities
*
*
*/
this.emit('close', ticket, closer_id);
}
/**
*
* @param {(string|number)} ticket - ID or number of the ticket
* @param {string} [guild_id] - The ID of the ticket's guild (used if a ticket number is provided instead of ID)
*/
async resolve(ticket, guild_id) {
if (!this.client.channels.resolve(ticket)) {
let row = await this.client.db.Models.Ticket.findOne({
let row = await this.client.db.models.Ticket.findOne({
where: {
number: ticket
number: ticket,
guild_id
}
});
if (!row) throw new Error(`Could not find a ticket with number ${ticket}`);
ticket = row.id;
ticket = row?.id;
}
let row = await this.client.db.Models.Ticket.findOne({
let row = await this.client.db.models.Ticket.findOne({
where: {
id: ticket
}
});
if (!row) throw new Error(`Could not find a ticket with ID ${ticket}`);
this.emit('beforeClose', ticket, closer);
/**
*
*
* for each message in table, create entities
*
*
*/
return row;
}
};