From 8c677dcf21fbc0d209876d5a8531c0341dc7b963 Mon Sep 17 00:00:00 2001 From: Tarrgon <61888458+Tarrgon@users.noreply.github.com> Date: Fri, 16 Jan 2026 09:17:53 -0500 Subject: [PATCH] Close tickets after 5 days of inactivity --- src/index.ts | 6 +++++- src/modals/open-ticket-modal.ts | 2 +- src/shared/Database.ts | 5 +++++ src/types/database-types.d.ts | 3 ++- src/utils/index.ts | 1 + src/utils/private-help-utils.ts | 24 ++++++++++++++++++++++++ 6 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/utils/private-help-utils.ts diff --git a/src/index.ts b/src/index.ts index cb936f9..9c19610 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import { handleAuditLogCreate, handleBanRemove, handleBulkMessageDelete, handleG import { Database } from './shared/Database'; import { openRedisClient } from './shared/RedisClient'; import { Handler } from './types'; -import { checkExpiredBans, initIfNecessary, loadHandlersFrom, refreshCommands } from './utils'; +import { checkExpiredBans, closeOldTickets, initIfNecessary, loadHandlersFrom, refreshCommands } from './utils'; import { initializeWebserver } from './webserver'; let ready = false; @@ -140,6 +140,10 @@ client.on('ready', async () => { checkExpiredBans(client); setInterval(checkExpiredBans.bind(null, client), 300000); + // Close old tickets every hour + closeOldTickets(client); + setInterval(closeOldTickets.bind(null, client), 3.6e+6); + ready = true; console.log('Ready'); diff --git a/src/modals/open-ticket-modal.ts b/src/modals/open-ticket-modal.ts index 6d374ce..eccdab9 100644 --- a/src/modals/open-ticket-modal.ts +++ b/src/modals/open-ticket-modal.ts @@ -38,7 +38,7 @@ export default { const row = new ActionRowBuilder().addComponents(closeButton, claimButton); await thread.send({ - content: `${interaction.user} feel free to direct your questions at any <@&${guildSettings.private_help_role_id}>. Only you and staff members can see this channel.\n\n**Reason for contact:**\n${reason}`, + content: `${interaction.user} feel free to direct your questions at any <@&${guildSettings.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: [interaction.user.id], diff --git a/src/shared/Database.ts b/src/shared/Database.ts index 0a0c578..d8eb176 100644 --- a/src/shared/Database.ts +++ b/src/shared/Database.ts @@ -143,6 +143,7 @@ export class Database { } static async getDiscordIds(e621Id: string | number): Promise { + // Perhaps this would be better and then we can return all the data: SELECT * FROM (SELECT * FROM discord_names WHERE user_id = ? ORDER BY id DESC) GROUP BY discord_id; const ids = await Database.db.all<{ discord_id: string }[]>('SELECT DISTINCT discord_id FROM discord_names WHERE user_id = ?', e621Id); return ids.map(r => r.discord_id); @@ -470,5 +471,9 @@ export class Database { return await Database.db.get('SELECT * from private_help_tickets WHERE user_id = ? ORDER BY timestamp DESC LIMIT 1', userId); } + static async getAllOpenPrivateHelpTickets(): Promise { + return await Database.db.all('SELECT * from private_help_tickets WHERE status = ?', PrivateHelpTicketStatus.OPEN); + } + // -- END PRIVATE HELP TICKETS } \ No newline at end of file diff --git a/src/types/database-types.d.ts b/src/types/database-types.d.ts index 48ff513..f92d1e4 100644 --- a/src/types/database-types.d.ts +++ b/src/types/database-types.d.ts @@ -72,6 +72,7 @@ export type KnowledgebaseItem = { export type PrivateHelpTicket = { id: number user_id: string - status: PrivateHelpTicketStatus, + thread_id: string + status: PrivateHelpTicketStatus timestamp: string } \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts index 2c97b9c..c5f919d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -17,6 +17,7 @@ export * from './ms-to-human'; export * from './name-sync'; export * from './note-utils'; export * from './oauth2'; +export * from './private-help-utils'; export * from './record-utils'; export * from './refresh-commands'; export * from './search-regex'; diff --git a/src/utils/private-help-utils.ts b/src/utils/private-help-utils.ts new file mode 100644 index 0000000..e6b66e0 --- /dev/null +++ b/src/utils/private-help-utils.ts @@ -0,0 +1,24 @@ +import { Client, ThreadChannel } from 'discord.js'; +import { Database } from '../shared/Database'; + +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); + if (latestMessage && latestMessage.createdTimestamp <= Date.now() - 432e6) { + await Database.closePrivateHelpTicket(thread.id); + + await thread.send('This ticket has been closed due to inactivity.'); + + thread.edit({ + archived: true, + locked: true + }); + } + } catch (e) { + console.error('Error closing ticket due to inactivity:'); + console.error(e); + } + } +} \ No newline at end of file