我目前有一个机器人,如果满足了某些条件,就会对人进行操作。问题是,如果内容被重复,机器人将发送垃圾邮件。例如,用户JohnSmith#1234说:“如果有人在聊天中说‘苹果’或‘苹果’的话,就会这样做。”然而,假设有4条信息是连续的:
User1:“嘿,你喜欢苹果吗?”
User2:“对,我喜欢苹果”
User1:“你最喜欢哪种苹果?”
User2:“我没有真正喜欢的苹果,但我喜欢史密斯奶奶
然后机器人会说:
@JohnSmith#1234代表“苹果”
@JohnSmith#1234代表“苹果”
@JohnSmith#1234代表“苹果”
@JohnSmith#1234代表“苹果”
因为“苹果”被提到过4次。有没有办法告诉机器人:“如果你在最后10秒内按了一下JohnSmith#1234,就别再打他了?”我基本上想要一个动作不执行,如果它是最近做的。
编辑:
虽然所显示的答案似乎只适用于一个用户,但似乎不适用于多个用户。答案将适合JohnSmith#1234,但从现在起,它将使机器人无用,直到JohnSmith#1234的延迟完成。
我不知道如何告诉机器人“继续做你正在做的事情,但是在一边运行延迟循环。”下面是机器人目前正在做的事情,继续我前面提供的示例:
1:假设是@JohnSmith#1234 for 'apple'
他说:把约翰·史密斯放到一本等待的字典里,直到10秒结束。(注:机器人除了等待什么都不做。这是不好的,因为它应该平用户除了约翰史密斯)。
他说:如果那10秒到了,它会再次击打约翰·史密斯。否则,它将继续为其他用户做一些事情。
发布于 2020-08-19 19:46:27
我想出了一个和我的机器人一起工作的方法。它依赖于线程模块。以下是基本代码:
import threading
global Username
global Username_ID
global Username_mention
global Username_ping
Username= []
Username_ID = XXXXXXXXXXXXXXXXXX #each X is a number. Used to actually ping people, since Discord Nitro lets people customize their mentions
Username_mention = 'Username#XXXX'
Username_ping = True
def Username_timer_func():
global Username_ping
Username_ping = True
#note: the following step is optional. Useful for bug checking
print("Username can be pinged again")
Username_timer = threading.Timer(30, Username_timer_func)
global Username_ping
if bool(in_Username) == True and bool(Username_ping) == True and str(message.author) != Username_mention:
await channel.send("<@"+ str(Username_ID)+">" + " for "+ str(set(<list of words in the message, e.g. apple>) & set(Username))[1:][:-1])
Username_ping = False
Username_timer.start()主要的问题是很多都是硬编码的。然而,服务器上的许多人想要特定的偏好(例如ping后的秒数、他们想要的是什么、他们的ID、他们的名字等等)。这要么是无法自动化的,要么是如果机器人离线的话,如何在不丢失所有数据的情况下实现自动化。
基本的逻辑是"ping这个用户。一旦你对他们进行平平,就可以再次将用户的ping设置为“False”。30秒后,运行一个函数,再次将该功能设置为"True“。由于线程计时器与代码的其余部分分开运行(例如,机器人在执行其他任务时不需要继续检查),所以我不必担心不必要的停止。
发布于 2020-08-18 21:42:34
创建一个冷却字典,在这里你可以存储机器人已经点击过的人和冷却结束。例:cooldows = {"JohnSmith#1234": 1597786825}.
然后,当你的机器人试图平某人,只需检查他们是否在字典中。如果他们检查时代时间戳,如果现在的时间大于所列出的时间,请将用户从字典中删除,并对其进行ping,如果它不对其进行平分。如果它们不在字典中,机器人就可以对它们进行平分。
发布于 2020-08-18 22:27:09
当用户输入'apple‘时,您可以制作一本字典来跟踪人们何时发布消息,您可以将他们的名字添加到字典中,后面跟着当前的日期和时间。
可以使用以下代码获取当前日期和时间:
import datetime
dateTime = datetime.datetime.now()你的字典会是这样的:
userCooldowns = {}然后,可以使用以下代码向其添加用户:
dateTime = datetime.datetime.now()
userCooldowns["JohnSmith#1234"] = dateTime一旦您有了所有可以运行的检查,查看消息是否在时间戳之后的10秒内发送,如果是,则不要做任何事情,否则请发送以下内容:
import datetime
import time
dateTime = datetime.datetime.now() # Gets the current date and time
seconds = datetime.timedelta(seconds=10) # Gets the 10 seconds needed
newTime = dateTime + seconds # Gets the date and time 10 seconds ahead of current time
while True:
dateTime = datetime.datetime.now() # Gets the current date and time again
if dateTime < newTime: # Checks if the current time is less than the time 10 seconds ahead
# Do something if time is within 10 seconds
else:
# Do something when 10 seconds are over
time.sleep(1)https://stackoverflow.com/questions/63476864
复制相似问题