首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Discord.py日志记录已删除和编辑的邮件

Discord.py日志记录已删除和编辑的邮件
EN

Stack Overflow用户
提问于 2020-07-31 07:32:45
回答 2查看 4.6K关注 0票数 2

我正在为我的服务器制作一个服务器机器人,我想记录所有的消息删除和编辑。它将登录到一个日志通道,供员工查看。在日志通道中,我想让消息显示被删除的内容、消息编辑之前的内容以及编辑消息之后的内容。我怎样才能机器人显示被删除或编辑的消息?

代码语言:javascript
运行
复制
@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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-31 21:23:05

你可以在使用的时候使用on_message_deleteon_message_edit,然后你应该给函数消息而不是ctx。

示例on_message_delete

代码语言:javascript
运行
复制
@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(channel, embed=embed)

示例on_message_edit

代码语言:javascript
运行
复制
@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(channel, embed=embed)
票数 3
EN

Stack Overflow用户

发布于 2021-06-09 05:12:39

在写这篇文章时,上面显示的方法将不起作用。这就是我编辑它的原因。

这行代码:

代码语言:javascript
运行
复制
embed=discord.Embed(title="{} edited a 
    message".format(message_before.member.name), 
    description="", color="Blue")

由于message没有member属性,因此它不起作用。它也不起作用,因为如果不进行整数转换,则无法将颜色设置为Blue或字符串。最好的方法就是像这样定义一个十六进制,输入color=0xFF0000,这样它就会变成红色。

整行内容已更改:

代码语言:javascript
运行
复制
embed = discord.Embed(title="{} deleted a message".format(message.author.name),
                      description="", color=0xFF0000)

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

代码语言:javascript
运行
复制
@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)

我将在您的代码顶部定义您想要使用的通道,如下所示。

代码语言:javascript
运行
复制
logging_channel = channelID
# then at your two commands do this:
global logging_channel
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63182846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档