Me and my group are currently programming a telegrambot (in python) and want it to play various games with you on demand. For example, a game should be executed with the command /game1. If the game process is active, only the commands for the game should be possible, as well as an /exit and a /help. Is there a way to program a Telegram bot in Python to block certain commands when executing a method?
Assuming the commands for the game number 1 are: /command1 and /command2 and for the game number 2: /command3 and /command4. If I now play the first game with /game1, it should not be possible to execute /command3 and /command4 until I end the current game with /exit and activate the second game with /game2.
>Solution :
Just create a dictionary mapping the game with the list of allowed commands for that game and then check whether the command is contained in that list.
One way to go could be
def is_command_allowed(game, command):
game_commands = {
'game1': ['command1', 'command2'],
'game2': ['command3', 'command4'],
}
return command in game_commands[game]
# READ INPUT i.e. game and command (here hardcoded)
# EXAMPLE 1
game = 'game1'
command = 'command3'
is_command_allowed(game, command) # False
# EXAMPLE 2
game = 'game2'
command = 'command4'
is_command_allowed(game, command) # True