-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil-check.js
147 lines (140 loc) · 4.43 KB
/
util-check.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* This module is for checking
* That is to route for any @mentions in the Message
*/
const Database = require('@replit/database')
const db = new Database()
const { embedder, profile } = require('./utility')
const { calculateStreak } = require('./util-functions')
const { listAll } = require('./util-list')
/**
*
* @param {Message} msg the discord message read
* @param {User} user the user for which streak is to be calculated
* @param {array} logs the array of logged messages
* @param {number} startDate the time in milliseconds of first log
*/
const streak = async (msg, user, logs, startDate) => {
/**
* str is the number of current streak
* if zero means not logged for today
* else the streak till the current day
* @type {number}
*/
str = await calculateStreak(logs, startDate)
if (str) embedder(msg, `Current streak for **${user.username}** is ${str}`)
else
embedder(
msg,
`Hey mate you must add something to today's log, or else your streak will become \`0\` !!`
)
}
/**
* utility message to be called to route and check for @mentions
* and call the streak method accordingly
* @param {Message} msg the discord message with prefix '++streak'
*/
const utilStreak = async (msg) => {
//check if any @mentions are present
if (msg.mentions.users.first() === undefined)
//no @mentions, call the streak method with the msg author
db.get(msg.author.id + '').then((logs) => {
if (logs != null && typeof logs.info !== 'undefined')
streak(msg, msg.author, logs.info, logs.startDate)
else
embedder(
msg,
`**${msg.author.tag}**, you do not have any current logs. Use \`++add {log}\` to start logging.`
)
})
else {
//@mentioned, call the streak method with the mentioned user
user = msg.mentions.users.first()
db.get(user.id + '').then((logs) => {
if (
logs != null &&
typeof logs.info !== 'undefined' &&
logs.info.length > 0
)
streak(msg, user, logs.info, logs.startDate)
else
embedder(
msg,
`${user.username} has no active logs, cannot calculate streak!`
)
})
}
}
/**
* routes to the profile function according to the @mentions in the
* provided message
* @param {Message} msg Discord Message with prefix '++profile'
*/
const utilProfile = async (msg) => {
//check if and @mentions are present
if (msg.mentions.users.first() === undefined)
//if not, then call the profile with the author of the message
db.get(msg.author.id).then((logs) => {
if (logs && typeof logs.info !== 'undefined')
profile(msg, msg.author, logs)
else
msg.reply(
`You don't have any logs currently. Start logging with us to check out your profile.`
)
})
else {
//if @mentioned, then call the profile for the first user mention
user = msg.mentions.users.first()
db.get(user.id + '').then((logs) => {
if (logs && typeof logs.info !== 'undefined') profile(msg, user, logs)
else
embedder(
msg,
`The user, ${user.username}, has not started logging yet.`
)
})
}
}
/**
* roputes according to the @mentions present in the message
* to print the complete list of logs by the @function listAll
* @param {Message} msg Discord Message with prefix '++list'
*/
const utilList = async (msg) => {
//chcek if any @mentions are present
if (msg.mentions.users.first() === undefined) {
//call the @function listAll for the author of the message
db.get(msg.author.id + '').then((logs) => {
if (
logs != null &&
typeof logs.info !== 'undefined' &&
logs.info.length > 0
)
listAll(msg, logs.info, logs.open)
else
embedder(
msg,
`You do not have any logs ${msg.author}. Start logging with \`++add {log}\` to check out your logs.`
)
})
} else {
//if @mention found, call listAll for the specified user
user = msg.mentions.users.first()
db.get(user.id + '').then((logs) => {
if (
logs != null &&
typeof logs.info !== 'undefined' &&
logs.info.length > 0
)
if (logs.open) listAll(msg, logs.info, logs.open)
else
msg.channel.send(`The user, ${user.username}'s account is private.`)
else embedder(msg, `${user.username} has not started any logging yet.`)
})
}
}
module.exports = {
utilProfile,
utilStreak,
utilList,
}