/[sudobot]/trunk/src/setTimeout.js
ViewVC logotype

Diff of /trunk/src/setTimeout.js

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

revision 45 by rakin, Mon Jul 29 17:28:20 2024 UTC revision 49 by rakin, Mon Jul 29 17:28:21 2024 UTC
# Line 1  Line 1 
1  const path = require('path');  const path = require('path');
2    const timeouts = new Map();
3    
4  const runTimeouts = async () => {  const runTimeouts = async () => {
5      const data = await app.db.allAsync("SELECT * FROM timeouts");      const data = await app.db.allAsync("SELECT * FROM timeouts");
# Line 6  const runTimeouts = async () => { Line 7  const runTimeouts = async () => {
7      // await console.log(data);      // await console.log(data);
8    
9      if (data && data.length > 0) {      if (data && data.length > 0) {
10            console.log('Running timeouts...');
11          for await (const row of data) {          for await (const row of data) {
12              // console.log(row);              // console.log(row)
13    
14              await setTimeout(async () => {              let timeout = await setTimeout(async () => {
15                  await console.log('TIMEOUT');                  await console.log('TIMEOUT');
16                  await app.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.id]);                  await app.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.id]);
17                    await timeouts.delete(row.id);
18                  await require(row.filePath)(...JSON.parse(row.params));                  await require(row.filePath)(...JSON.parse(row.params));
19              }, new Date(row.time).getTime() - Date.now());              }, new Date(row.time).getTime() - Date.now());
20    
21                await timeouts.set(row.id, {
22                    row,
23                    timeout
24                });
25          }          }
26      }      }
27  };  };
28    
29  const setTimeoutv2 = async (file, time, ...params) => {  const setTimeoutv2 = async (file, time, guild_id, cmd, ...params) => {
30      await console.log('SETTING');      await console.log('SETTING');
31      await app.db.allAsync("INSERT INTO timeouts(created_at, filePath, time, params) VALUES(?, ?, ?, ?)", [new Date().toISOString(), path.resolve(__dirname, '../queues', file), new Date(Date.now() + time).toISOString(), JSON.stringify(params)]);      await app.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]);
32      const id = (await app.db.getAsync('SELECT * FROM timeouts ORDER BY id DESC LIMIT 0, 1'))?.id;      const row = await app.db.getAsync('SELECT * FROM timeouts ORDER BY id DESC LIMIT 0, 1');
33    
34      await setTimeout(async () => {      const timeout = await setTimeout(async () => {
35          await console.log('TIMEOUT_SET');          await console.log('TIMEOUT_SET');
36          await app.db.runAsync("DELETE FROM timeouts WHERE id = ?", [id]);          await app.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.id]);
37            await timeouts.delete(row.id);
38          await require(path.resolve(__dirname, '../queues', file))(...params);          await require(path.resolve(__dirname, '../queues', file))(...params);
39      }, time);      }, time);
40        
41        const data = {
42            row,
43            timeout
44        };
45    
46        await timeouts.set(row.id, data);
47        return data;
48  };  };
49    
 module.exports = { runTimeouts, setTimeoutv2 };  
50    const clearTimeoutv2 = async ({ row, timeout }) => {
51        await clearTimeout(timeout);
52        await app.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.id]);
53        await timeouts.delete(row.id);
54    };
55    
56    const getTimeout = id => timeouts.get(id);
57    
58    const hasTimeout = id => timeouts.has(id);
59    
60    const getTimeouts = () => timeouts;
61    
62    module.exports = { runTimeouts, setTimeoutv2, clearTimeoutv2, getTimeout, hasTimeout, getTimeouts };

Legend:
Removed from v.45  
changed lines
  Added in v.49

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26