D
D
dpushin12020-11-17 12:12:06
Python
dpushin1, 2020-11-17 12:12:06

How to update the time every minute?

How can I make the time update every minute so that the rest of the functions work?
I'm probably dumb, but here's a piece of code:

@client.command(pass_context=True)
@commands.has_any_role(admins[0], admins[1], admins[2])
async def clear(ctx, amount=100):
    await ctx.channel.purge(limit=amount + 1)
    emb = discord.Embed(colour=discord.Color.green())
    emb.set_footer(text=f'Администратор {ctx.author.name} удалил {amount} сообщений', icon_url=ctx.author.avatar_url)
    await ctx.send(embed=emb)

And I have 5 such functions, somewhere among them I need to do so that every minute the current time is displayed to me in the console, but through while True other functions do not work for me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2020-11-17
@dpushin1

Don't use pass_context. This argument does not exist in the rewrite version of the library.
Regarding the question, using while Trueasyncio in code is usually not the best idea, since it almost always blocks the main loop.
Use asyncio tasks: https://docs.python.org/3/library/asyncio-task.html
There is also an extension in discord.py to make it easy to create and process tasks (such as canceling a loop, or a network crash while looping): https://discordpy.readthedocs.io/en/stable/ext/tas...

from discord.ext import tasks, commands

@tasks.loop(seconds=5.0)
async def printer():
    print("5 seconds has passed")

printer.start()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question