Merge pull request #319 from n1kkl/main

fix: close ticket when the channel is deleted
This commit is contained in:
Isaac 2023-01-06 17:07:27 +00:00 committed by GitHub
commit 6a1a75d572
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 38 deletions

View File

@ -188,6 +188,15 @@
"platform",
"doc"
]
},
{
"login": "n1kkl",
"name": "Niklas",
"avatar_url": "https://avatars.githubusercontent.com/u/100782498?v=4",
"profile": "https://github.com/n1kkl",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,

View File

@ -1,7 +1,8 @@
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-19-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
@ -35,6 +36,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center"><a href="https://foxco-network.de"><img src="https://avatars.githubusercontent.com/u/54017453?v=4?s=100" width="100px;" alt="FoxXxHater"/><br /><sub><b>FoxXxHater</b></sub></a><br /><a href="#platform-FoxXxHater" title="Packaging/porting to new platform">📦</a></td>
<td align="center"><a href="https://adminrat.codes"><img src="https://avatars.githubusercontent.com/u/24538037?v=4?s=100" width="100px;" alt="AdminRAT"/><br /><sub><b>AdminRAT</b></sub></a><br /><a href="#platform-AdminRAT" title="Packaging/porting to new platform">📦</a></td>
<td align="center"><a href="https://c43721.dev"><img src="https://avatars.githubusercontent.com/u/55610086?v=4?s=100" width="100px;" alt="c43721"/><br /><sub><b>c43721</b></sub></a><br /><a href="#platform-c43721" title="Packaging/porting to new platform">📦</a> <a href="https://github.com/discord-tickets/bot/commits?author=c43721" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/n1kkl"><img src="https://avatars.githubusercontent.com/u/100782498?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Niklas</b></sub></a><br /><a href="https://github.com/discord-tickets/bot/commits?author=n1kkl" title="Code">💻</a></td>
</tr>
</tbody>
</table>

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 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,