Variables 如何使反垃圾邮件函数discord.py?
我的discord服务器需要反垃圾邮件功能。请帮帮我。我试过这个:Variables 如何使反垃圾邮件函数discord.py?,variables,discord,discord.py,bots,Variables,Discord,Discord.py,Bots,我的discord服务器需要反垃圾邮件功能。请帮帮我。我试过这个: import datetime import time time_window_milliseconds = 5000 max_msg_per_window = 5 author_msg_times = {} @client.event async def on_ready(): print('logged in as {0.user}'.format(client))
import datetime
import time
time_window_milliseconds = 5000
max_msg_per_window = 5
author_msg_times = {}
@client.event
async def on_ready():
print('logged in as {0.user}'.format(client))
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing,name="stack overflow"))
@client.event
async def on_message(message):
global author_msg_counts
ctx = await client.get_context(message)
author_id = ctx.author.id
# Get current epoch time in milliseconds
curr_time = datetime.datetime.now().timestamp() * 1000
# Make empty list for author id, if it does not exist
if not author_msg_times.get(author_id, False):
author_msg_times[author_id] = []
# Append the time of this message to the users list of message times
author_msg_times[author_id].append(curr_time)
# Find the beginning of our time window.
expr_time = curr_time - time_window_milliseconds
# Find message times which occurred before the start of our window
expired_msgs = [
msg_time for msg_time in author_msg_times[author_id]
if msg_time < expr_time
]
# Remove all the expired messages times from our list
for msg_time in expired_msgs:
author_msg_times[author_id].remove(msg_time)
# ^ note: we probably need to use a mutex here. Multiple threads
# might be trying to update this at the same time. Not sure though.
if len(author_msg_times[author_id]) > max_msg_per_window:
await ctx.send("Stop Spamming")
ping()
client.run(os.getenv('token'))
导入日期时间
导入时间
时间\窗口\毫秒=5000
每个窗口的最大消息数=5
作者_msg_times={}
@客户端事件
_ready()上的异步定义:
打印('以{0.user}身份登录'。格式(客户端))
等待客户端。更改状态(activity=discord.activity(type=discord.ActivityType.playing,name=“堆栈溢出”))
@客户端事件
异步def on_消息(消息):
全局作者\u消息\u计数
ctx=等待客户端。获取上下文(消息)
author\u id=ctx.author.id
#以毫秒为单位获取当前历元时间
curr_time=datetime.datetime.now().timestamp()*1000
#如果作者id不存在,则为空列表
如果不是author\u msg\u times.get(author\u id,False):
作者信息时代[作者id]=[]
#将此消息的时间附加到消息时间的用户列表中
author\u msg\u times[author\u id]。追加(当前时间)
#找到我们时间窗口的开始。
expr\u time=当前时间-时间窗口\u毫秒
#查找窗口开始之前发生的消息时间
过期的\u msgs=[
msg\u作者时间中的msg\u时间[author\u id]
如果消息时间max\u msg\u per\u窗口:
等待ctx.send(“停止垃圾邮件”)
ping()
client.run(os.getenv('token'))
当我一遍又一遍地输入相同的信息时,它似乎不起作用。你们能帮帮我吗?我需要一个好的反垃圾邮件功能,它可以在消息的
内部工作,我想你能做的最好的事情就是创建一个事件,每次用户加入时都会调用它。然后在此事件中,您可以创建一个列表,而不是保存用户id及其当前货币的变量。
users\u currency=[“用户id”、“5$”、“另一个用户id”、“7$”]
等等。接下来,我建议将其保存到文本文件中
示例代码
现在,如果有人加入,他们的id将出现在列表中,并将其货币更改为0
如何使用列表中的指定值
如果将代码保持在比users\u currency[0]
,users\u currency[2]
,[…]更高的位置。您将获得用户ID,并在users\u currency[1]
、users\u currency[3]
等网站上获得他们的货币。然后,您可以使用消息上的
事件来生成命令,该命令将显示和-他们的货币
将其保存到文本文件
您必须将其保存在文本文件()中,然后创建一个函数,该函数将在bot开始时运行,并从文件中读取所有内容,然后将其分配到列表中。
示例代码:
with open("users_currency.txt") as f:
rd=f.read()
changed_to_a_list=rd.split()
users_currency = changed_to_a_list
欢迎来到堆栈溢出!请阅读,并提供一个“为我实现此功能”或“我如何做到这一点”是本网站的主题。你必须做出诚实的尝试,然后问一个关于你的算法或技术的具体问题。如果你不知道从哪里开始,你需要查阅一本教程或与导师交谈。堆栈溢出是错误的地方,请寻求一般建议谢谢您的帮助。我要自己做,然后请帮帮我1我真的很困惑。为什么你要编辑整个问题,并将其改为另一个问题,而不是提出一个新问题?非常感谢!谢谢你也能回答这个新问题吗?
with open("users_currency.txt") as f:
rd=f.read()
changed_to_a_list=rd.split()
users_currency = changed_to_a_list