电报 – 节点。 Js处理多个更新

我正在用电报创build一个游戏,目前我有一个关于同时处理多个更新的问题。 我正在使用node.js

例如,我有这个代码

var TelegramBot = require('node-telegram-bot-api'), bot = new TelegramBot("MY_TOKEN", {polling: true}); bot.onText(/^\/createroom/, function (res, match) { //Here i have some logic, to check whether if the room already created or not service.checkIfRoomExist(res) // this service here, will always return false, because of the simultaneously chat .then (function(isExist) { if (isExist === false) { service.createRoom(res) .then (function() { }); } }); //it works fine, if player type "/createroom" not simultaneously //but if more than 1 player type "/createroom" simultaneously, my logic here doesn't work, it will create multiple room } 

有什么想法来解决这个问题?

非常感谢,任何帮助将不胜感激

您需要将唯一的聊天/用户ID链接到您的数据库,以防止这种冲突。 请参阅下面的代码和评论如何做到这一点。

 var TelegramBot = require('node-telegram-bot-api'), bot = new TelegramBot("MY_TOKEN", { polling: true }); bot.onText(/^\/createroom/, function (res, match) { //use res.chat.id for groups and res.user.id for individuals service.checkIfRoomExist(res.chat.id).then(function (isExist) { if (isExist === false) { service.createRoom(res.chat.id).then(function () { bot.sendMessage(res.chat.id, 'Initializing game!') // send game content here }); } bot.sendMessage(res.chat.id, 'A game has already started in this group!') }) }); function checkIfRoomExist(id) { // Your logic here that checks in database if game has been created }