Answer the question
In order to leave comments, you need to log in
How to specify a variable outside of the Asynchronous code in this very code?
Good afternoon, I'm writing a bot for the discord. I need the variable "Matrix" to be used in a function, and then the modified "Matrix" can be used in another function.
Now my code looks like this:
import discord
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
Matrix = "â–¢"
@client.event
async def on_message(message):
if message.content.startswith("#on"):
Matrixsend = Matrix.replace(Matrix[0], "â–£")
Matrix = Matrixsend
await message.channel.send(''+str(Matrix))
@client.event
async def on_message(message):
if message.content.startswith("#off"):
Matrixsend = Matrix.replace(Matrix[0], "â–¢")
Matrix = Matrixsend
await message.channel.send(''+str(Matrix))
client.run("Тут могла быть ваша реклама")
Answer the question
In order to leave comments, you need to log in
1. It is not necessary to use on_message 2 times, you will only get an error.
2. You can use the global variable for full access to the variable from the code
import discord
client = discord.Client()
Matrix = "â–¢"
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
global Matrix
if message.content.startswith("#on"):
Matrixsend = Matrix.replace(Matrix[0], "â–£")
Matrix = Matrixsend
await message.channel.send(''+str(Matrix))
if message.content.startswith("#off"):
Matrixsend = Matrix.replace(Matrix[0], "â–¢")
Matrix = Matrixsend
await message.channel.send(''+str(Matrix))
client.run("Тут могла быть ваша реклама")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question