1
votes

I would like for my discord bot, to, every 5 seconds, based on if I have just recently typed something in chat, send a message saying "You have spoken". For example, if I sent these messages (below) Hi (message #1, one second has passed) Hi (message #2, two seconds have passed) Hi (message #3, three seconds have passed) Hi (message #4, 5 seconds have passed) (Bot says): You have spoken (keep in mind, the bot says this only one time, not 4 times)

However, as of now, it is idle and does not send any messages. I am not getting any errors, and the bot itself runs and is online. I was wondering if anyone could help me edit my code so that if I have said something, after 5 seconds, the bot says "You have spoken" only once. Previous problems before this code include the bot spamming "You have spoken", which is why I want it to only say "You have spoken" once.

(Below)I want it to make it so that whenever I talk, if 5 seconds have passed, the bot will say you have spoken (only once)

async def on_message(message): if message.author.id == 'XXXXXXXXXXXXXXX':

    mins = 0 #mins standing for minutes#
    num = 0 #var for counting how many times bot has sent msg#
    if "" in message.content.lower(): #means if I say anything#
      if mins % 5 == 0: #if seconds past is divisible by 5 (meaning 5 seconds have past)
        num +=1
        if num == 1:
          msg = 'You have spoken!'
          await client.send_message(message.channel,msg)
          num -=1 #make num 0 again so bot does not repeatedly send msg#
          time.sleep(5)
          mins +=1
        if (mins % 5)>0:
          time.sleep(5)
          mins +=1 #do nothing if not divisible#

I would like the bot to, every 5 seconds, if i have said anything, say "You have spoken" one time.

1

1 Answers

3
votes

Whenever the bot sees a message, take the timestamp of that message and compare it to the timestamp of the message that the bot responded to last. If more than five seconds have elapsed, record that timestamp and send a message:

from datetime import timedelta
from discord.ext.commands import Bot

bot = Bot(command_prefix="!")

last_time = None

@bot.event
async def on_message(message):
    global last_time
    if message.author == bot.user: # ignore our own messages
        return
    if message.author.id == ...:
        if last_time is None or message.created_at - last_time <= timedelta(seconds=5):
            last_time = message.created_at
            await message.channel.send("You have spoken")  # bot.send_message(message.channel, ...) on async

bot.run("TOKEN")