/[sudobot]/trunk/src/utils/setTimeout.ts
ViewVC logotype

Annotation of /trunk/src/utils/setTimeout.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26