-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
106 lines (106 loc) · 3.53 KB
/
utils.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
const fs = require('fs')
const usersDataFile = './data/data_users.json'
const optionsDataFile = './data/data_options.json'
module.exports = {
checkUsername: async function (msg, usersObj) {
if (msg.from.username !== usersObj[msg.chat.id][msg.from.id].username ||
msg.from.first_name !== usersObj[msg.chat.id][msg.from.id].firstname ||
msg.from.last_name !== usersObj[msg.chat.id][msg.from.id].lastname) {
// Username or first name has changed
usersObj[msg.chat.id][msg.from.id].username = msg.from.username
usersObj[msg.chat.id][msg.from.id].firstname = msg.from.first_name
usersObj[msg.chat.id][msg.from.id].lastname = msg.from.last_name
module.exports.writeUsersDataToFile(usersObj)
}
return usersObj
},
createUserList: async function (msg, usersObj) {
if(!usersObj[msg.chat.id][msg.from.id]) {
usersObj[msg.chat.id][msg.from.id] = {
'wallet' : 0,
'username' : msg.from.username,
'firstname' : msg.from.first_name,
'lastname' : msg.from.last_name
}
module.exports.writeUsersDataToFile(usersObj)
}
return usersObj
},
createChatList: function (msg, usersObj) {
if (!usersObj[msg.chat.id]) {
usersObj[msg.chat.id] = {}
module.exports.writeUsersDataToFile(usersObj)
}
return usersObj
},
createOptionsList: function (msg, usersObj) {
if (!usersObj[msg.chat.id]) {
usersObj[msg.chat.id] = {
'box' : 40,
'bottle' : 2,
'currency' : '$'
}
module.exports.writeOptionsDataToFile(usersObj)
}
return usersObj
},
writeUsersDataToFile: function (items) {
module.exports.writeFile(usersDataFile, items)
},
writeOptionsDataToFile: function (items) {
module.exports.writeFile(optionsDataFile, items)
},
writeFile: function (filepath, item) {
fs.writeFile(filepath, JSON.stringify(item, null, 2), 'utf8', function (err) {
if (err) {
return console.log(err)
}
})
},
doesFileExists: function (filePath){
try {
if (fs.existsSync(filePath)) {
return true
} else {
return false
}
} catch(err) {
console.error(err)
}
},
readUsersData: function () {
let tmp = fs.readFileSync(usersDataFile, 'utf8')
// Let transform the string to an object, see https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object
return JSON.parse(tmp)
},
readOptionsData: function () {
let tmp = fs.readFileSync(optionsDataFile, 'utf8')
// Let transform the string to an object, see https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object
return JSON.parse(tmp)
},
createUsersDataFile: function () {
fs.appendFileSync(usersDataFile, '{}', 'utf8')
console.log(usersDataFile, 'should exist now...')
},
createOptionsDataFile: function () {
fs.appendFileSync(optionsDataFile, '{}', 'utf8')
console.log(optionsDataFile, 'should exist now...')
},
initializeUsers: function () {
if (!module.exports.doesFileExists(usersDataFile)) {
console.log("File doesn't exist!")
module.exports.createUsersDataFile()
}
return module.exports.readUsersData()
},
initializeOptions: function () {
if (!module.exports.doesFileExists(optionsDataFile)) {
console.log("File doesn't exist!")
module.exports.createOptionsDataFile()
}
return module.exports.readOptionsData()
},
findUserIdByUsername: function (data, username) {
return Object.keys(data).find( k => data[k]['username'] === username )
}
}