-1

What I'm trying to do: I am trying to return a command when it is called after you send a message.

What I sent: command hello

Result: function() NameError: name 'function' is not defined

How can I fix this?

Code:

@bot.command()
async def command():
    global variable 
    variable = False
    async def function():
        variable = True
    if variable == True:
        return

@bot.event
async def on_message(message):
    function()
    await bot.process_commands(message)

raid6n
  • 55
  • 1
  • 7

1 Answers1

1

You cannot define a function inside another function. Python will not recognize this as an identifier that can be used outside of the scope it was defined in. Move your function definition outside, into the global scope like so:

async def function(variable):
    variable = True

@bot.command()
async def command():
    global variable 
    variable = False
    function(variable)
    if variable == True:
        return

@bot.event
async def on_message(message):
    function(some_other_variable)
    await bot.process_commands(message)