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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 339 - (hide annotations)
Mon Jul 29 17:29:37 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 2100 byte(s)
refactor(queuejobs): use mongodb
1 rakin 51 import path from "path";
2     import DiscordClient from "../client/Client";
3 rakin 59 import Timeout from "../models/Timeout";
4 rakin 51
5     const timeouts = new Map();
6    
7     export const runTimeouts = async () => {
8 rakin 339 const data = (await Timeout.find());
9 rakin 51
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     let timeout = await setTimeout(async () => {
16     await console.log('TIMEOUT');
17 rakin 339 await row.delete();
18 rakin 59 await timeouts.delete(row.get('id'));
19     (await import(row.get('filePath') as string)).default(DiscordClient.client, ...JSON.parse(row.get('params') as string));
20     }, new Date(row.get('time') as string).getTime() - Date.now());
21 rakin 51
22 rakin 339 // await timeouts.set(row.get('id'), {
23     // row,
24     // timeout
25     // });
26 rakin 51 }
27     }
28     };
29    
30     export const setTimeoutv2 = async (file: string, time: number, guild_id: string, cmd: string, ...params: any[]) => {
31     await console.log('SETTING');
32 rakin 339 const row = await Timeout.create({
33     createdAt: new Date(),
34     filePath: path.resolve(__dirname, '../queues', file),
35     time: new Date(Date.now() + time).toISOString(),
36     params: JSON.stringify(params),
37     guild_id,
38     cmd
39     })
40 rakin 51
41     const timeout = await setTimeout(async () => {
42     await console.log('TIMEOUT_SET');
43 rakin 339 await timeouts.delete(row.id);
44     await row.delete();
45 rakin 51 (await import(path.resolve(__dirname, '../queues', file))).default(DiscordClient.client, ...params);
46     }, time);
47    
48     const data = {
49     row,
50     timeout
51     };
52    
53     await timeouts.set(row.id, data);
54     return data;
55     };
56    
57     export const clearTimeoutv2 = async ({ row, timeout }: any) => {
58     await clearTimeout(timeout);
59     await timeouts.delete(row.id);
60 rakin 339 await row.delete();
61 rakin 51 };
62    
63     export const getTimeout = (id: string | number) => timeouts.get(id);
64    
65     export const hasTimeout = (id: string | number) => timeouts.has(id);
66    
67     export const getTimeouts = () => timeouts;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26