Skip to content

Commit

Permalink
Merge pull request #26 from epfl-dojo/feature/balance
Browse files Browse the repository at this point in the history
[FT.] /balance
  • Loading branch information
JaavLex authored Dec 20, 2019
2 parents 3715050 + b7c052e commit 5e2414b
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,31 @@ bot.on('text', async (msg) => {
})

var commands = {
'list': [{
'name': "start",
'desc': 'Welcome command.'
},
{
'name': "help",
'desc': 'Shows a list of available commands.'
},
{
'name': "setprice",
'desc': 'Sets the price of a box or a bottle of mate.'
},
{
'name': "drink",
'desc': 'The user drinks a club-mate.'
},
{
'name': "buybox",
'desc': 'The user buys a box of club mate.'
}
]
'list': [{
'name': "start",
'desc': 'Welcome command.'
},
{
'name': "help",
'desc': 'Shows a list of available commands.'
},
{
'name': "setprice",
'desc': 'Sets the price of a box or a bottle of mate.'
},
{
'name': "drink",
'desc': 'The user drinks a club-mate.'
},
{
'name': "buybox",
'desc': 'The user buys a box of club mate.'
},
{
'name': "balance",
'desc': 'Shows the status of everyone\'s wallet.'
}
]
}

// /start command
Expand Down Expand Up @@ -103,13 +107,22 @@ bot.on([`/${commands.list[3].name}`], (msg) => {

// /buybox command
bot.on([`/${commands.list[4].name}`], (msg) => {
users[msg.chat.id][msg.from.id].wallet -= parseInt(prices[msg.chat.id].box)
fs.writeFile('./users_data.json', JSON.stringify(users, null, 2), 'utf8', function(err) {
if (err) {
return console.log(err)
}
})
msg.reply.text(`Thanks ${users[msg.chat.id][msg.from.id].username} just bought a box of club-mate ! :)\nYou currently have ${users[msg.chat.id][msg.from.id].wallet} in your wallet!`)
users[msg.chat.id][msg.from.id].wallet -= parseInt(prices[msg.chat.id].box)
fs.writeFile('./users_data.json', JSON.stringify(users, null, 2), 'utf8', function(err) {
if (err) {
return console.log(err)
}
})
msg.reply.text(`Thanks ${users[msg.chat.id][msg.from.id].username} just bought a box of club-mate ! :)\nYou currently have ${users[msg.chat.id][msg.from.id].wallet} in your wallet!`)
});

// /balance command
bot.on([`/${commands.list[5].name}`], (msg) => {
let tmpMsg = ""
Object.values(users[msg.chat.id]).forEach(element =>
tmpMsg += `${element.username}${element.wallet}.-\n`
)
msg.reply.text(tmpMsg)
});

// bot.on(/^\/send(.+)$/, (msg, props) => {
Expand Down

0 comments on commit 5e2414b

Please sign in to comment.