'python discord bot sleep to stop people spamming

when trying the make the bot sleep for 5 seconds before it replies again. but i can continue spamming "down" and bot replies

import os
import random
import discord
import time 

keywords3 = ['down']

@client.event
async def on_message(message):
global cooldown

if message.author == client.user:
    return

 messC = message.content.lower()

if any(word in messC for word in keywords3):
    await message.channel.send('hello')
    await asyncio.sleep(5)

`



Solution 1:[1]

i'm not familiar with the discord api but this should do what you want

cooling_down = False
eventually_cool_up_task = None

async def eventually_cool_up(wait):
    global cooling_down

    await asyncio.sleep(wait)
    cooling_down = False

@client.event
async def on_message(message):
    global cooling_down, eventually_cool_up_task

    if message.author == client.user:
        return

    messC = message.content.lower()

    if any(word in messC for word in keywords3):
        if not cooling_down:
            cooling_down = True
            await message.channel.send('hello')
            eventually_cool_up_task = asyncio.create_task(eventually_cool_up(5))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 avi