簡體   English   中英

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文檔
參考: 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)

我希望這是更好的閱讀,並幫助任何可能需要它的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM