仅每5秒触发(聊天触发的机器人)的问题

问题描述 投票:1回答:1

我希望我的不和谐机器人,每5秒,根据我刚刚在聊天中输入的内容,发送一条消息说“你已经说过”。例如,如果我发送这些消息(下面)嗨(消息#1,已经过了一秒)嗨(消息#2,已经过了两秒)嗨(消息#3,已经过了三秒)嗨(消息#4, 5秒钟过去了)(Bot说):你说过了(请记住,机器人只说这一次,而不是4次)

但是,截至目前,它处于空闲状态,不会发送任何消息。我没有得到任何错误,机器人本身运行并在线。我想知道是否有人可以帮助我编辑我的代码,这样如果我说了些什么,5秒后,机器人会说“你已经说过”了一次。此代码之前的问题包括机器人垃圾邮件“你说过了”,这就是为什么我只想说“你已经说过”了一次。

(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#

我希望机器人每隔5秒,如果我说了什么,就说“你已经说过”了一次。

python-3.x discord discord.py
1个回答
3
投票

每当机器人看到一条消息时,请记录该消息的时间戳,并将其与机器人最后响应的消息的时间戳进行比较。如果超过五秒钟,请记录该时间戳并发送消息:

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")
© www.soinside.com 2019 - 2024. All rights reserved.