Close tickets after 5 days of inactivity
This commit is contained in:
+5
-1
@@ -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');
|
||||
|
||||
@@ -38,7 +38,7 @@ export default {
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().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],
|
||||
|
||||
@@ -143,6 +143,7 @@ export class Database {
|
||||
}
|
||||
|
||||
static async getDiscordIds(e621Id: string | number): Promise<string[]> {
|
||||
// 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<PrivateHelpTicket>('SELECT * from private_help_tickets WHERE user_id = ? ORDER BY timestamp DESC LIMIT 1', userId);
|
||||
}
|
||||
|
||||
static async getAllOpenPrivateHelpTickets(): Promise<PrivateHelpTicket[]> {
|
||||
return await Database.db.all<PrivateHelpTicket[]>('SELECT * from private_help_tickets WHERE status = ?', PrivateHelpTicketStatus.OPEN);
|
||||
}
|
||||
|
||||
// -- END PRIVATE HELP TICKETS
|
||||
}
|
||||
Vendored
+2
-1
@@ -72,6 +72,7 @@ export type KnowledgebaseItem = {
|
||||
export type PrivateHelpTicket = {
|
||||
id: number
|
||||
user_id: string
|
||||
status: PrivateHelpTicketStatus,
|
||||
thread_id: string
|
||||
status: PrivateHelpTicketStatus
|
||||
timestamp: string
|
||||
}
|
||||
@@ -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';
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user