Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify global variable from async function in python [duplicate]

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?

like image 447
Máté Varga Avatar asked Mar 23 '26 18:03

Máté Varga


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
like image 115
PraveenB Avatar answered Mar 25 '26 06:03

PraveenB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!