Code running twice?
hsnabn opened this issue · 2 comments
Hi all. I'm having a strange issue which seems to be a botogram issue, since it works when I use another framework for the API.
Using:
botogram2-0.6 installed via Pip
Python 3.8
Windows 10 x64
import botogram
bot = botogram.create("API-KEY")
player_ids = []
@bot.command("begin")
def cmd_begin(chat, message, args):
"""Begin a game."""
btns = botogram.Buttons()
btns[0].callback("Join.", "join_game")
btns[1].callback("Embark.", "embark_game")
chat.send("Press Join to join the party.\nPress Embark to begin your journey.", attach=btns)
@bot.callback("join_game")
def clbk_join_game(query, chat, message):
if query.sender.id in player_ids:
query.notify("Already joined.")
return
player_ids.append(query.sender.id)
query.notify("Joined the party.")
bot.logger.info(f"Added player name {query.sender.name} and id {query.sender.id}")
bot.logger.info(f"{len(player_ids)} players in list")
@bot.callback("embark_game")
def clbk_embark_game(query, chat, message):
if not player_ids:
query.notify("Not enough players! One or more players should join.")
return
if __name__ == "__main__":
bot.run()
With this code, a user is able to press Join twice and receive the "Joined the party." message twice, and the logging shows that the user has been in fact added twice. The return condition for the callback is somehow not satisfying when I press the button the second time, while for all purposes it should... shouldn't it?
I tried the same with another API wrapper for Python and it works as expected.
It's not a bug: Botogram has multiple workers, so you can't use global variables like player_ids
.
You can use a database (in this case I suggest you redis
) to store things shared across workers.
Ahh, makes sense! Thanks for the quick reply.