Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] /setprice command #20

Merged
merged 1 commit into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed ancien index.js
Empty file.
31 changes: 31 additions & 0 deletions dataHandle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs')
const usersDataFile = './users_data.json'
const usersPricesFile = './prices_data.json'
module.exports = {
createUserList: async function (msg, usersObj) {
if(!usersObj[msg.chat.id][msg.from.id]) {
Expand All @@ -26,6 +27,20 @@ module.exports = {
})
}
return usersObj
},
createPricesList: function (msg, usersObj) {
if (!usersObj[msg.chat.id]) {
usersObj[msg.chat.id] = {
'box' : 40,
'bottle' : 2
}
fs.writeFileSync(usersPricesFile, JSON.stringify(usersObj, null, 2), 'utf8', function (err) {
if (err) {
return console.log(err)
}
})
}
return usersObj
},
doesFileExists: function (filePath){
try {
Expand All @@ -43,15 +58,31 @@ module.exports = {
// 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)
},
readPricesData: function () {
let tmp = fs.readFileSync(usersPricesFile, '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...')
},
createPricesDataFile: function () {
fs.appendFileSync(usersPricesFile, '{}', 'utf8')
console.log(usersPricesFile, 'should exist now...')
},
initializeUsers: function () {
if (!module.exports.doesFileExists(usersDataFile)) {
console.log("File doesn't exist!")
module.exports.createUsersDataFile()
}
return module.exports.readUsersData()
},
initializePrices: function () {
if (!module.exports.doesFileExists(usersPricesFile)) {
console.log("File doesn't exist!")
module.exports.createPricesDataFile()
}
return module.exports.readPricesData()
},
}
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const TeleBot = require('telebot')
const Secrets = require('./secrets.json')
const fs = require('fs')
const bot = new TeleBot(Secrets.BOT_TOKEN)
const dh = require('./dataHandle')

var users = dh.initializeUsers()
var prices = dh.initializePrices()

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

var commands = {
Expand All @@ -18,6 +22,10 @@ var commands = {
{
'name': "help",
'desc': 'Shows a list of available commands'
},
{
'name': "setprice",
'desc': 'Sets the price of a box or a bottle of mate'
}
]
}
Expand All @@ -36,6 +44,44 @@ bot.on([`/${commands.list[1].name}`], (msg) => {
msg.reply.text(helpmsg)
});

// /setprice command
bot.on([`/${commands.list[2].name}`], (msg) => {
var args = msg.text.split(" ")

if (args[1] && args[2]) {
if (isNaN(args[2])) {
msg.reply.text("You must type a number!")
} else {
switch (args[1]) {
case "box":
prices[msg.chat.id].box = parseInt(args[2])

fs.writeFile('./prices_data.json', JSON.stringify(prices, null, 2), 'utf8', function (err) {
if (err) {
return console.log(err)
}
})

msg.reply.text(`The price of a box of mate is now ${prices[msg.chat.id].box} .-`)
break;
case "bottle":
prices[msg.chat.id].bottle = parseInt(args[2])

fs.writeFile('./prices_data.json', JSON.stringify(prices, null, 2), 'utf8', function (err) {
if (err) {
return console.log(err)
}
})

msg.reply.text(`The price of a bottle of mate is now ${prices[msg.chat.id].bottle} .-`)
break;
default:
msg.reply.text(`${args[1]} is invalid!`)
}
}
}
});

// bot.on(/^\/send(.+)$/, (msg, props) => {
// const value = props.match[1].trim()
// console.log(value)
Expand Down
6 changes: 6 additions & 0 deletions prices_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"-331544286": {
"box": 40,
"bottle": 2
}
}