-
Notifications
You must be signed in to change notification settings - Fork 2
/
util-add.js
128 lines (119 loc) · 3.75 KB
/
util-add.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
const Database = require('@replit/database')
const db = new Database()
//getting only the embedder function
const { embedder } = require('./utility')
//to calculate totalDays from startDate till now
const { totalDays } = require('./util-functions')
/**
* To add the given log into the record of the sender
* @param {Message} msg Discord Message prefix '++add'
* @returns {Promise<Message>|<Message[]>} the new DIscord Message sent from the bot
*/
const add = async (msg) => {
/**
* to first clear out the newLog
* @type {string}
*/
newLog = msg.content.split('++add')[1].trim()
//if empty string return
if (newLog === '') return embedder(msg, `Cannot add empty log.`)
/**
* gets the record with the specified key
* then checking if logs is not null and
* logs.info is not undefined
*/
db.get(msg.author.id + '').then((logs) => {
if (logs && typeof logs.info !== 'undefined') {
//if exists, push into the list and set as new record
/**
* get the current day number
* @type {number}
*/
var day = await totalDays(logs.startDate)
logs.info.push({ logged: newLog, date: day })
db.set(msg.author.id + '', logs).then(() => {
embedder(
msg,
`${msg.author}, you have successfully logged in for day ${day}\nYay you did it! Remember to log next day 🥰`
)
})
/**
* deleteing the log message if hidden account
* and not in DMChannel of Discord
*/
if (!logs.open && msg.channel.type !== 'dm')
msg.delete().then(console.log('Msg deleted')).catch(console.error)
} else {
//for creating of a new record
db.set(msg.author.id + '', {
userName: msg.author.tag,
info: [
{
logged: newLog,
date: 1,
},
],
startDate: Date.now(),
open: true,
}).then(() => {
embedder(msg, `Logging your first DayOfCode!!`)
})
}
})
}
/**
* To clear out all the logs of current day for the author of the message
* And add only the log present in the content with prefix '++update '
* @param {Message} msg Discord Message with prefix '++update'
* @return {Promise<Message>|<Message[]>} the new DIscord Message sent from the bot
*/
const update = async (msg) => {
/**
* to first clear out the newLog
* @type {string}
*/
newLog = msg.content.split('++update')[1].trim()
if (newLog === '') return embedder(msg, `Cannot add empty log.`)
db.get(msg.author.id + '').then((logs) => {
if (logs && typeof logs.info !== 'undefined') {
//if exists, push into the list and set as new record
/**
* get the current day number
* @type {number}
*/
var day = await totalDays(logs.startDate)
/**
* filter out all the logs of current day
* and then push into the list a new record
*/
logs.info = logs.info.filter((log) => log.date != day)
logs.info.push({ logged: newLog, date: day })
db.set(msg.author.id + '', logs).then(() =>
embedder(
msg,
`${msg.author}, you have successfully updated your logs for day ${day}`
)
)
/**
* deleteing the log message if hidden account
* and not in DMChannel of Discord
*/
if (!logs.open && msg.channel.type !== 'dm')
msg.delete().then(console.log('Msg deleted')).catch(console.error)
}
//create a new record for the user
else
db.set(msg.author.id + '', {
userName: msg.author.tag,
info: [{ logged: newLog, date: 1 }],
startDate: Date.now(),
open: true,
}).then(() => {
embedder(msg, `You didnt have any logs. Logging your first DayOfCode!!`)
})
})
}
module.exports = {
add,
update,
}