1 |
|
/** |
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"; |
import path from "path"; |
21 |
import DiscordClient from "../client/Client"; |
import DiscordClient from "../client/Client"; |
22 |
import Timeout from "../models/Timeout"; |
import Timeout from "../models/Timeout"; |
24 |
const timeouts = new Map(); |
const timeouts = new Map(); |
25 |
|
|
26 |
export const runTimeouts = async () => { |
export const runTimeouts = async () => { |
27 |
const data = (await Timeout.findAll()); |
const data = (await Timeout.find()); |
28 |
|
|
29 |
// await console.log(data); |
// await console.log(data); |
30 |
|
|
31 |
if (data && data.length > 0) { |
if (data && data.length > 0) { |
32 |
console.log('Running timeouts...'); |
console.log('Running timeouts...'); |
33 |
for await (const row of data) { |
for await (const row of data) { |
|
// console.log(row) |
|
|
|
|
34 |
let timeout = await setTimeout(async () => { |
let timeout = await setTimeout(async () => { |
35 |
await console.log('TIMEOUT'); |
await console.log('TIMEOUT'); |
36 |
await DiscordClient.client.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.get('id')]); |
await row.delete(); |
37 |
await timeouts.delete(row.get('id')); |
await timeouts.delete(row.get('id')); |
38 |
(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)); |
39 |
}, new Date(row.get('time') as string).getTime() - Date.now()); |
}, new Date(row.get('time') as string).getTime() - Date.now()); |
40 |
|
|
41 |
await timeouts.set(row.get('id'), { |
// await timeouts.set(row.get('id'), { |
42 |
row, |
// row, |
43 |
timeout |
// timeout |
44 |
}); |
// }); |
45 |
} |
} |
46 |
} |
} |
47 |
}; |
}; |
48 |
|
|
49 |
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[]) => { |
50 |
await console.log('SETTING'); |
await console.log('SETTING'); |
51 |
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({ |
52 |
const row = await DiscordClient.client.db.getAsync('SELECT * FROM timeouts ORDER BY id DESC LIMIT 0, 1'); |
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 () => { |
const timeout = await setTimeout(async () => { |
61 |
await console.log('TIMEOUT_SET'); |
await console.log('TIMEOUT_SET'); |
62 |
await DiscordClient.client.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.id]); |
await timeouts.delete(row.id); |
63 |
await timeouts.delete(row.id); |
await row.delete(); |
64 |
(await import(path.resolve(__dirname, '../queues', file))).default(DiscordClient.client, ...params); |
(await import(path.resolve(__dirname, '../queues', file))).default(DiscordClient.client, ...params); |
65 |
}, time); |
}, time); |
66 |
|
|
75 |
|
|
76 |
export const clearTimeoutv2 = async ({ row, timeout }: any) => { |
export const clearTimeoutv2 = async ({ row, timeout }: any) => { |
77 |
await clearTimeout(timeout); |
await clearTimeout(timeout); |
|
await DiscordClient.client.db.runAsync("DELETE FROM timeouts WHERE id = ?", [row.id]); |
|
78 |
await timeouts.delete(row.id); |
await timeouts.delete(row.id); |
79 |
|
await row.delete(); |
80 |
}; |
}; |
81 |
|
|
82 |
export const getTimeout = (id: string | number) => timeouts.get(id); |
export const getTimeout = (id: string | number) => timeouts.get(id); |