简体   繁体   中英

How do you make a loop in discord.py?

I am trying to make a timer that shows and embed how much time left, but while loops and for loops don't work on discord.py

here is the code

@client.command(aliases = ['timer'])
async def timer_to(ctx, time: int):
    await ctx.send(f"OK\nTimer set to {time} seconds")
    def check(message):
        return message.channel == ctx.channel and message.author == ctx.author and message.content.lower() == "cancel timer"
    try:
        await client.wait_for("message", check=check, timeout=time)
        await ctx.send("Timer cancelled")
    except asyncio.TimeoutError:
        await ctx.send(f"{ctx.message.author.mention} timer finished")

There actually is a way to implement loops compatible with discord.py The pseudo code is as follows

from discord.ext.tasks import loop

@loop(seconds=1)
async def server_timer():
    await client.wait_until_ready()
    # your code here

my_loop.start()
client.run('Token')

You can find more information here in the discord.py docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM