我正在发出一条命令,当有人键入!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'
答案 0 :(得分:2)
您可以在文档中看到,Message.attachments
返回Attachments
的列表。然后,您需要在收到的邮件的附件列表的第一个元素上调用url
方法,而不是ctx.message
(调用命令的消息)上的方法。
@commands.command(name='1')
async def qwe(self, ctx):
question = '[Upload image]'
# Sending embed
embed = discord.Embed(description=question, colour=0xD5A6BD)
await ctx.author.send(embed=embed)
# Waiting for user input
def check(message):
return isinstance(message.channel, discord.DMChannel) and message.author == ctx.author
message = await self.client.wait_for('message', check=check)
# Sending image
embed = discord.Embed(description='image:', colour=0xD5A6BD)
attachments = message.attachments
embed.set_image(url=attachments[0].url)
await ctx.channel.send(embed=embed)
注意:您不需要调用create_dm
方法。