D
D
Dream2021-06-18 20:42:26
Python
Dream, 2021-06-18 20:42:26

How to delay a command in a discord bot?

How to delay per command? So that when you try to register a command, a message is displayed again that you need to wait a while.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Endora Blackwell, 2021-06-18
@dreameddd

Discord.py already provides this, you can add a decorator:

@commands.cooldown(1, (Время задержки), commands.BucketType.user)

For output, you need to use an event:
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandOnCooldown):
        embed = discord.Embed( 
            title = 'Команда на задержке.', 
            description = f'Повторить через `{error.retry_after :.0f} секунд', 
            colour = discord.Color.red() 
        )

        return await ctx.send(embed = embed)

A
Anton Neverov, 2021-06-18
@TTATPuOT

After applying the command, save the timestamp in the database. Further, when reapplying, calculate the timestamp of the moment when the minimum command should have been used the first time. If this time is more than what is in the database, execute the command. Otherwise, display a message.
You are free to come up with the implementation yourself. But you definitely need somewhere to save the user id, command id (if there are several commands) and the timestamp of the last use. Base, file or memory - you choose.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question