【问题标题】:I need to make a discord bot stop its process and wait a few seconds if people spam its command我需要让一个不和谐的机器人停止它的进程并等待几秒钟,如果人们向它的命令发送垃圾邮件
【发布时间】:2021-07-16 23:58:41
【问题描述】:

我正在制作一个不和谐的机器人,它会在另一个用户输入特定单词时发送消息并且它工作正常,但我需要有一种方法让机器人停止其进程并在短时间内忽略命令如果需要激活它的单词在短时间内被大量输入,则需要时间。

我已经对我的代码进行了一些工作以检测垃圾邮件,但我仍然无法弄清楚如何阻止它。

import os
import discord
import datetime
import time

from keep_on import keep_on

bot_token = os.environ['TOKEN']

client = discord.Client()

@client.event
async def on_ready():
  print('{0.user} is online'.format(client))
  print('###########################################')

enabled = True

time_storage = int(datetime.datetime.utcnow().timestamp())

@client.event
async def on_message(message):
  global enabled
  global time_storage
  sentWord = message.content
  CSentWord = sentWord.upper()
  if message.content == "!start":
    enabled = True
    await message.channel.send("Having Trouble?")
    await message.channel.send(file=discord.File('Having trouble.png'))
  elif message.content == "!stop":
    enabled = False
    await message.channel.send("Ok Ill Stop.")
    await message.channel.send(file=discord.File('Joey will stop.png'))
  elif enabled == True:
    if message.author == client.user:
      return
    if "SORRY" in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))
      time_storage = int(datetime.datetime.utcnow().timestamp())
    if "S_O_R_R_Y" in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))
      time_storage = int(datetime.datetime.utcnow().timestamp())
    if "S.O.R.R.Y" in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))
      time_storage = int(datetime.datetime.utcnow().timestamp())
    if "S|O|R|R|Y" in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))
      time_storage = int(datetime.datetime.utcnow().timestamp())
    if "S/O/R/R/Y" in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))
      time_storage = int(datetime.datetime.utcnow().timestamp())
    if "S\O\R\R\Y" in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))
      time_storage = int(datetime.datetime.utcnow().timestamp())
    if "FEEL BAD" in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))
      time_storage = int(datetime.datetime.utcnow().timestamp())
    if ".F.E.E.L. .B.A.D." in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))
      time_storage = int(datetime.datetime.utcnow().timestamp())
    if ".F.E.E.L.B.A.D." in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))
      time_storage = int(datetime.datetime.utcnow().timestamp())
    if "FEELBAD" in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))  
      time_storage = int(datetime.datetime.utcnow().timestamp())
    if "F_E_E_L_B_A_D" in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))
      time_storage = int(datetime.datetime.utcnow().timestamp())
    if "F|E|E|L B|A|D" in CSentWord:
      time.sleep(0.5)
      time_delay = int(datetime.datetime.utcnow().timestamp())
      time_difference = time_delay - time_storage
      print('time since last stored:',time_storage)
      print('time now:',time_delay)
      print('time difference:',time_difference)
      print('###########################################')
      if time_difference < 5:
        await message.channel.send("You are moving too fast")
        await message.channel.send(file=discord.File('Bruh what.png'))
        time.sleep(2)
        return
      await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
      await message.channel.send(file=discord.File('Joey Is Sorry.png'))
      time_storage = int(datetime.datetime.utcnow().timestamp())
               
keep_on()
client.run(os.getenv('TOKEN'))

【问题讨论】:

  • 也许添加一个不会进入您的非垃圾邮件案例的操作标志?输入1时设置,操作完成时取消设置?或者另一种方法是将您的异步输入用于最大大小的队列。如果 FIFO 应该很容易处理。
  • 你能用一些代码解释一下吗?

标签: python discord discord.py bots


【解决方案1】:

discord.py rewrite 中有一个装饰器可以为命令设置冷却时间。

@commands.cooldown(1, 30, commands.BucketType.user)

但为此,您需要重写您的机器人以使用 discord.ext.commands

from discord.ext import commands # you need this
import discord

bot = commands.Bot(command_prefix=";", intents=intents)

否则,您可以使用 datetime.datetime() 运行命令时获取当前时间戳并将其存储在变量中。然后下次运行时,您可以检查当前日期时间是否距离之前的变量足够早

【讨论】:

  • 你好,我花了最后一个时间处理这个问题,我设法让时间戳正常工作,当有垃圾邮件时它会发送不同的消息,但它仍然是垃圾邮件。例如,如果您在短时间内发送了 3 条消息,它会识别出您在发送垃圾邮件并发送不同的消息,但它会发送 3 次相同的消息,我希望它发送 1 条消息而忘记其余消息。我使用了您对 datetime.datetime 的建议,但经过一些研究后,我使用了 int(datetime.datetime.utcnow().timestamp()) 效果更好
【解决方案2】:

我在考虑这样的操作标志,因为你已经有一个启用的标志。只需添加另一个全局标志即可操作。

enabled = True
busy = False
@client.event
async def on_message(message):
    global enabled, busy
    if busy:
        pass #do nothing or you can show that something is spamming
    else:
        if case1:
           busy = True
           #do stuff
           busy = False
        else: 
           busy = True
           #do another thing
           busy = False

我认为上述内容只会强制执行单个操作。所以不是真正的异步,但一次只有 1 个输入。您可以允许它不中断某些操作,因此它是保护您的代码的一种简单方法。

另一种方法可能是使用队列。这种方式将需要在某些规则下将传入消息进行比较为垃圾邮件。一旦定义了垃圾邮件的规则,您就可以在运行时之前构建一个小型检查器来确定是否对命令执行操作。 https://docs.python.org/3/library/asyncio-queue.html

您可以使用 queue 将您的命令排队,然后将它们弹出并检查。使用队列的最大限制,如果队列已满,您可以获得错误返回。也许:

command_queue = Queue(maxsize = 5)#maximum number of items in the queue
@client.event
async def on_message(message):
    global command_queue
    
    if command_queue.full():
         print("We are being spammed")
         #we dont put anything more into the queue
    else:
       command_queue.put((message,datetime.datetime.now())) #you now have the message, and the time it was input. 
#You will have to determine the rules of what should be ignored or not. 
#Maybe you can even include the sender.
#example may be, any set of messages sent by the same sender within 3 seconds of the previous message should be ignored. 
        #your stuff here
       #Depending on what is available on the discord api, you can even group your commands by sender and content and insert your rules here before entering your main processing logic. 

【讨论】:

  • 我需要导入任何东西才能使用队列吗?
  • 查看我链接的python页面。那是图书馆。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 2018-10-08
  • 2020-07-18
  • 2021-03-05
  • 2021-08-19
  • 2022-07-26
相关资源
最近更新 更多