【发布时间】:2021-05-18 16:34:56
【问题描述】:
我有问题。我拥有一个全球聊天室,它在 100 多台服务器上共享用户消息。所有消息都有一个一次性代码,用于在每台服务器上删除此消息。
但我的机器人需要 2 个多小时才能删除一条消息,如果有人发送垃圾邮件侮辱,这将是非常糟糕和糟糕的。所以我在问如何加快我的代码速度,以便他可以非常快速地从所有服务器中删除消息。
我的代码是一个不和谐命令,它会删除机器人所在的每台服务器上的特定消息(每条消息都包含一个一次性代码并保存在数据库中)。
消息如何保存在数据库中的图片: https://i.imgur.com/UAgVBCL.png
messageID = 来自消息作者的原始消息 ID。
code = 每条消息的一次性代码。
ids = 所有其他消息 ID,其中包含相同的一次性代码。
time = 此时删除该行,从数据库中删除非常旧的消息,以便清理。
我的“服务器”表如何保存在 DB 中的图片: https://i.imgur.com/VfiIBCZ.png
guildID = 来自特定服务器的公会 ID。
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")
【问题讨论】:
-
是bot无法从所有服务器快速删除它的问题吗?或者它滞后并且在 2 小时后没有删除第一条消息
-
bot 无法从所有服务器的快速删除它。
-
您的变量名能否更具描述性?例如。什么是
x和xx -
编辑了问题。
标签: python-3.x discord.py mysql-python