mirror of
https://github.com/nx-bat/discordbot-ng.git
synced 2026-11-11 07:13:37 -05:00
(tickets): Move ticket closure functionality to a seperate utilities function & replace close-ticket logic with .
This commit is contained in:
+10
-12
@@ -1,5 +1,5 @@
|
|||||||
import { ButtonInteraction, Client, MessageFlags, ChannelType } from 'discord.js';
|
import { ButtonInteraction, Client, MessageFlags, ChannelType } from 'discord.js';
|
||||||
import { Database } from '../shared/Database';
|
import { closeTicket } from '../utils';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'close-ticket',
|
name: 'close-ticket',
|
||||||
@@ -7,23 +7,21 @@ export default {
|
|||||||
const channel = await interaction.channel?.fetch();
|
const channel = await interaction.channel?.fetch();
|
||||||
|
|
||||||
if (!channel || !channel.isThread() || !channel.isSendable() || channel.type != ChannelType.PrivateThread)
|
if (!channel || !channel.isThread() || !channel.isSendable() || channel.type != ChannelType.PrivateThread)
|
||||||
return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Oops. Something went wrong. Please report this to a staff member.' });
|
return await interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Oops. Something went wrong. Please report this to a staff member.' });
|
||||||
|
|
||||||
|
await interaction.reply({
|
||||||
|
content: 'Closing, please wait...',
|
||||||
|
flags: MessageFlags.Ephemeral
|
||||||
|
});
|
||||||
|
|
||||||
await interaction.message.edit({
|
await interaction.message.edit({
|
||||||
content: interaction.message.content,
|
content: interaction.message.content,
|
||||||
components: []
|
components: []
|
||||||
});
|
});
|
||||||
|
|
||||||
await Database.closePrivateHelpTicket(channel.id);
|
if (await closeTicket(client, channel, `This ticket has been closed by ${interaction.user}`))
|
||||||
|
return await interaction.editReply({ content: 'An error occured while trying to close this ticket.' });
|
||||||
|
|
||||||
await channel.send(`This ticket has been closed by ${interaction.user}`);
|
await interaction.editReply({ content: 'Ticket closed.' });
|
||||||
|
|
||||||
await interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Ticket closed.' });
|
|
||||||
|
|
||||||
channel.edit({
|
|
||||||
name: `[CLOSED] ${channel.name}`.slice(0, 100),
|
|
||||||
archived: true,
|
|
||||||
locked: true
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1,31 +1,58 @@
|
|||||||
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelType, ChatInputCommandInteraction, Client, Guild, GuildMember, ModalBuilder, PrivateThreadChannel, TextChannel, TextInputStyle, ThreadAutoArchiveDuration, ThreadChannel, UserContextMenuCommandInteraction } from 'discord.js';
|
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 { Database } from '../shared/Database';
|
||||||
import { createTextInput, createYesNoMenu } from '.';
|
import { CreateDefaultEmbed, createTextInput, createYesNoMenu, SetSeverity } from '.';
|
||||||
|
|
||||||
export async function closeOldTickets(client: Client) {
|
export async function closeOldTickets(client: Client) {
|
||||||
for (const ticket of await Database.getAllOpenPrivateHelpTickets()) {
|
for (const ticket of await Database.getAllOpenPrivateHelpTickets()) {
|
||||||
const thread = await client.channels.fetch(ticket.thread_id) as ThreadChannel;
|
|
||||||
const latestMessage = (await thread.messages.fetch({ limit: 1 })).at(0);
|
|
||||||
|
|
||||||
// We only process tickets older than 5 days.
|
|
||||||
if (latestMessage && latestMessage.createdTimestamp > Date.now() - 432e6) continue;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Database.closePrivateHelpTicket(thread.id);
|
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 thread.send('This ticket has been closed due to inactivity.');
|
await closeTicket(client, thread, 'This ticket has been closed due to inactivity.');
|
||||||
|
|
||||||
await thread.edit({
|
|
||||||
name: `[CLOSED] ${thread.name}`,
|
|
||||||
archived: true,
|
|
||||||
locked: true,
|
|
||||||
});
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error closing ticket due to inactivity:', 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.log('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.',
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function createPrivateHelpTicketThread(client: Client, guild: Guild, creator: GuildMember | null, reason: string, customTitle: string = '', additionalMembersToAdd: string[] = []): Promise<PrivateThreadChannel | null> {
|
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);
|
const settings = await Database.getOrCreateSettings(guild.id);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user