-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonHandler.js
49 lines (42 loc) · 1.14 KB
/
jsonHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
import fs from 'fs'
import cron from 'node-cron'
import { performCheck } from './dailycheck.js'
const schema = require('./schema.json')
let data = {}
export function getData() {
if (Object.keys(data) == 0) {
if (fs.existsSync('./data.json')) data = require('./data.json')
else saveData(schema)
}
return data
}
function saveData(newData = null) {
if (newData) data = newData
fs.writeFile('./data.json', JSON.stringify(data), (err) => {
if (err) {
throw err
}
console.log('JSON updated')
})
}
export function updateSubscribers(id) {
data.subscribers.push(id)
saveData()
}
export function getSubscribers() {
return getData().subscribers
}
export function getWhitelist() {
return getData().whitelist
}
export function initializeData(telegram) {
getSubscribers().map((subscriber) => {
cron.schedule('0 10 * * *', async () => {
await performCheck(subscriber, telegram)
telegram.sendMessage(subscriber, 'performed daily cron')
})
telegram.sendMessage(subscriber, 'resuming subscription after downtime')
})
}