Skip to main content
deleted 179 characters in body; edited tags
Source Link
FLAK-ZOSO
  • 4.2k
  • 5
  • 11
  • 35

My problem is similar to this, but it's not into a class.

My code is this (after removing some ininfluent lines):

@Bot.command()
async def Limonate(ctx, secondi, menzione = Nonemenzione=None):
    global attesa
    global canale
    attesa = float(secondi)
    canale = ctx.channel

This is a command, and I need that part to do this:

@Bot.event
async def on_ready():
    print(Bot.user, " è ora online ", "ID: ", Bot.user.id)
    attesa = 100
    global canale #The problem is there
    while (True):
        frase = choice(frasi_ad_effetto)
        await sleep(attesa)
        global canale # Here's the problem
        global menzione
        await canale.send(menzione + ', ' + frase) # 

So, the problem is that, if the function is alreadyBot has just started, the global keywork doesn't work, because when (and the bot is readycommand Limonate was never executed), the canale variable "canale" is empty.

Do you know any way to solve my problem?

If there's not a way to do that, I'll avoid it just using the ID of a random-choiced channel.

My problem is similar to this, but it's not into a class.

My code is this (after removing some ininfluent lines):

@Bot.command()
async def Limonate(ctx, secondi, menzione = None):
    global attesa
    attesa = float(secondi)
    canale = ctx.channel

This is a command, and I need that part to do this:

@Bot.event
async def on_ready():
    print(Bot.user, " è ora online ", "ID: ", Bot.user.id)
    attesa = 100
    global canale #The problem is there
    while (True):
        frase = choice(frasi_ad_effetto)
        await sleep(attesa)
        global canale
        global menzione
        await canale.send(menzione + ', ' + frase)

So, the problem is that, if the function is already started, the global keywork doesn't work, because when the bot is ready, the variable "canale" is empty.

Do you know any way to solve my problem?

If there's not a way to do that, I'll avoid it just using the ID of a random-choiced channel.

My problem is similar to this, but it's not into a class.

My code is this (after removing some ininfluent lines):

@Bot.command()
async def Limonate(ctx, secondi, menzione=None):
    global attesa
    global canale
    attesa = float(secondi)
    canale = ctx.channel

This is a command, and I need that part to do this:

@Bot.event
async def on_ready():
    attesa = 100
    while (True):
        frase = choice(frasi_ad_effetto)
        await sleep(attesa)
        global canale # Here's the problem
        global menzione
        await canale.send(menzione + ', ' + frase) # 

So the problem is that, if the Bot has just started (and the command Limonate was never executed), the canale variable is empty.

Do you know any way to solve my problem?

Source Link
FLAK-ZOSO
  • 4.2k
  • 5
  • 11
  • 35

I've problems with Python global

My problem is similar to this, but it's not into a class.

My code is this (after removing some ininfluent lines):

@Bot.command()
async def Limonate(ctx, secondi, menzione = None):
    global attesa
    attesa = float(secondi)
    canale = ctx.channel

This is a command, and I need that part to do this:

@Bot.event
async def on_ready():
    print(Bot.user, " è ora online ", "ID: ", Bot.user.id)
    attesa = 100
    global canale #The problem is there
    while (True):
        frase = choice(frasi_ad_effetto)
        await sleep(attesa)
        global canale
        global menzione
        await canale.send(menzione + ', ' + frase)

So, the problem is that, if the function is already started, the global keywork doesn't work, because when the bot is ready, the variable "canale" is empty.

Do you know any way to solve my problem?

If there's not a way to do that, I'll avoid it just using the ID of a random-choiced channel.