如何在Discord.py中通过单次channel.send使Embed显示在Files之前?
解决Discord.py中Embed在附件上方显示的问题
你当前通过await channel.send(files=files, embed=embedvar)发送消息时,附件(图片)显示在Embed上方,这是因为Discord客户端默认会将附件优先渲染在Embed之前。如果想要实现Embed在上、附件在下的效果,无法通过单次channel.send()直接实现(因为Discord API会同时传递Embed和附件数据,客户端渲染顺序由平台决定),但可以通过先发送Embed,再编辑消息添加附件的方式实现同一条消息内的顺序调整,具体代码如下:
embedvar = discord.Embed(title=f'🔗 {name}', description=text, url=link) embedvar.set_author(name=user, icon_url=img) embedvar.add_field(name="", value="", inline=True) embedvar.set_footer(text=f'Posted on: Requested by:') files = [] for filename in os.listdir(path): file_path = os.path.join(path, filename) files.append(discord.File(file_path, filename)) channel = bot.get_channel(*channel) # 先发送Embed sent_message = await channel.send(embed=embedvar) # 编辑消息添加附件,此时附件会显示在Embed下方 await sent_message.edit(files=files)
补充说明
- 这种方式本质是对同一条消息进行两次操作,但最终呈现给用户的是一条包含Embed和附件的消息,且Embed位于附件上方。
- 你了解的Discord.js中
.send().then()方案,逻辑和这个一致:先发送Embed得到消息对象,再通过消息对象添加附件。
内容的提问来源于stack exchange,提问作者J_L
相关产品推荐
相关产品推荐

