Issue
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.
Solution
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
Answered By - Łukasz Kwieciński
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.