Skip to content

Commit

Permalink
Merge pull request #19 from epfl-dojo/feature/functionsFiles
Browse files Browse the repository at this point in the history
Feature/functions files
  • Loading branch information
JaavLex authored Dec 17, 2019
2 parents d98fee1 + d61c975 commit b0ddbb5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
Empty file added ancien index.js
Empty file.
47 changes: 20 additions & 27 deletions dataHandle.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const fs = require('fs')
const usersDataFile = 'users_data.json'
const usersDataFile = './users_data.json'
module.exports = {
createUserList: async function (msg, usersObj) {
console.log(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
}
console.debug(usersObj)
fs.writeFile(usersDataFile, JSON.stringify(usersObj, null, 2), 'utf8', function (err) {
if (err) {
return console.log(err)
Expand All @@ -18,47 +16,42 @@ module.exports = {
}
return usersObj
},
createChatList: async function (msg, usersObj) {
createChatList: function (msg, usersObj) {
if (!usersObj[msg.chat.id]) {
usersObj = {
[msg.chat.id]: {
}
}
fs.writeFile(usersDataFile, JSON.stringify(usersObj, null, 2), 'utf8', function (err) {
usersObj[msg.chat.id] = {}
fs.writeFileSync(usersDataFile, JSON.stringify(usersObj, null, 2), 'utf8', function (err) {
if (err) {
return console.log(err)
}
})
}
console.log(usersObj)
return usersObj
},
doesFileExists: function () {
console.log('Does', usersDataFile, 'exists ?')
try {
if (fs.existsSync(usersDataFile)) {
console.log(' → yes')
return true
} else {
console.log(' → no')
return false
}
} catch(err) {
console.error(err)
}
},
doesFileExists: function (filePath){
try {
if (fs.existsSync(filePath)) {
return true
} else {
return false
}
} catch(err) {
console.error(err)
}
},
readUsersData: function () {
return fs.readFileSync(usersDataFile, 'utf8')
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)
},
createUsersDataFile: function () {
fs.appendFileSync(usersDataFile, '{}', 'utf8')
console.log(usersDataFile, 'should exist now...')
},
initializeUsers: function () {
if (!module.exports.doesFileExists()) {
if (!module.exports.doesFileExists(usersDataFile)) {
console.log("File doesn't exist!")
module.exports.createUsersDataFile()
}
return module.exports.readUsersData()
}
},
}
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ const bot = new TeleBot(Secrets.BOT_TOKEN)
const dh = require('./dataHandle')

var users = dh.initializeUsers()

bot.on('text', (msg) => {
msg.reply.text(msg.text)
dh.createChatList(msg, users)
dh.createUserList(msg, users)
bot.on('text', async (msg) => {
users = await dh.createChatList(msg, users)
users = await dh.createUserList(msg, users)
})

var commands = {
Expand Down Expand Up @@ -38,4 +36,9 @@ bot.on([`/${commands.list[1].name}`], (msg) => {
msg.reply.text(helpmsg)
});

// bot.on(/^\/send(.+)$/, (msg, props) => {
// const value = props.match[1].trim()
// console.log(value)
// })

bot.start()

0 comments on commit b0ddbb5

Please sign in to comment.