我正在制作一个命令,当有人键入!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'
正如您在文档中看到的,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
方法。