Modify global variable from async function in python

Viewed 9948

I'm making a Discord bot in Python using discord.py. I'd like to set/modify a global variable from an async thread.

message = ""

@bot.command()
async def test(ctx, msg):
    message = msg

However this doesn't work. How can I achieve something that does this?

1 Answers

As I said in the comment you have to use the keyword global in the functions wherever you are modifying the global variable. If you are just reading it in function than you don't need it.

message = ""

@bot.command()
async def test(ctx, msg):
    global message
    message = msg
Related