+6
-10
@@ -1,10 +1,11 @@
|
||||
import { Client as DiscordClient, GatewayIntentBits, MessageFlags, Partials } from 'discord.js';
|
||||
import { config } from './config';
|
||||
import { handleAuditLogCreate, handleBanRemove, handleBulkMessageDelete, handleGuildCreate, handleMemberJoin, handleMessageCreate, handleMessageDelete, handleMessageUpdate, handleThreadCreate, handleVoiceStateUpdate } from './events';
|
||||
import { ScheduledTasks, Scheduler } from './scheduler';
|
||||
import { Database } from './shared/Database';
|
||||
import { openRedisClient } from './shared/RedisClient';
|
||||
import { Handler } from './types';
|
||||
import { checkExpiredBans, closeOldTickets, initIfNecessary, loadHandlersFrom, refreshCommands } from './utils';
|
||||
import { initIfNecessary, loadHandlersFrom, refreshCommands } from './utils';
|
||||
import { initializeWebserver } from './webserver';
|
||||
|
||||
let ready = false;
|
||||
@@ -28,6 +29,8 @@ const client = new DiscordClient({
|
||||
}
|
||||
});
|
||||
|
||||
const scheduler: Scheduler = new Scheduler(client);
|
||||
|
||||
const commands: Handler[] = [];
|
||||
const contextMenus: Handler[] = [];
|
||||
const buttons: Handler[] = [];
|
||||
@@ -135,16 +138,9 @@ client.on('clientReady', async () => {
|
||||
|
||||
await initializeWebserver(client);
|
||||
|
||||
// Check for expired bans every 5 minutes
|
||||
checkExpiredBans(client);
|
||||
setInterval(checkExpiredBans.bind(null, client), 300000);
|
||||
|
||||
// Close old tickets every hour
|
||||
closeOldTickets(client);
|
||||
setInterval(closeOldTickets.bind(null, client), 3.6e+6);
|
||||
ScheduledTasks.forEach((task) => scheduler.add(task));
|
||||
|
||||
ready = true;
|
||||
|
||||
console.log('Ready');
|
||||
});
|
||||
|
||||
@@ -162,4 +158,4 @@ client.on('voiceStateUpdate', handleVoiceStateUpdate);
|
||||
client.on('error', console.error);
|
||||
process.on('uncaughtException', console.error);
|
||||
|
||||
client.login(config.DISCORD_TOKEN);
|
||||
client.login(config.DISCORD_TOKEN);
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Client } from "discord.js";
|
||||
import { Task } from "../types";
|
||||
import ScheduledTasks from "./tasks";
|
||||
|
||||
class Scheduler {
|
||||
private tasks = new Map<Task, NodeJS.Timeout>();
|
||||
private context: Client;
|
||||
|
||||
constructor(context: Client) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
add(task: Task) {
|
||||
const interval = setInterval(() => task.handle(this.context), task.interval);
|
||||
if (task.firstRun) interval._onTimeout();
|
||||
|
||||
this.tasks.set(task, interval);
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
ScheduledTasks, Scheduler
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Client } from "discord.js";
|
||||
import { Task } from "../../types";
|
||||
import { checkExpiredBans } from "../../utils";
|
||||
|
||||
class CheckExpiredBansTask implements Task {
|
||||
interval: number = 300000;
|
||||
firstRun: boolean = true;
|
||||
|
||||
async handle(context: Client): Promise<void> {
|
||||
await checkExpiredBans(context);
|
||||
}
|
||||
}
|
||||
|
||||
export default new CheckExpiredBansTask();
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Client } from "discord.js";
|
||||
import { Task } from "../../types";
|
||||
import { closeOldTickets } from "../../utils";
|
||||
|
||||
class CloseStaleTicketsTask implements Task {
|
||||
interval: number = 3.6e6;
|
||||
firstRun: boolean = true;
|
||||
|
||||
async handle(context: Client): Promise<void> {
|
||||
await closeOldTickets(context);
|
||||
}
|
||||
}
|
||||
|
||||
export default new CloseStaleTicketsTask();
|
||||
@@ -0,0 +1,7 @@
|
||||
import checkExpiredBansTask from './check-expired-bans-task';
|
||||
import closeStaleTicketsTask from './close-stale-tickets-task';
|
||||
|
||||
export default [
|
||||
closeStaleTicketsTask,
|
||||
checkExpiredBansTask,
|
||||
];
|
||||
Vendored
+1
@@ -3,3 +3,4 @@ export * from './database-types.d';
|
||||
export * from './e621-types.d';
|
||||
export * from './handler.d';
|
||||
export * from './helper-types.d';
|
||||
export * from './scheduler.d';
|
||||
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
import { Client } from "discord.js";
|
||||
|
||||
export interface Task {
|
||||
interval: number;
|
||||
firstRun: boolean;
|
||||
|
||||
handle(context: Client): Promise<void>;
|
||||
};
|
||||
Reference in New Issue
Block a user