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", "platform",
"doc" "doc"
] ]
},
{
"login": "n1kkl",
"name": "Niklas",
"avatar_url": "https://avatars.githubusercontent.com/u/100782498?v=4",
"profile": "https://github.com/n1kkl",
"contributions": [
"code"
]
} }
], ],
"contributorsPerLine": 7, "contributorsPerLine": 7,

View File

@ -1,7 +1,8 @@
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> <!-- 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 --> <!-- ALL-CONTRIBUTORS-BADGE:END -->
## Contributors ✨ ## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): 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://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://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://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> </tr>
</tbody> </tbody>
</table> </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 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,