-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
65 lines (54 loc) · 1.95 KB
/
helper.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const db = require('./db')
const parser = require('./parser')
const src = require('./src')
exports.setup = async () => {
await db.createTables()
globalThis.goals, globalThis.assists; [goals, assists] = await db.getStats()
console.log(`Setup completed`)
}
exports.checkStats = async (bot) => {
let currGoals, currAssists
[currGoals, currAssists] = await parser.getStats()
const isGoal = await checkGoals(bot, currGoals)
const isAssist = await checkAssists(bot, currAssists)
if (isGoal || isAssist) await db.updateStats(currGoals, currAssists)
}
async function checkGoals(bot, currGoals){
if (goals < currGoals){
const users = await db.getUsers('goalNotif')
let alarmMessage = {}
const rand = Math.random()
src.languages.forEach(lang => {
alarmMessage[lang] =
src.goalStrList[lang][Math.floor(rand * src.goalStrList[lang].length)] + ' ' +
src.emojiList[Math.floor(rand * src.emojiList.length)]
})
users.forEach(user => { bot.sendMessage(user.id, alarmMessage[user.lang]) })
goals = currGoals
shuffle(src.goalStrList), shuffle(src.emojiList)
return true
}
return false
}
async function checkAssists(bot, currAssists){
if (assists < currAssists){
const users = await db.getUsers('assistNotif')
let alarmMessage = {}
const rand = Math.random()
src.languages.forEach(lang => {
alarmMessage[lang] =
src.assistMsg[lang] + ' ' + src.emojiList[Math.floor(rand * src.emojiList.length)]
})
users.forEach(user => { bot.sendMessage(user.id, alarmMessage[user.lang]) })
assists = currAssists
shuffle(src.emojiList)
return true
}
return false
}
function shuffle(arr){
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]]
}
}