mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2024-12-23 00:03:09 +02:00
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:
parent
16ce243dde
commit
0ddd7c4166
23
src/listeners/channelDelete.js
Normal file
23
src/listeners/channelDelete.js
Normal 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');
|
||||
}
|
||||
};
|
@ -236,15 +236,20 @@ module.exports = class TicketManager extends EventEmitter {
|
||||
const guild = this.client.guilds.cache.get(t_row.guild);
|
||||
const settings = await this.client.utils.getSettings(guild.id);
|
||||
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 pinned = await channel.messages.fetchPinned();
|
||||
let pinned;
|
||||
if (channel) {
|
||||
pinned = await channel.messages.fetchPinned();
|
||||
}
|
||||
await t_row.update({
|
||||
closed_by: closer_id || null,
|
||||
closed_reason: reason ? this.client.cryptr.encrypt(reason) : null,
|
||||
open: false,
|
||||
pinned_messages: [...pinned.keys()]
|
||||
pinned_messages: pinned ? [...pinned.keys()] : []
|
||||
});
|
||||
|
||||
if (closer_id) {
|
||||
@ -252,42 +257,46 @@ module.exports = class TicketManager extends EventEmitter {
|
||||
|
||||
await this.archives.updateMember(ticket_id, closer);
|
||||
|
||||
const description = reason
|
||||
? i18n('ticket.closed_by_member_with_reason.description', closer.user.toString(), reason)
|
||||
: i18n('ticket.closed_by_member.description', closer.user.toString());
|
||||
await channel.send({
|
||||
embeds: [
|
||||
new MessageEmbed()
|
||||
.setColor(settings.success_colour)
|
||||
.setAuthor(closer.user.username, closer.user.displayAvatarURL())
|
||||
.setTitle(i18n('ticket.closed.title'))
|
||||
.setDescription(description)
|
||||
.setFooter(settings.footer, guild.iconURL())
|
||||
]
|
||||
});
|
||||
if (channel) {
|
||||
const description = reason
|
||||
? i18n('ticket.closed_by_member_with_reason.description', closer.user.toString(), reason)
|
||||
: i18n('ticket.closed_by_member.description', closer.user.toString());
|
||||
await channel.send({
|
||||
embeds: [
|
||||
new MessageEmbed()
|
||||
.setColor(settings.success_colour)
|
||||
.setAuthor(closer.user.username, closer.user.displayAvatarURL())
|
||||
.setTitle(i18n('ticket.closed.title'))
|
||||
.setDescription(description)
|
||||
.setFooter(settings.footer, guild.iconURL())
|
||||
]
|
||||
});
|
||||
|
||||
setTimeout(async () => {
|
||||
await channel.delete(`Ticket channel closed by ${closer.user.tag}${reason ? `: "${reason}"` : ''}`);
|
||||
}, 5000);
|
||||
setTimeout(async () => {
|
||||
await channel.delete(`Ticket channel closed by ${closer.user.tag}${reason ? `: "${reason}"` : ''}`);
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
this.client.log.info(`${closer.user.tag} closed a ticket (${ticket_id})${reason ? `: "${reason}"` : ''}`);
|
||||
} else {
|
||||
const description = reason
|
||||
? i18n('ticket.closed_with_reason.description')
|
||||
: i18n('ticket.closed.description');
|
||||
await channel.send({
|
||||
embeds: [
|
||||
new MessageEmbed()
|
||||
.setColor(settings.success_colour)
|
||||
.setTitle(i18n('ticket.closed.title'))
|
||||
.setDescription(description)
|
||||
.setFooter(settings.footer, guild.iconURL())
|
||||
]
|
||||
});
|
||||
if (channel) {
|
||||
const description = reason
|
||||
? i18n('ticket.closed_with_reason.description')
|
||||
: i18n('ticket.closed.description');
|
||||
await channel.send({
|
||||
embeds: [
|
||||
new MessageEmbed()
|
||||
.setColor(settings.success_colour)
|
||||
.setTitle(i18n('ticket.closed.title'))
|
||||
.setDescription(description)
|
||||
.setFooter(settings.footer, guild.iconURL())
|
||||
]
|
||||
});
|
||||
|
||||
setTimeout(async () => {
|
||||
await channel.delete(`Ticket channel closed${reason ? `: "${reason}"` : ''}`);
|
||||
}, 5000);
|
||||
setTimeout(async () => {
|
||||
await channel.delete(`Ticket channel closed${reason ? `: "${reason}"` : ''}`);
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
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);
|
||||
await close();
|
||||
});
|
||||
} else {
|
||||
await close();
|
||||
}
|
||||
|
||||
this.emit('close', ticket_id);
|
||||
@ -423,9 +434,7 @@ module.exports = class TicketManager extends EventEmitter {
|
||||
async resolve(ticket_id, guild_id) {
|
||||
let t_row;
|
||||
|
||||
if (this.client.channels.resolve(ticket_id)) {
|
||||
t_row = await this.client.db.models.Ticket.findOne({ where: { id: ticket_id } });
|
||||
} else {
|
||||
if (!(t_row = await this.client.db.models.Ticket.findOne({ where: { id: ticket_id } }))) {
|
||||
t_row = await this.client.db.models.Ticket.findOne({
|
||||
where: {
|
||||
guild: guild_id,
|
||||
|
Loading…
Reference in New Issue
Block a user