【发布时间】:2020-07-26 23:29:09
【问题描述】:
我正在发出一个命令,当有人键入 !1 时,机器人会发送一个 dm 并要求上传图片,然后机器人会嵌入该图片并将其发送到频道。
这是我目前的代码。
@commands.command(name='1')
async def qwe(self, ctx):
question = '[Upload image]'
dm = await ctx.author.create_dm()
channel = self.client.get_channel()
embed = discord.Embed(
description=question,
colour=0xD5A6BD
)
await dm.send(embed=embed)
await self.client.wait_for('message', check=ctx.author)
url = ctx.message.attachments
embed = discord.Embed(
description='image:',
colour=0xD5A6BD
)
embed.set_image(url=url.url)
await channel.send(embed=embed)
但是,当我使用上传图片回答机器人时出现此错误:
discord.ext.commands.errors.CommandInvokeError:
Command raised an exception: AttributeError: 'list' object has no attribute 'url'
【问题讨论】:
-
您可以从您获取的附件列表中获取图片网址,
url = url[0].url -
我试过了,但是 discord.ext.commands.errors.CommandInvokeError: Command 引发了异常:IndexError: list index out of range
-
这是因为您要求
ctx.message.attachments,这将是一个空列表。实际上,ctx.message表示调用命令的消息。我添加了一些更改的答案。
标签: python discord.py discord.py-rewrite