python-3.x Discord.py 记录删除和编辑的消息

ymdaylpp  于 2022-12-05  发布在  Python
关注(0)|答案(2)|浏览(154)

我正在为我的服务器制作一个服务器机器人,我想记录所有邮件的删除和编辑。它将登录到一个日志通道,供员工查看。在日志通道中,我想让邮件显示哪些被删除或哪些在邮件编辑之前,哪些在邮件编辑之后。我如何才能让机器人显示删除或编辑的邮件?

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

0tdrvxhp1#

您可以使用on_message_delete和on_message_edit,就像您正在使用的一样,然后您应该将函数指定为message而不是ctx。
消息删除示例:

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

消息编辑示例:

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

wqsoz72f2#

上面显示的方式将不会工作的时候,使这篇文章。这就是为什么我已经编辑它,使它会。
行:

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

相关问题