variables - 如何制作反垃圾邮件功能discord.py?
问题描述
我的不和谐服务器上需要反垃圾邮件功能。请帮我。我试过这个:
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'))
当我一遍又一遍地键入相同的消息时,它似乎不起作用。你们能帮帮我吗?我需要可以在内部工作的良好反垃圾邮件功能on_message
解决方案
我认为你能做的最好的事情是制作一个事件on_member_join,每次用户加入时都会调用它。然后在这种情况下,您可以创建一个列表,而不是保存用户 ID 及其当前货币的变量。
users_currency = ["user's id", "5$", "another user's id", "7$"]
等等。接下来,我建议将其保存到文本文件中。
示例代码
global users_currency
users_currrency = []
@client.event
global users_currency
async def on_member_join(member): #on_member_join event
user = str(member.id) #gets user's id and changes it to string
users_currency.append(user) #adds user's id to your list
users_currency.append("0") #sets the currency to 0
现在,如果有人加入,他们的 id 将出现在列表中并将他们的货币更改为 0。
如何在列表中使用分配的值
如果您将代码保持在更高的示例上,则使用users_currrency[0]
, users_currrency[2]
, [...]。您将获得用户的 id 和 onusers_currrency[1]
等users_currrency[3]
。您将获得他们的货币。然后您可以使用on_message
事件或@client.command
创建将在列表中查找用户 ID并更改下一个值- 他们的货币的命令。
将其保存到文本文件
您必须将其保存在文本文件中(使用 Python 将列表写入文件),然后创建一个将在机器人启动时运行的函数,并从文件中读取所有内容并将其分配到您的列表中。
示例代码:
with open("users_currency.txt") as f:
rd=f.read()
changed_to_a_list=rd.split()
users_currency = changed_to_a_list
推荐阅读
- reactjs - 如何在 GitHub 页面上部署我的 React 应用程序,使我的部署与“someName.github.io”完全一样
- r - 在 R 中使用 lmap 将函数应用到列表时出错
- reactjs - React 模块的 Typescript 模块增强
- github - 需要仔细检查我是否理解 Github 的工作原理
- javascript - JSON 中位置 0 处的意外标记
- flutter - 如何使用另一个类的 TextEditingController()
- django - Django 管理:从自定义用户模型中删除记录
- java - 当 Feature 跨越 Step 类时,使用 spring 依赖注入的 Java-Cucumber 测试会抛出 NullPointerException
- c# - 如何在 WPF C# 中的父控件顶部设置控件位置?
- react-native - 方法 [RNCPushNotificationIOS addNotificationRequest] 中的未知参数类型“UNNotificationRequest”