当用户回复机器人的问题并让机器人嵌入时,是否可以获取图像附件
Is it possible to get the image attachment when a user reply to the bot's question and make the bot embed it
我正在发出一个命令,当有人输入 !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
returns a list of Attachments
. You then need to call the 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
方法。
我正在发出一个命令,当有人输入 !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
returns a list of Attachments
. You then need to call the 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
方法。