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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 59 by rakin, Mon Jul 29 17:28:25 2024 UTC revision 339 by rakin, Mon Jul 29 17:29:37 2024 UTC
# Line 5  import Timeout from "../models/Timeout"; Line 5  import Timeout from "../models/Timeout";
5  const timeouts = new Map();  const timeouts = new Map();
6    
7  export const runTimeouts = async () => {  export const runTimeouts = async () => {
8      const data = (await Timeout.findAll());      const data = (await Timeout.find());
9    
10      // await console.log(data);      // await console.log(data);
11    
12      if (data && data.length > 0) {      if (data && data.length > 0) {
13          console.log('Running timeouts...');          console.log('Running timeouts...');
14          for await (const row of data) {          for await (const row of data) {
             // console.log(row)  
   
15              let timeout = await setTimeout(async () => {              let timeout = await setTimeout(async () => {
16                  await console.log('TIMEOUT');                  await console.log('TIMEOUT');
17                  await DiscordClient.client.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.get('id')]);                  await row.delete();
18                  await timeouts.delete(row.get('id'));                  await timeouts.delete(row.get('id'));
19                  (await import(row.get('filePath') as string)).default(DiscordClient.client, ...JSON.parse(row.get('params') as string));                  (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());              }, new Date(row.get('time') as string).getTime() - Date.now());
21    
22              await timeouts.set(row.get('id'), {              // await timeouts.set(row.get('id'), {
23                  row,              //     row,
24                  timeout              //     timeout
25              });              // });
26          }          }
27      }      }
28  };  };
29    
30  export const setTimeoutv2 = async (file: string, time: number, guild_id: string, cmd: string, ...params: any[]) => {  export const setTimeoutv2 = async (file: string, time: number, guild_id: string, cmd: string, ...params: any[]) => {
31      await console.log('SETTING');      await console.log('SETTING');
32      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]);      const row = await Timeout.create({
33      const row = await DiscordClient.client.db.getAsync('SELECT * FROM timeouts ORDER BY id DESC LIMIT 0, 1');          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    
41      const timeout = await setTimeout(async () => {      const timeout = await setTimeout(async () => {
42          await console.log('TIMEOUT_SET');          await console.log('TIMEOUT_SET');
43          await DiscordClient.client.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.id]);          await timeouts.delete(row.id);  
44          await timeouts.delete(row.id);                await row.delete();  
45          (await import(path.resolve(__dirname, '../queues', file))).default(DiscordClient.client, ...params);          (await import(path.resolve(__dirname, '../queues', file))).default(DiscordClient.client, ...params);
46      }, time);      }, time);
47            
# Line 52  export const setTimeoutv2 = async (file: Line 56  export const setTimeoutv2 = async (file:
56    
57  export const clearTimeoutv2 = async ({ row, timeout }: any) => {  export const clearTimeoutv2 = async ({ row, timeout }: any) => {
58      await clearTimeout(timeout);      await clearTimeout(timeout);
     await DiscordClient.client.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.id]);  
59      await timeouts.delete(row.id);      await timeouts.delete(row.id);
60        await row.delete();
61  };  };
62    
63  export const getTimeout = (id: string | number) => timeouts.get(id);  export const getTimeout = (id: string | number) => timeouts.get(id);

Legend:
Removed from v.59  
changed lines
  Added in v.339

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26