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

Annotation of /branches/4.x/src/utils/setTimeout.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2824 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2022 OSN Inc.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20     import path from "path";
21     import DiscordClient from "../client/Client";
22     import Timeout from "../models/Timeout";
23    
24     const timeouts = new Map();
25    
26     export const runTimeouts = async () => {
27     const data = (await Timeout.find());
28    
29     // await console.log(data);
30    
31     if (data && data.length > 0) {
32     console.log('Running timeouts...');
33     for await (const row of data) {
34     let timeout = await setTimeout(async () => {
35     await console.log('TIMEOUT');
36     await row.delete();
37     await timeouts.delete(row.get('id'));
38     (await import(row.get('filePath') as string)).default(DiscordClient.client, ...JSON.parse(row.get('params') as string));
39     }, new Date(row.get('time') as string).getTime() - Date.now());
40    
41     // await timeouts.set(row.get('id'), {
42     // row,
43     // timeout
44     // });
45     }
46     }
47     };
48    
49     export const setTimeoutv2 = async (file: string, time: number, guild_id: string, cmd: string, ...params: any[]) => {
50     await console.log('SETTING');
51     const row = await Timeout.create({
52     createdAt: new Date(),
53     filePath: path.resolve(__dirname, '../queues', file),
54     time: new Date(Date.now() + time).toISOString(),
55     params: JSON.stringify(params),
56     guild_id,
57     cmd
58     })
59    
60     const timeout = await setTimeout(async () => {
61     await console.log('TIMEOUT_SET');
62     await timeouts.delete(row.id);
63     await row.delete();
64     (await import(path.resolve(__dirname, '../queues', file))).default(DiscordClient.client, ...params);
65     }, time);
66    
67     const data = {
68     row,
69     timeout
70     };
71    
72     await timeouts.set(row.id, data);
73     return data;
74     };
75    
76     export const clearTimeoutv2 = async ({ row, timeout }: any) => {
77     await clearTimeout(timeout);
78     await timeouts.delete(row.id);
79     await row.delete();
80     };
81    
82     export const getTimeout = (id: string | number) => timeouts.get(id);
83    
84     export const hasTimeout = (id: string | number) => timeouts.has(id);
85    
86     export const getTimeouts = () => timeouts;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26