fix(channels): close ticket when the channel is deleted

- change close function on manager so tickets without channel can be closed
- create an event listener to close tickets when someone deletes a channel
Note: The ticket will be closed when someone deletes the channel,
but it is supposed to be a fallback. Pinned messages will
not be stored.

Fixes #276
This commit is contained in:
Nikkl 2022-06-25 20:48:23 +02:00
parent 16ce243dde
commit 0ddd7c4166
2 changed files with 69 additions and 37 deletions

View File

@ -0,0 +1,23 @@
const EventListener = require('../modules/listeners/listener');
module.exports = class ChannelDeleteEventListener extends EventListener {
constructor(client) {
super(client, { event: 'channelDelete' });
}
async execute(channel) {
if (!channel.guild || channel.type !== 'GUILD_TEXT') return;
// resolve ticket by id
const t_row = await this.client.tickets.resolve(channel.id, channel.guild.id);
if (!t_row) return;
// fetch user from audit logs
const logEntry = (await channel.guild.fetchAuditLogs({ type: 'CHANNEL_DELETE' })).entries.find(entry =>
entry.target.id === channel.id
);
if (logEntry.executor.id === this.client.user.id) return;
await this.client.tickets.close(t_row.id, logEntry?.executor?.id || null, channel.guild.id, 'Channel was deleted');
}
};

View File

@ -236,15 +236,20 @@ module.exports = class TicketManager extends EventEmitter {
const guild = this.client.guilds.cache.get(t_row.guild); const guild = this.client.guilds.cache.get(t_row.guild);
const settings = await this.client.utils.getSettings(guild.id); const settings = await this.client.utils.getSettings(guild.id);
const i18n = this.client.i18n.getLocale(settings.locale); const i18n = this.client.i18n.getLocale(settings.locale);
const channel = await this.client.channels.fetch(t_row.id); const channel = await this.client.channels.cache.find(channel =>
channel.id === t_row.id
) ? await this.client.channels.fetch(t_row.id) : null;
const close = async () => { const close = async () => {
const pinned = await channel.messages.fetchPinned(); let pinned;
if (channel) {
pinned = await channel.messages.fetchPinned();
}
await t_row.update({ await t_row.update({
closed_by: closer_id || null, closed_by: closer_id || null,
closed_reason: reason ? this.client.cryptr.encrypt(reason) : null, closed_reason: reason ? this.client.cryptr.encrypt(reason) : null,
open: false, open: false,
pinned_messages: [...pinned.keys()] pinned_messages: pinned ? [...pinned.keys()] : []
}); });
if (closer_id) { if (closer_id) {
@ -252,6 +257,7 @@ module.exports = class TicketManager extends EventEmitter {
await this.archives.updateMember(ticket_id, closer); await this.archives.updateMember(ticket_id, closer);
if (channel) {
const description = reason const description = reason
? i18n('ticket.closed_by_member_with_reason.description', closer.user.toString(), reason) ? i18n('ticket.closed_by_member_with_reason.description', closer.user.toString(), reason)
: i18n('ticket.closed_by_member.description', closer.user.toString()); : i18n('ticket.closed_by_member.description', closer.user.toString());
@ -269,9 +275,11 @@ module.exports = class TicketManager extends EventEmitter {
setTimeout(async () => { setTimeout(async () => {
await channel.delete(`Ticket channel closed by ${closer.user.tag}${reason ? `: "${reason}"` : ''}`); await channel.delete(`Ticket channel closed by ${closer.user.tag}${reason ? `: "${reason}"` : ''}`);
}, 5000); }, 5000);
}
this.client.log.info(`${closer.user.tag} closed a ticket (${ticket_id})${reason ? `: "${reason}"` : ''}`); this.client.log.info(`${closer.user.tag} closed a ticket (${ticket_id})${reason ? `: "${reason}"` : ''}`);
} else { } else {
if (channel) {
const description = reason const description = reason
? i18n('ticket.closed_with_reason.description') ? i18n('ticket.closed_with_reason.description')
: i18n('ticket.closed.description'); : i18n('ticket.closed.description');
@ -288,6 +296,7 @@ module.exports = class TicketManager extends EventEmitter {
setTimeout(async () => { setTimeout(async () => {
await channel.delete(`Ticket channel closed${reason ? `: "${reason}"` : ''}`); await channel.delete(`Ticket channel closed${reason ? `: "${reason}"` : ''}`);
}, 5000); }, 5000);
}
this.client.log.info(`A ticket was closed (${ticket_id})${reason ? `: "${reason}"` : ''}`); this.client.log.info(`A ticket was closed (${ticket_id})${reason ? `: "${reason}"` : ''}`);
} }
@ -409,6 +418,8 @@ module.exports = class TicketManager extends EventEmitter {
this.client.log.debug(error); this.client.log.debug(error);
await close(); await close();
}); });
} else {
await close();
} }
this.emit('close', ticket_id); this.emit('close', ticket_id);
@ -423,9 +434,7 @@ module.exports = class TicketManager extends EventEmitter {
async resolve(ticket_id, guild_id) { async resolve(ticket_id, guild_id) {
let t_row; let t_row;
if (this.client.channels.resolve(ticket_id)) { if (!(t_row = await this.client.db.models.Ticket.findOne({ where: { id: ticket_id } }))) {
t_row = await this.client.db.models.Ticket.findOne({ where: { id: ticket_id } });
} else {
t_row = await this.client.db.models.Ticket.findOne({ t_row = await this.client.db.models.Ticket.findOne({
where: { where: {
guild: guild_id, guild: guild_id,