From aa3ff607f5d1863609388760efb3cb5a3ed3a01c Mon Sep 17 00:00:00 2001 From: CarelessInternet Date: Tue, 21 Dec 2021 17:57:39 +0100 Subject: [PATCH] if ticket author left the thread, archive the thread --- src/events/server/threadMembersUpdate.ts | 72 ++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/events/server/threadMembersUpdate.ts diff --git a/src/events/server/threadMembersUpdate.ts b/src/events/server/threadMembersUpdate.ts new file mode 100644 index 00000000..0054f1e9 --- /dev/null +++ b/src/events/server/threadMembersUpdate.ts @@ -0,0 +1,72 @@ +import { Collection, MessageEmbed, Snowflake, ThreadMember } from 'discord.js'; +import { channelMention, memberNicknameMention } from '@discordjs/builders'; +import { RowDataPacket } from 'mysql2'; +import { version } from '../../../package.json'; +import { conn } from '../../utils'; +import { Handler, Tables } from '../../types'; + +export const execute: Handler['execute'] = async ( + client, + members: Collection +) => { + const first = members.first()!; + const { thread } = first; + const { name, id } = first.thread; + + const ticketUserId = name.split('-').at(-1)!; + const member = members.get(ticketUserId); + + if (!member) return; + + if (name === `ticket-${ticketUserId}`) { + if ( + thread.members.cache.has(client.user!.id) && + !thread.members.cache.has(ticketUserId) + ) { + const [rows] = await conn.execute( + 'SELECT * FROM TicketingManagers WHERE GuildID = ?', + [thread.guildId] + ); + const record = ( + rows as RowDataPacket[] + )[0] as Tables.TicketingManagers | null; + + if (!record) return; + + const user = member.user!; + const embed = new MessageEmbed() + .setColor('YELLOW') + .setAuthor(user.tag, user.displayAvatarURL({ dynamic: true })) + .setTitle('Ticket Archived') + .setDescription( + `${memberNicknameMention(user.id)} left the support ticket!` + ) + .setTimestamp() + .setFooter(`Version ${version}`); + + await thread.send({ embeds: [embed] }); + await thread.setArchived(true); + + if (record.LogsChannel !== '0') { + embed.setDescription( + `${memberNicknameMention( + user.id + )} left their ticket! View it at ${channelMention(id)}` + ); + embed.addField('Name of Ticket', name); + + const logsChannel = await thread.guild.channels.fetch( + record.LogsChannel + ); + + if (!logsChannel?.isText()) return; + if ( + !logsChannel.permissionsFor(thread.guild.me!).has(['SEND_MESSAGES']) + ) + return; + + logsChannel.send({ embeds: [embed] }); + } + } + } +};