1 |
const path = require('path'); |
2 |
const timeouts = new Map(); |
3 |
|
4 |
const runTimeouts = async () => { |
5 |
const data = await app.db.allAsync("SELECT * FROM timeouts"); |
6 |
|
7 |
// await console.log(data); |
8 |
|
9 |
if (data && data.length > 0) { |
10 |
console.log('Running timeouts...'); |
11 |
for await (const row of data) { |
12 |
// console.log(row) |
13 |
|
14 |
let timeout = await setTimeout(async () => { |
15 |
await console.log('TIMEOUT'); |
16 |
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)); |
19 |
}, 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, guild_id, cmd, ...params) => { |
30 |
await console.log('SETTING'); |
31 |
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 row = await app.db.getAsync('SELECT * FROM timeouts ORDER BY id DESC LIMIT 0, 1'); |
33 |
|
34 |
const timeout = await setTimeout(async () => { |
35 |
await console.log('TIMEOUT_SET'); |
36 |
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); |
39 |
}, time); |
40 |
|
41 |
const data = { |
42 |
row, |
43 |
timeout |
44 |
}; |
45 |
|
46 |
await timeouts.set(row.id, data); |
47 |
return data; |
48 |
}; |
49 |
|
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 }; |