Skip to content

Commit

Permalink
if ticket author left the thread, archive the thread
Browse files Browse the repository at this point in the history
  • Loading branch information
CarelessInternet committed Dec 21, 2021
1 parent cb96b16 commit aa3ff60
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/events/server/threadMembersUpdate.ts
Original file line number Diff line number Diff line change
@@ -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<Snowflake, ThreadMember>
) => {
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] });
}
}
}
};

0 comments on commit aa3ff60

Please sign in to comment.