1
1
1 22020-07-19 07:40:43
Python
1 2, 2020-07-19 07:40:43

Discord.py How to output a variable in one command to another?

I have a variable in one command and I need to output it from another, I tried to use global, but nothing has changed, an error occurs that the moder variable is missing.

@client.command()
async def _init_( ctx ):
  global moder
  embed = discord.Embed(
    title = 'Это мой заголовок',
    description = 'Это мой основной текст',
    color = 0x83c837
  )
  moder = await ctx.send(
    embed = embed
  )


@client.command()
async def test( ctx ):
  global moder
  await ctx.send(
    moder
  )

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
soremix, 2020-07-19
@weRifiCatoR

If you want to use globals, the variable must initially be declared outside of any functions

_
_, 2020-07-19
@mrxor

Make moder a global variable

moder = None

@client.command()
async def _init_( ctx ):
  global moder
  embed = discord.Embed(
    title = 'Это мой заголовок',
    description = 'Это мой основной текст',
    color = 0x83c837
  )
  moder = await ctx.send(
    embed = embed
  )


@client.command()
async def test( ctx ):
  await ctx.send(
    moder
  )

T
Taruu, 2020-07-21
@Taruu

In this case, you need to use Class

class MyClass:
    @client.command()
    async def _init_(sefl,ctx):
        self.moder = None
        embed = discord.Embed(
            title='Это мой заголовок',
            description='Это мой основной текст',
            color=0x83c837)
        self.moder = await ctx.send(embed=embed)

    @client.command()
    async def test(sefl,ctx):
        await ctx.send(
            self.moder
        )

I strongly advise you to look at what asynchronous programming is and why there is clearly no place for global variables :/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question