Access variables between commands with discord.py

后端 未结 2 722
执念已碎
执念已碎 2020-12-12 05:47

I have this (overly simplified) Discord bot

voting_enabled = False

@bot.command()
async def start():
    voting_enabled = True

@bot.command()
async def fin         


        
2条回答
  •  误落风尘
    2020-12-12 06:16

    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
    

提交回复
热议问题