0

An example of what I want to do would be something like this

i = 0
@bot.command()
async def IncreaseI(ctx):
    global i
    while True:
        i += 1
        sleep(5)

@bot.command()
async def PrintI(ctx):
    await ctx.send(f"I: {i}")
    

but this doesn't work.

0

1 Answer 1

1

First of all, you're using time.sleep(), that's gonna block your whole code, use await asyncio.sleep() instead. Second of all it's better to create a task, it's a lot easier to manage, here's how:

from discord.ext import tasks

i = 0

@tasks.loop(seconds=5)
async def increaseI():
    global i
    i += 1


increaseI.start() # You can also start in a command

# You can also
increaseI.stop()
increaseI.cancel()
increaseI.restart()
# Read the docs for more methods

Reference

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.