Py-Cord按钮回调函数无法编辑消息Embed的问题求助
解决Py-Cord 2.4.1中按钮回调替换Embed显示对象地址的问题
问题出在按钮回调函数的edit_message调用参数错误:你把新创建的Embed对象传给了content参数,但content仅支持文本字符串,直接传入Embed对象会输出它的内存地址字符串,而非渲染Embed。同时原Embed未被替换,是因为你没有指定embed参数来覆盖原消息里的Embed。
修正方法很简单,将await interaction.response.edit_message(content=embed, view=None)中的content=embed改为embed=embed即可。
修正后的完整代码:
from discord.ui import Button, View import discord from discord.ext import commands class TestCog(commands.Cog): def __init__(self, bot): self.bot = bot @commands.slash_command(name='test', description='') async def flip(self, ctx): async def button_callback(interaction): embed = discord.Embed( description=f"{ctx.author.mention}\nNew embed!! Which I can't see", color=0xf3c70d, ) # 修正:将content参数改为embed参数 await interaction.response.edit_message(embed=embed, view=None) button = Button(custom_id='button', label='click me', style=discord.ButtonStyle.green, emoji="🪙") button.callback = button_callback my_view = View() my_view.add_item(button) embed = discord.Embed( title="First embed. This one I can see clearly.", color=0xf3c70d, ) await ctx.respond(embed=embed, view=my_view) def setup(bot): bot.add_cog(TestCog(bot))
内容的提问来源于stack exchange,提问作者syrok
相关产品推荐
相关产品推荐

