F
F
Flam3y2021-06-12 11:30:06
Python
Flam3y, 2021-06-12 11:30:06

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("Тут могла быть ваша реклама")

How can this be implemented in python 3.7?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Endora Blackwell, 2021-06-12
@Allan-BlackWell

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 question

Ask a Question

731 491 924 answers to any question