【问题标题】:discord py - slow performance on message delete (multiple servers)discord py - 消息删除性能缓慢(多台服务器)
【发布时间】: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 无法从所有服务器的快速删除它。
  • 您的变量名能否更具描述性?例如。什么是xxx
  • 编辑了问题。

标签: python-3.x discord.py mysql-python


【解决方案1】:

看了你的代码一段时间后,我意识到这里的嵌套循环效率非常低:

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。

【讨论】:

  • 看起来怎么样?我对you store a foreign key in the form of the guild's id with a message when it's posted by your bot and added to the db. 感到困惑。
  • 这就是我将消息保存到表中的方式:pastebin.com/v6LNEf1W
  • 所以我必须将 guildID 作为列添加到消息数据库表中?然后呢?
  • 然后将其合并到您的代码中。因此,与其浪费大量时间尝试将消息 id 匹配到正确的服务器,您只需遍历每条消息,使用数据库中的 guildid 从该特定公会中删除消息。这将删除嵌套循环。
猜你喜欢
  • 2021-04-15
  • 2021-04-09
  • 2020-10-26
  • 2022-01-20
  • 2015-02-01
  • 2020-08-24
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多