python-3.x - discord py - 消息删除性能缓慢(多台服务器)
问题描述
我有个问题。我拥有一个全球聊天室,它在 100 多台服务器上共享用户消息。所有消息都有一个一次性代码,用于在每台服务器上删除此消息。
但是我的机器人需要 2 个多小时才能删除一条消息,如果有人发送垃圾邮件侮辱,那将是非常糟糕和糟糕的消息。所以我在问如何加快我的代码速度,以便他可以非常快速地从所有服务器中删除消息。
我的代码是一个不和谐命令,它删除机器人所在的每台服务器上的特定消息(每条消息都包含一个一次性代码并保存在数据库中)。
消息如何保存在数据库中的图片: https
messageID
://i.imgur.com/UAgVBCL.png = 来自消息作者的原始消息 ID。
code
= 每条消息的一次性代码。
ids
= 所有其他消息 ID,其中包含相同的一次性代码。
time
=此时删除该行,从数据库中删除非常旧的消息,以清除事物。
我的“服务器”表如何保存在 DB 中的图片: https
guildID
://i.imgur.com/VfiIBCZ.png = 来自特定服务器的 guildid。
channelID
= 机器人发布消息的频道。
这就是我删除消息命令的代码:
@bot.command(aliases=["delmsg", "deletemessage", "msgdelete", "del"])
async def delete(ctx, code=None):
guild = bot.get_guild(616655040614236160)
member = guild.get_member(ctx.author.id)
role2 = guild.get_role(792894127972155393) # Admin
role3 = guild.get_role(792894172829974529) # Mod
if role2 in member.roles or role3 in member.roles:
mydb = mysql.connector.connect(
host="**",
user="**",
password="**",
database="global-bot"
)
mycursor = mydb.cursor()
mycursor.execute(
f"SELECT * FROM messeges WHERE code = '{code}'")
myresult3 = mycursor.fetchall()
if myresult3:
await ctx.message.delete()
await ctx.send('start deleting message..', delete_after=15)
mycursor.execute(f"SELECT * FROM servers") ##########
myresult = mycursor.fetchall()
ids = myresult3[0][2].split(" ")
for x in ids:
for server in myresult:
try:
x1 = [server]
channel = bot.get_channel(int(x1[0][1]))
msg = await channel.fetch_message(int(x))
await msg.delete()
except:
pass
for server in myresult:
try:
x1 = [server]
channel = bot.get_channel(int(x1[0][1]))
msg = await channel.fetch_message(myresult3[0][0])
await msg.delete()
except:
pass
channel2 = bot.get_channel(794269462848077845)
await ctx.send(f"message with code {code} was deleted", delete_after=15)
await channel2.send(embed=embed2)
sql = f"DELETE FROM messeges WHERE code = '{code}'" # This line here is to delete the row with the used code in the delete command, for let my bot regenerate it and use it in a different message sometime.
mycursor.execute(sql)
mydb.commit()
else:
await ctx.send(f"message with one-time code `{code}` could not be deleted! Maybe you had a typo or the code doesnt exist.", delete_after=15)
await ctx.message.delete()
else:
await ctx.send("not enough rights")
解决方案
看了你的代码一段时间后,我意识到这里的嵌套循环非常低效:
for x in ids:
for server in myresult:
try:
x1 = [server]
channel = bot.get_channel(int(x1[0][1]))
msg = await channel.fetch_message(int(x))
await msg.delete()
except:
pass
这是因为程序浪费了大量时间来尝试将消息 ID 匹配到正确的服务器。我强烈建议您不要让程序执行此操作,而是在您的机器人发布并添加到数据库时以公会 ID 的形式存储一个带有消息的外键。
这将允许您删除嵌套循环并仅按消息 id 循环,因为您已经为每条消息拥有相应的公会 id。
推荐阅读
- datepicker - katalon 记录器在运行时无法读取日期选择器
- javascript - 在导出函数中调用方法或 useState setState
- android - 如何检测图像是垂直还是水平
- linux - 使用 epoll 监听只读 sysfs 文件
- javascript - 如何用数组替换 javascript url?
- opentracing - 如何在正在运行的应用程序中启用和禁用跟踪
- android - 迁移未正确处理 Room 数据库 (java.lang.IllegalStateException)
- websocket - STM32 + ESP8266 Websocket 客户端从服务器获取 HTTP/1.1 400 Bad Request
- javascript - 如何在 Web 表单中使用 javascript 隐藏复选框和复选框文本
- c# - 在 Windows 10 中从 Windows 服务启动交互式应用程序