/[sudobot]/branches/2.x/src/utils/setTimeout.ts
ViewVC logotype

Contents of /branches/2.x/src/utils/setTimeout.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2470 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 import path from "path";
2 import DiscordClient from "../client/Client";
3 import Timeout from "../models/Timeout";
4
5 const timeouts = new Map();
6
7 export const runTimeouts = async () => {
8 const data = (await Timeout.findAll());
9
10 // await console.log(data);
11
12 if (data && data.length > 0) {
13 console.log('Running timeouts...');
14 for await (const row of data) {
15 // console.log(row)
16
17 let timeout = await setTimeout(async () => {
18 await console.log('TIMEOUT');
19 await DiscordClient.client.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.get('id')]);
20 await timeouts.delete(row.get('id'));
21 (await import(row.get('filePath') as string)).default(DiscordClient.client, ...JSON.parse(row.get('params') as string));
22 }, new Date(row.get('time') as string).getTime() - Date.now());
23
24 await timeouts.set(row.get('id'), {
25 row,
26 timeout
27 });
28 }
29 }
30 };
31
32 export const setTimeoutv2 = async (file: string, time: number, guild_id: string, cmd: string, ...params: any[]) => {
33 await console.log('SETTING');
34 await DiscordClient.client.db.allAsync("INSERT INTO timeouts(created_at, filePath, time, params, guild_id, cmd) VALUES(?, ?, ?, ?, ?, ?)", [new Date().toISOString(), path.resolve(__dirname, '../queues', file), new Date(Date.now() + time).toISOString(), JSON.stringify(params), guild_id, cmd]);
35 const row = await DiscordClient.client.db.getAsync('SELECT * FROM timeouts ORDER BY id DESC LIMIT 0, 1');
36
37 const timeout = await setTimeout(async () => {
38 await console.log('TIMEOUT_SET');
39 await DiscordClient.client.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.id]);
40 await timeouts.delete(row.id);
41 (await import(path.resolve(__dirname, '../queues', file))).default(DiscordClient.client, ...params);
42 }, time);
43
44 const data = {
45 row,
46 timeout
47 };
48
49 await timeouts.set(row.id, data);
50 return data;
51 };
52
53 export const clearTimeoutv2 = async ({ row, timeout }: any) => {
54 await clearTimeout(timeout);
55 await DiscordClient.client.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.id]);
56 await timeouts.delete(row.id);
57 };
58
59 export const getTimeout = (id: string | number) => timeouts.get(id);
60
61 export const hasTimeout = (id: string | number) => timeouts.has(id);
62
63 export const getTimeouts = () => timeouts;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26