Reputation: 453
I have this (overly simplified) Discord bot
voting_enabled = False
@bot.command()
async def start():
voting_enabled = True
@bot.command()
async def finish():
voting_enabled = False
@bot.command()
async def vote():
if voting_enabled:
# Do something
else:
# Do something else
When I call call the vote()
command, it always goes through the else part of the code. Even after calling the start()
command
I want that the vote()
command behave differently depending on if the other two commands where called previously
I tried using the global
keyword like this on the first line
global voting_enabled
voting_enabled = False
But it did nothing
Upvotes: 1
Views: 14336
Reputation: 161
Except don't use globals because they stinky. Discord.py has another way to do this.
bot.voting_enabled = False
@bot.command()
async def start():
bot.voting_enabled = True
@bot.command()
async def finish():
bot.voting_enabled = False
@bot.command()
async def vote():
if bot.voting_enabled:
# Do something
else:
# Do something else
Upvotes: 16
Reputation: 453
The global
keyword was not used correctly.
global
should be defined within every function.
Example:
voting_enabled = False
@bot.command()
async def start():
global voting_enabled
voting_enabled = True
@bot.command()
async def finish():
global voting_enabled
voting_enabled = False
@bot.command()
async def vote():
global voting_enabled
if voting_enabled:
# Do something
else:
# Do something else
Upvotes: 4