discord py - 消息删除性能低下(多台服务器)
discord py - slow performance on message delete (multiple servers)
我有问题。我拥有一个全球聊天系统,可以在 100 多台服务器上共享用户消息。所有消息都有一个一次性代码,用于在每台服务器上删除此消息。
但是我的机器人需要 2 个多小时才能删除一条消息,如果有人发送侮辱性垃圾邮件,那将是非常糟糕和糟糕的。所以我在问我如何加快我的代码,这样他就可以非常快地从所有服务器上删除消息。
我的代码是一个 discord 命令,它删除机器人所在的每台服务器上的特定消息(每条消息都包含一个一次性代码并保存在数据库中)。
消息如何保存在数据库中的图片: https://i.imgur.com/UAgVBCL.png
messageID
= 来自消息作者的原始消息 ID。
code
= 每条消息的一次性代码。
ids
= 所有其他消息 ID,其中包含相同的一次性代码。
time
= 此时删除该行,从数据库中删除非常旧的消息,清理一下。
我的“服务器”-table 如何保存在数据库中的图片: https://i.imgur.com/VfiIBCZ.png
guildID
= 来自特定服务器的 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。
我有问题。我拥有一个全球聊天系统,可以在 100 多台服务器上共享用户消息。所有消息都有一个一次性代码,用于在每台服务器上删除此消息。
但是我的机器人需要 2 个多小时才能删除一条消息,如果有人发送侮辱性垃圾邮件,那将是非常糟糕和糟糕的。所以我在问我如何加快我的代码,这样他就可以非常快地从所有服务器上删除消息。
我的代码是一个 discord 命令,它删除机器人所在的每台服务器上的特定消息(每条消息都包含一个一次性代码并保存在数据库中)。
消息如何保存在数据库中的图片: https://i.imgur.com/UAgVBCL.png
messageID
= 来自消息作者的原始消息 ID。
code
= 每条消息的一次性代码。
ids
= 所有其他消息 ID,其中包含相同的一次性代码。
time
= 此时删除该行,从数据库中删除非常旧的消息,清理一下。
我的“服务器”-table 如何保存在数据库中的图片: https://i.imgur.com/VfiIBCZ.png
guildID
= 来自特定服务器的 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。