如何防止从垃圾邮件中升级为Python Discord机器人
因此,最近,我一直在使用Python的Discord机器人进行工作,该机器人在您在Discord服务器中说话时将您升级,许多流行的机器人,例如Carl Bot和Mee6,都具有这样的功能,我觉得很有趣。所以我想尝试重新创建它。该机器人效果很好,但是它确实有一些警告,我想尝试并修复,因为一个人无法检查您的水平或获得服务器中前10名人员的排行榜。但是,我想在这里找到和解决的问题是您可以垃圾邮件消息。毫无意义的消息和许多只需要一个或两个的消息。因此,我该怎么做,如何防止系统中的垃圾邮件来创建此问题。任何帮助将不胜感激!
from discord.ext import commands
import json
import discord
import os
import random
import time
client = commands.Bot(command_prefix = "!")
#On Ready Event.
@client.event
async def on_ready():
print("Bot Ready.")
time.sleep(1)
print("Logged In As: {0.user}".format(client))
#Function For Getting A Users Information And Level Them Up.
@client.event
async def on_member_join(member):
with open("users.json", "r") as f:
users = json.load(f)
await update_data(users, member)
with open("users.json", "w") as f:
json.dump(users, f)
#Function For Sending Messages.
@client.event
async def on_message(message):
with open("users.json", "r") as f:
users = json.load(f)
await update_data(users, message.author)
await add_experience(users, message.author, 5)
await level_up(users, message.author, message.channel)
with open("users.json", "w") as f:
json.dump(users, f)
async def update_data(users, user):
if not str(user.id) in users:
users[str(user.id)] = {}
users[str(user.id)]["experience"] = 0
users[str(user.id)]["level"] = 1
async def add_experience(users, user, exprience):
users[str(user.id)]["experience"] += exprience
async def level_up(users, user, channel):
experience = users[str(user.id)]["experience"]
level_start = users[str(user.id)]["level"]
level_end = int(experience ** (1/4))
if level_start < level_end:
await channel.send(f"{user.mention} Has Leveled Up! They Have Leveled Up To Level {level_end}!")
users[str(user.id)]["level"] = level_end
client.run(os.getenv("Token"))
client.run(os.environ["Token"])
So, recently I've been working on a discord bot in python that levels you up while you talk in a discord server, many popular bots like Carl Bot and MEE6 both have functions like this which I find to be quite interesting; so I wanted to try and recreate it. The bot works perfectly fine, but it does have a few caveats which I would like to try and have fixed, for one there is no way to check your level or get a leader board for the top 10 people in the server. But, the issue I want to find and resolve here is the fact that you can spam messages; messages that make no sense and many messages that only need to be one or two. So, how do I do this, how can I prevent spam in my system for creating this. Any help would be appreciated!
from discord.ext import commands
import json
import discord
import os
import random
import time
client = commands.Bot(command_prefix = "!")
#On Ready Event.
@client.event
async def on_ready():
print("Bot Ready.")
time.sleep(1)
print("Logged In As: {0.user}".format(client))
#Function For Getting A Users Information And Level Them Up.
@client.event
async def on_member_join(member):
with open("users.json", "r") as f:
users = json.load(f)
await update_data(users, member)
with open("users.json", "w") as f:
json.dump(users, f)
#Function For Sending Messages.
@client.event
async def on_message(message):
with open("users.json", "r") as f:
users = json.load(f)
await update_data(users, message.author)
await add_experience(users, message.author, 5)
await level_up(users, message.author, message.channel)
with open("users.json", "w") as f:
json.dump(users, f)
async def update_data(users, user):
if not str(user.id) in users:
users[str(user.id)] = {}
users[str(user.id)]["experience"] = 0
users[str(user.id)]["level"] = 1
async def add_experience(users, user, exprience):
users[str(user.id)]["experience"] += exprience
async def level_up(users, user, channel):
experience = users[str(user.id)]["experience"]
level_start = users[str(user.id)]["level"]
level_end = int(experience ** (1/4))
if level_start < level_end:
await channel.send(f"{user.mention} Has Leveled Up! They Have Leveled Up To Level {level_end}!")
users[str(user.id)]["level"] = level_end
client.run(os.getenv("Token"))
client.run(os.environ["Token"])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保成员不能垃圾消息和仍然获得信用的方法是恳求某种形式的正时系统。为此,您需要能够存储数据。但是,您可能不需要持续的数据存储,并且可以使用字典。
在此示例中,我从
time.time()
中存储成员的最后一条消息,并检查其上一次记录的消息时间戳是否为 从现在开始 。这只会允许会员一次获得信用纪要。此外,请注意,我仅在收到信用后才更新会员的最后一条消息。这样可以确保他们可能仍然每分钟发送一条以上的消息,并且还会因间隔一分钟的消息而获得信用。The method to ensure that members cannot spam messages and still receive credit is to implore some form of a timing system. To do this, you need to be able to store data. However, you likely won't need a persistent data storage and can use a dictionary.
In this example, I am storing a member's last message in terms of seconds from
time.time()
and checking if their last recorded message timestamp is less than 60 seconds from now. This will only allow members to receive credit for their message minutes at a time. Additionally, notice that I only update a member's last message after receiving credit. This ensures that they may still send more than a single message a minute and still receive credit for their messages spaced a minute apart.