· 4 years ago · Apr 11, 2021, 10:00 PM
1// Config
2const loadYAML = require('@modules/yaml.js')
3const config = loadYAML('config')
4
5// SQLite
6const Database = require('better-sqlite3')
7let db = new Database('data.db');
8
9let taskIDs = new Array()
10
11// Get client
12const { client } = require('@root/index.js')
13
14// Chalk
15const chalk = require('chalk');
16const infoPrefixColor = chalk.black.bgWhite
17const warnPrefixColor = chalk.black.bgYellow
18const errorPrefixColor = chalk.white.bgRed
19const urlColor = chalk.blue.underline
20const highlightColor = chalk.yellow
21
22module.exports = async (type, task, userID, runAt) => {
23 db.exec('CREATE TABLE IF NOT EXISTS tasks (id SMALLINT(255), task VARCHAR(255), user_id BIGINT(255), run_at TIMESTAMP)');
24 const tasksTableSelection = db.prepare('SELECT * FROM tasks');
25 const tasksTableGetSelection = tasksTableSelection.all();
26
27 const insert = db.prepare('INSERT INTO tasks (id, task, user_id, run_at) VALUES (?, ?, ?, ?)');
28
29 switch (type) {
30 case 'list':
31 if (tasksTableGetSelection.length > 0) {
32 console.log(infoPrefixColor(config.ConsoleStyle.Prefix.Info), `====== Aesthetic BOT Task Queue List ======`)
33 for (listTask of tasksTableGetSelection) {
34 let date = new Date(0)
35 date.setUTCSeconds(listTask.run_at)
36 switch (listTask.task) {
37 case 'unban':
38 console.log(`[${listTask.id}]> Unban ${listTask.user_id} by ${date} (UNIX ${listTask.run_at})`)
39 break
40 case 'unmute':
41 console.log(`[${listTask.id}]> Unmute ${listTask.user_id} by ${date} (UNIX ${listTask.run_at})`)
42 break
43 default:
44 console.log(warnPrefixColor(config.ConsoleStyle.Prefix.Warn), `Task ${listTask.task} is not a recognised task`)
45 break
46 }
47 }
48 console.log(infoPrefixColor(config.ConsoleStyle.Prefix.Info), `====== Aesthetic BOT Task Queue List ======`)
49 }
50 return
51 case 'create':
52 if (!task || !userID || !runAt) return console.log(warnPrefixColor(config.ConsoleStyle.Prefix.Warn), `Missing arguments to create a task.`)
53
54 for (taskID of tasksTableGetSelection) {
55 if (!taskIDs.includes(taskID.id)) taskIDs.push(taskID.id)
56 }
57 let nextTaskID = Math.floor(Math.random() * 32767)
58 while (taskIDs.includes(nextTaskID)) {
59 nextTaskID = Math.floor(Math.random() * 32767)
60 }
61
62 insert.run(nextTaskID, `${task}`, userID, runAt)
63 taskIDs.push(nextTaskID)
64 return
65 case 'run':
66 let runDate = Math.floor(Date.now() / 1000)
67 for (runTask of tasksTableGetSelection) {
68 if (runDate >= runTask.run_at) {
69 switch (runTask.task) {
70 case 'unban':
71 db.exec(`DELETE FROM tasks WHERE ${runTask.id}`);
72 removeID = taskIDs.indexOf(runTask.id)
73 taskIDs.splice(removeID, 1, '');
74 console.log(`[${listTask.id}]> Unban of ${listTask.user_id} executed`)
75 break
76 case 'unmute':
77 db.exec(`DELETE FROM tasks WHERE ${runTask.id}`);
78 removeID = taskIDs.indexOf(runTask.id)
79 taskIDs.splice(removeID, 1, '');
80 console.log(`[${listTask.id}]> Unmute of ${listTask.user_id} executed`)
81
82 const guild = await client.guilds.fetch(config.serverID)
83 console.log(guild)
84
85 return
86 default:
87 break
88 }
89 }
90 }
91 return
92 case 'clear':
93 db.exec('DROP TABLE IF EXISTS tasks');
94 db.exec('CREATE TABLE IF NOT EXISTS tasks (id SMALLINT(255), task VARCHAR(255), user_id BIGINT(255), run_at TIMESTAMP)');
95 return
96 default:
97 return console.log(warnPrefixColor(config.ConsoleStyle.Prefix.Warn), `${type} is not a valid task type.`)
98 }
99}