TypeError:**之后的format()参数必须是映射,而不是附件

时间:2018-10-03 23:19:23

标签: python typeerror discord discord.py discord.py-rewrite

嗨,我正在尝试使用Discord机器人修复日志模块的问题。我收到错误TypeError: format() argument after ** must be a mapping, not Attachment

cogs\core.py", line 351, in on_message_delete delmessage.add_field(name='**Attachment**', value='[{filename}]({url})'.format(**attachment), inline=True) TypeError: format() argument after ** must be a mapping, not Attachment

我不确定是什么原因造成的。

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

async def on_message_delete(self, message):
    guild = message.guild
    author = message.author
    bot_member = guild.me
    time = dt.datetime.utcnow()
    now = dt.datetime.utcnow()
    if author.bot:
        return
    if not guild:
        return
    cleanmsg = message.content
    for i in message.mentions:
        cleanmsg = cleanmsg.replace(i.mention, str(i))
    name = author
    name = " ~ ".join((name.name, name.nick)) if name.nick else name.name
    changes = True
    if author.id != self.bot.user.id:
        infomessage = "A message by {} was deleted.".format(message.author.mention, message.channel.mention)
        delmessage = discord.Embed(description=infomessage, colour=discord.Color.purple(), timestamp=time)
        delmessage.add_field(name="Message:", value=cleanmsg)
        delmessage.set_footer(text="ID: {}".format(message.author.id))
        delmessage.set_author(name=name + "'s message was deleted.", icon_url=message.author.avatar_url)
        if message.attachments:
            for attachment in message.attachments:
                delmessage.add_field(name='**Attachment**', value='[{filename}]({url})'.format(**attachment), inline=True)
        await self.bot.send_log_message(guild, embed=delmessage)

2 个答案:

答案 0 :(得分:1)

一些关于OP的信息。以这种方式访问​​任何链接均无效。 Discord最近更改了缓存图像的方式。如果您在删除图像之前访问该图像,则可以获取缓存的副本;否则,如果在访问之前将其删除,则该副本将永久消失。

嵌入链接并访问它只会返回403错误。

答案 1 :(得分:0)

您不能解压缩对象以获取其属性(除非该对象是专门为此目的而设计的)。相反,您可以编写格式字符串以通过点表示法访问属性:

delmessage.add_field(name='**Attachment**', value='[{0.filename}]({0.url})'.format(attachment), inline=True)