Discord.py 重写,审核日志,如何让它提及消息所在的频道?

Discord.py Rewrite, Audit Log, How do I make it mention the channel the message was in?

我一直在研究 discord.py 机器人的审计日志。我不知道如何提及编辑消息的频道。示例:用户在#general 中编辑消息,如何让我的机器人获取用户编辑的消息的频道 ID。 我希望嵌入参考谁编辑了消息,之前和之后,以及消息是在哪个频道中编辑的。

这是我目前拥有的:

class Log(Cog):

def __init__(self, bot):
    self.bot = bot

@commands.Cog.listener()
async def on_ready(self):
    print("Bot is online.")

@Cog.listener()
async def on_message_edit(self, before, after):
    if not after.author.bot:
        if before.content != after.content:
            embed = Embed(title="Message Edited",
                          description=f"Edit by <#{after.author.display_name}>",
                          color=after.author.color,
                          timestamp=datetime.utcnow())

            fields = [("Before", before.content, False),
                      ("After", after.content, False),
                      ("Channel", "<#{CHANNEL ID}>", False)]  # This is the part I'm stuck on

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

            logchannel = self.bot.get_channel(798623500641894461)  # logs channel

            await logchannel.send(embed=embed)

def setup(bot):
    bot.add_cog(Log(bot))

由于 beforeafterdiscord.Message 对象,您可以通过键入 before.channelafter.channel 来访问频道属性。这是一个简短的例子:

@Cog.listener()
async def on_message_edit(self, before, after):
    channel = before.channel
    logchannel = self.bot.get_channel(798623500641894461)

    await logchannel.send(f'Message edited in {channel.mention}'

这类问题可以看discord.py documentation.
参考:discord.on_message_edit(before, after)

我最近在这里回答了某人遇到的问题,我说的是从 2021 年 8 月 6 日开始工作。

根据提供的建议进行了更新。

这是之前链接的文章中概述的工作代码。

    @client.event
async def on_message_delete(message):
    embed = discord.Embed(title="{} deleted a message".format(message.author.name),
                          description="", color=0xFF0000)
    embed.add_field(name=message.content, value="This is the message that he has deleted",
                    inline=True)
    channel = client.get_channel(channelid)
    await channel.send(channel, embed=embed)


@client.event
async def on_message_edit(message_before, message_after):
    embed = discord.Embed(title="{} edited a message".format(message_before.author.name),
                          description="", color=0xFF0000)
    embed.add_field(name=message_before.content, value="This is the message before any edit",
                    inline=True)
    embed.add_field(name=message_after.content, value="This is the message after the edit",
                    inline=True)
    channel = client.get_channel(channelid)
    await channel.send(channel, embed=embed)

我希望这是更好的阅读,并能帮助到任何可能需要它的人。