如何使用变量设置Discord嵌入式消息的图像?

时间:2018-01-15 14:45:08

标签: python python-3.x beautifulsoup discord discord.py

所以我在变量中有我的图像网址。它需要在变量中,因为它在每次运行命令时都会更改。不幸的是,文档说set_image需要一个字符串url,并且尝试使用变量会抛出400错误。然后我尝试使用链接进行简单的send_message,但Discord不会将图像下载到聊天中,因为它不是字符串。有谁知道怎么解决这个问题?谢谢!

embed.set_image(url = exampleVariable) #throws error

2 个答案:

答案 0 :(得分:0)

如果您想要最简单的做事方式,只需发送图片网址即可。唯一的问题是它会发送网址和图片。

如果您想获得更好的结果,则必须执行以下操作:

如果您正在使用重写分支,则需要执行

imageURL = "image url"
embed = discord.Embed()
embed.set_image(url=imageURL)
await ctx.send(embed = embed)

如果您使用的是异步分支,则需要执行

imageURL = "image url"
embed = discord.Embed()
embed.set_image(url=imageURL)
await bot.send_message(ctx.message.channel, embed = embed)

要确定您拥有哪个分支,您需要执行print(discord.__version__)。如果打印1.0.0a,则您具有重写分支。如果打印0.16.2,则您具有异步分支

答案 1 :(得分:0)

如果您的机器人无法将嵌入物发送到频道,则会抛出异常(discord.Forbidden)。

将嵌入发送到频道非常简单:

# rewrite
await ctx.send(embed=embed_object)

# async
await bot.send_message(CHANNEL_ID, embed=embed_object)

否则,在设置嵌入图像时,必须传入字符串URL (请参阅async doc和rewrite)。

网址必须是字符串,并且如文档中所述"仅支持HTTPS"

您传递变量并不重要,因为变量只是对对象的引用,在本例中是一个字符串。 如果我没记错,discord将不会显示无效的图片网址。因此,您可能需要仔细检查网址。

否则,这样的代码应该有用。

(假设改写)

@bot.command()
async def image(ctx):
    return await ctx.send(embed=discord.Embed().set_image(url=ctx.author.avatar_url))

(假设异步)

@bot.command(pass_context=True)
async def image(ctx):
    em = discord.Embed().set_image(url=ctx.message.author.avatar_url)
    return await bot.send_message(ctx.message.channel.id, embed=em)