尝试指定成员时出现位置错误

时间:2018-09-26 23:48:00

标签: python python-3.x discord discord.py

嗨,我正在尝试获取一个会员名而不是作者名

我尝试了一些方法,例如for member is message.server.members:,它对服务器中的每个成员都返回了多个结果,并尝试将member: discord.Member作为产生错误的签名:

这里是我正在使用的东西:

async def on_message_delete(self, message):
    server = message.server
    author = message.author
    role =  get(server.roles, name="Powerbot")
    channel = get(message.server.channels, name="mod-log")
    time = datetime.utcnow()
    cleanmsg = message.content
    for i in message.mentions:
        cleanmsg = cleanmsg.replace(i.mention, str(i))
    fmt = '%H:%M:%S'
    name = author
    name = " ~ ".join((name.name, name.nick)) if name.nick else name.name
    if role not in author.roles:
        infomessage = "A message by {}, was deleted in {} by {}".format(message.author.mention, message.channel.mention, member,mention)
        delmessage = discord.Embed(description=infomessage, colour=discord.Color.purple(), timestamp=time)
        delmessage.add_field(name="Message:", value=cleanmsg)
        delmessage.set_footer(text="User ID: {}".format(message.author.id))
        delmessage.set_author(name=name + " message deleted.", icon_url=message.author.avatar_url)
        delmessage.set_thumbnail(url="http://i.imgur.com/fJpAFgN.png")
        try:
            await self.bot.send_message(channel, embed=delmessage)
        except:
            pass

member.mention所在的行。

infomessage = "A message by {}, was deleted in {} by {}".format(message.author.mention, message.channel.mention, member.mention)

示例输出:作者的消息已在成员的频道中删除。

如果有人可以提供帮助,将不胜感激。

2 个答案:

答案 0 :(得分:1)

由于所使用的discord.py不包含对审核日志的支持,因此无法从您使用的discord版本中查看删除邮件的人。 您将需要discord.py rewrite 才能使用以下解决方案:

@bot.event()
async def on_message_delete(msg):
    audits = await msg.guild.audit_logs(limit=10, action=discord.AuditLogAction.message_delete)
    async for audit in audits:
        try:
            await audit.target.get_message(msg.id)
        except discord.NotFound:
            continue      
        print(audit.user)
        break

这是假设即使删除邮件也保留邮件。如果上述方法不起作用,我想我们可以做的最好的事情是:

@bot.event()
async def on_message_delete(msg):
    audits = await msg.guild.audit_logs(limit=10, action=discord.AuditLogAction.message_delete)
    audit = await audits.get(extra__channel=msg.channel)
    print(audit.user)

有关更多信息,请参见:

https://discordapp.com/developers/docs/resources/audit-log

https://discordpy.readthedocs.io/en/rewrite/api.html#discord.AuditLogAction

答案 1 :(得分:0)

我非常确定不会通知客户端是谁删除了该消息,而只是得知该消息已被删除。 message对象本身不保存该信息,并且rewrite分支中的RawMessageDeleteEvent也没有该信息。

我对底层的Discord API不太熟悉,但是Message Delete网关事件似乎是discord.py在删除消息时收到的消息,并且没有提及任何有关身份的信息删除器。