为了账号安全,请及时绑定邮箱和手机立即绑定

Discord.py 记录删除和编辑的消息

Discord.py 记录删除和编辑的消息

森林海 2023-04-18 17:36:14
我正在为我的服务器制作一个服务器机器人,我想记录所有消息的删除和编辑。它将登录到一个日志通道供工作人员查看。在日志通道中,我想让消息显示被删除的内容或消息编辑之前的内容以及消息编辑之后的内容。我将如何让机器人显示已删除或已编辑的消息?@client.event()async def on_message_delete(ctx):    embed=discord.Embed(title="{} deleted a message".format(member.name), description="", color="Blue")    embed.add_field(name="What the message was goes here" ,value="", inline=True)    channel=client.get_channel(channel_id)    await channel.send(channel, embed=embed)
查看完整描述

2 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

您可以在使用时使用on_message_delete和on_message_edit,然后您应该给函数消息而不是 ctx。

示例on_message_delete:

@client.event

async def on_message_delete(message):

    embed=discord.Embed(title="{} deleted a message".format(message.member.name), 

    description="", color="Blue")

    embed.add_field(name= message.content ,value="This is the message that he has 

    deleted", 

    inline=True)

    channel=client.get_channel(channel_id)

await channel.send(embed=embed)

示例on_message_edit:

@client.event

async def on_message_edit(message_before, message_after):

    embed=discord.Embed(title="{} edited a 

    message".format(message_before.member.name), 

    description="", color="Blue")

    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(channel_id)

await channel.send(embed=embed)


查看完整回答
反对 回复 2023-04-18
?
慕的地8271018

TA贡献1796条经验 获得超4个赞

该行:


embed=discord.Embed(title="{} edited a 

    message".format(message_before.member.name), 

    description="", color="Blue")

它不工作,因为message没有属性member。它也不起作用,因为您不能将颜色设置为Blue或不进行整数转换的字符串。做到这一点的最好方法是定义一个像这样的十六进制输入,color=0xFF0000这会使它变成红色。


整行改变了:


embed = discord.Embed(title="{} deleted a message".format(message.author.name),

                      description="", color=0xFF0000)

以下是经过编辑的完整两个命令。


@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)

我会在您的代码顶部定义您想要使用的频道。


logging_channel = channelID

# then at your two commands do this:

global logging_channel


查看完整回答
反对 回复 2023-04-18
  • 2 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信