mirror of
https://github.com/nx-bat/discordbot-ng.git
synced 2026-11-11 07:13:37 -05:00
129 lines
4.9 KiB
TypeScript
129 lines
4.9 KiB
TypeScript
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelType, ChatInputCommandInteraction, Client, Guild, GuildMember, ModalBuilder, PrivateThreadChannel, TextChannel, TextInputStyle, ThreadAutoArchiveDuration, ThreadChannel, UserContextMenuCommandInteraction } from 'discord.js';
|
|
import { Database } from '../shared/Database';
|
|
import { CreateDefaultEmbed, createTextInput, createCheckbox, SetSeverity } from '.';
|
|
|
|
export async function closeOldTickets(client: Client) {
|
|
for (const ticket of await Database.getAllOpenPrivateHelpTickets()) {
|
|
try {
|
|
const thread = await client.channels.fetch(ticket.thread_id) as ThreadChannel;
|
|
const latestMessage = (await thread.messages.fetch({ limit: 1 })).at(0);
|
|
|
|
// Only process stale tickets. (Tickets with 5 days since the last message.)
|
|
if (latestMessage && latestMessage.createdTimestamp > Date.now() - 432e6) continue;
|
|
|
|
await closeTicket(client, thread, 'This ticket has been closed due to inactivity.');
|
|
} catch (e) {
|
|
console.error('Error closing ticket due to inactivity:', e);
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function closeTicket(context: Client, thread: ThreadChannel, reason: string): Promise<boolean> {
|
|
try {
|
|
await Database.closePrivateHelpTicket(thread.id);
|
|
|
|
await thread.send({
|
|
embeds: [{
|
|
...CreateDefaultEmbed(context),
|
|
...SetSeverity('info'),
|
|
|
|
title: 'Ticket Closed',
|
|
description: reason,
|
|
}],
|
|
});
|
|
|
|
await thread.edit({
|
|
name: `[CLOSED] ${thread.name}`.slice(0, 100),
|
|
archived: true,
|
|
locked: true,
|
|
});
|
|
|
|
return true;
|
|
} catch (error) {
|
|
console.error('An error occured while trying to close the ticket:', error);
|
|
|
|
await thread.send({
|
|
embeds: [{
|
|
...SetSeverity('error'),
|
|
description: 'I encountered an error while trying to close this ticket.',
|
|
}],
|
|
}).catch(() => {});
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function createPrivateHelpTicketThread(client: Client, guild: Guild, creator: GuildMember | null, reason: string, customTitle: string = '', additionalMembersToAdd: string[] = []): Promise<PrivateThreadChannel | null> {
|
|
const settings = await Database.getOrCreateSettings(guild.id);
|
|
|
|
if (!settings.private_help_channel_id || !settings.private_help_role_id) return null;
|
|
|
|
const channel = await client.channels.fetch(settings.private_help_channel_id) as TextChannel;
|
|
|
|
const thread = await channel.threads.create({
|
|
name: customTitle ? customTitle : (creator ? `${creator.displayName}'s Ticket` : 'Mod Ticket'),
|
|
autoArchiveDuration: ThreadAutoArchiveDuration.OneWeek,
|
|
invitable: false,
|
|
type: ChannelType.PrivateThread
|
|
}) as PrivateThreadChannel;
|
|
|
|
if (creator) await Database.createPrivateHelpTicket(creator.id, thread.id);
|
|
|
|
if (creator) {
|
|
const closeButton = new ButtonBuilder()
|
|
.setCustomId('close-ticket')
|
|
.setLabel('Click here if you no longer need help')
|
|
.setStyle(ButtonStyle.Danger);
|
|
|
|
const claimButton = new ButtonBuilder()
|
|
.setCustomId('claim-ticket')
|
|
.setLabel('Claim ticket')
|
|
.setStyle(ButtonStyle.Primary);
|
|
|
|
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(closeButton, claimButton);
|
|
|
|
await thread.send({
|
|
content: `${creator} feel free to direct your questions at any <@&${settings.private_help_role_id}>. Only you and staff members can see this channel.\n\n**Reason for contact:**\n${reason}\n\n-# Tickets will automatically close after 5 days of inactivity.`,
|
|
components: [row],
|
|
allowedMentions: {
|
|
users: [creator.id],
|
|
roles: [settings.private_help_role_id]
|
|
}
|
|
});
|
|
} else {
|
|
const closeButton = new ButtonBuilder()
|
|
.setCustomId('close-mod-ticket')
|
|
.setLabel('Close Mod Ticket')
|
|
.setStyle(ButtonStyle.Danger);
|
|
|
|
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(closeButton);
|
|
|
|
await thread.send({
|
|
content: reason,
|
|
components: [row],
|
|
allowedMentions: {
|
|
users: Array.from(new Set(additionalMembersToAdd))
|
|
}
|
|
});
|
|
}
|
|
|
|
for (const id of additionalMembersToAdd) {
|
|
await thread.members.add(id);
|
|
}
|
|
|
|
return thread;
|
|
}
|
|
|
|
export async function openModTicketModal(interaction: UserContextMenuCommandInteraction | ChatInputCommandInteraction, member: GuildMember) {
|
|
const modal = new ModalBuilder()
|
|
.setCustomId(`open-mod-ticket_${member.id}`)
|
|
.setTitle('Opening A Mod Ticket');
|
|
|
|
const titleLabel = createTextInput('title', 'Title', `The name of the thread. Defaults to "Mod Ticket For ${member.displayName}" if left empty`, false, TextInputStyle.Short, 100, null);
|
|
const initialMessageLabel = createTextInput('initial-message', 'Inital Message', 'The inital message sent in the thread', false, TextInputStyle.Paragraph, 1800, null);
|
|
const autoJoinLabel = createCheckbox('auto-join-thread', 'Auto Join Thread', 'Whether or not to automatically add you to the thread.', true);
|
|
|
|
modal.addLabelComponents(titleLabel, initialMessageLabel, autoJoinLabel);
|
|
interaction.showModal(modal);
|
|
}
|