python - 在 Discord.py bot 中记录删除消息的人

标签 python discord.py

我正在使用 on_message_delete 记录已删除的 Discord 消息像这样的事件:

@commands.Cog.listener()
    async def on_message_delete(self, message):
        if not message.author.bot:
            channel = db.field("SELECT LogID FROM guilds WHERE GuildID = ?", message.guild.id)

            if channel != 0: #If this guild wants to log deleted messages

                embed = discord.Embed(title="Message Deleted",
                                      color=discord.Color.red(),
                                      timestamp=getTime())
                embed.add_field(name="Member: ", value=message.author.mention, inline=False)
                embed.add_field(name="Deleter: ", value="What do I put here", inline=False)
                embed.set_footer(text=f"User ID: {message.author.id}")
                embed.set_thumbnail(url=message.author.avatar_url)
                fields = [("Message:", message.content, True),
                          ("Channel:", message.channel.mention, False)]

                for name, value, inline in fields:
                    embed.add_field(name=name, value=value, inline=inline)

                await self.bot.get_channel(channel).send(embed=embed)

有没有办法获取删除消息的人的 id,这样即使 mod 删除了其他人的消息,您仍然可以获得他们的 id?
(我也查看了其他来源,例如 Reddit page API 消息,但都没有给出任何见解)

最佳答案

鉴于@yotam rec 关于使用 的评论,我终于能够记录它们Discord Audit Logs .
我是这样做的:

  • 重用我的 on_message_delete听众

  • @commands.Cog.listener()
        async def on_message_delete(self, message):
            if not message.author.bot:
    
  • 创建一个 bool 变量以确保我不会两次发布相同的嵌入

  • modDeleted = False
    
  • 检查消息删除审核日志事件是否与此功能同时归档
  • async for entry in message.guild.audit_logs(limit=1, action=discord.AuditLogAction.message_delete):
        if entry.created_at.now(timezone("US/Eastern")) == getTime(): #Custom time function
            modDeleted = True
            embed = discord.Embed(title="Message Deleted By Mod")
            embed.add_field(name="Member: ", value = message.author.mention, inline=True)
            embed.add_field(name = "Mod: ", value = entry.user.mention, inline=True)
            embed.add_field(name = "Message: ", value = message.content, inline=False)
            embed.add_field(name = "Channel: ", value = message.channel.mention, inline=False)
    
            await self.bot.get_channel(channel).send(embed=embed)
    
  • 检查是否在审计日志中找不到它,如果没有,则创建另一个嵌入,知道用户删除了他们自己的消息
  • if modDeleted == False:
    
        embed = discord.Embed(title="Message Deleted")
        embed.add_field(name="Member: ", value=message.author.mention, inline=False)
        embed.add_field(name="Message: ", value=message.content, inline=True)
        embed.add_field(name="Channel: ", value=message.channel.mention, inline=False)
    
        await self.bot.get_channel(channel).send(embed=embed)
    
    希望这也能帮助其他遇到此问题的人!

    关于python - 在 Discord.py bot 中记录删除消息的人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68786644/

    相关文章:

    python - 如何使用 HTTP "Host" header (使用 GoLang)定位特定的应用程序服务器

    python - 如何对协程进行限速,限速后重新调用协程?

    python - 尝试使用 Discord.py 从 MySQL 读取时,“NoneType”对象没有属性 'mention'

    python - Discord.py - 如何从 Bot Command 中提及 Discord 用户

    python - Discord.py 重写如何DM命令?

    python - 属性错误 : 'Bot' object has no attribute 'add_roles'

    python - Sqlalchemy,关系与关系

    面向零售商/分销商的 Python 网上商店

    python - 如何在按某些条件查询后更改 pandas DataFrame 值

    python - elasticsearch 使用 python 客户端滚动