英文:
How do i edit the original message after defering the interaction in discord.py?
问题
我正在使用 discord.py 制作一个斜杠命令,其中包含一个按钮,该按钮首先将交互延迟到思考状态(以防止交互超时),然后我想编辑原始消息(具有按钮视图的消息),但似乎无法实现。
原始命令
@group.command(name="completion", description="Text completion and code generation from a prompt")
@app_commands.describe(prompt="The text generation prompt - no default", temperature="Sampling temperature - 0.5")
async def completion(self, interaction, prompt: str, temperature: float = 0.5):
msg = await interaction.response.defer(thinking=True)
view = Regen(prompt, temperature, msg)
api_json = await completion(prompt, temperature)
embed = discord.Embed()
embed.set_author(name=f"Text Completion • {temperature}/1")
embed.description = f"**{prompt}** {api_json}"
await interaction.followup.send(embed=embed, view=view)
按钮类
@ui.button(label="Regenerate", style=discord.ButtonStyle.blurple, emoji="<:regen:1060690906547765409>")
async def regen(self, interaction: discord.Integration, button: ui.Button):
try:
await interaction.response.defer(thinking=True, ephemeral=True)
api_json = await completion(self.prompt, self.temperature)
embed = discord.Embed()
embed.set_author(name=f"Text Completion • {self.temperature}/1")
embed.description = f"**{self.prompt}** {api_json}"
await self.msg.edit(embed=embed)
except Exception as e:
print(e)
我原本期望的是编辑 msg
对象会编辑原始消息中的跟随响应,但它实际上编辑了按钮的跟随响应,从而创建了新消息。
英文:
I'm making a slash command using discord.py with a button that would first defer the interaction to thinking (to prevent interaction timeouts) then i want to edit the original message (the one with the button view) but i seem to be unable to do that
original command
@group.command(name="completion", description="Text completion and code generation from a prompt")
@app_commands.describe(prompt="The text generation prompt - no default", temperature="Sampling temperature - 0.5")
async def completion(self, interaction, prompt: str, temperature: float = 0.5):
msg = await interaction.response.defer(thinking=True)
view = Regen(prompt, temperature, msg)
api_json = await completion(prompt, temperature)
embed = discord.Embed()
embed.set_author(name=f"Text Completion · {temperature}/1")
embed.description = f"**{prompt}** {api_json}"
await interaction.followup.send(embed=embed, view=view)
button class
@ui.button(label="Regenerate", style=discord.ButtonStyle.blurple, emoji="<:regen:1060690906547765409>")
async def regen(self, interaction: discord.Integration, button: ui.Button):
try:
await interaction.response.defer(thinking=True, ephemeral=True)
api_json = await completion(self.prompt, self.temperature)
embed = discord.Embed()
embed.set_author(name=f"Text Completion · {self.temperature}/1")
embed.description = f"**{self.prompt}** {api_json}"
await self.msg.edit(embed=embed)
except Exception as e:
print(e)
What i was expecting was that editing the msg
object would edit the followup response in the original message, but it edits the followup of the button instead, creating a new message.
答案1
得分: 0
在按钮交互中,你可以使用 await interaction.original_response()
获取原始交互/消息。从中,你可以编辑消息。
@ui.button(label="Regenerate", style=discord.ButtonStyle.blurple, emoji="<:regen:1060690906547765409>")
async def regen(self, interaction: discord.Integration, button: discord.ui.Button):
try:
api_json = await completion(self.prompt, self.temperature)
embed = discord.Embed()
embed.set_author(name=f"Text Completion · {self.temperature}/1")
embed.description = f"**{self.prompt}** {api_json}"
msg = await interaction.original_response()
await msg.edit(embed=embed)
except Exception as e:
print(e)
你可以在这里了解更多关于 await interaction.original_response()
的信息:
https://discordpy.readthedocs.io/en/latest/interactions/api.html#discord.Interaction.original_response
英文:
On the Button Interaction, you can get the original interaction/message using await interaction.original_response()
. From that, you can edit the message.
@ui.button(label="Regenerate", style=discord.ButtonStyle.blurple, emoji="<:regen:1060690906547765409>")
async def regen(self, interaction: discord.Integration, button: discord.ui.Button):
try:
api_json = await completion(self.prompt, self.temperature)
embed = discord.Embed()
embed.set_author(name=f"Text Completion · {self.temperature}/1")
embed.description = f"**{self.prompt}** {api_json}"
msg = await interaction.original_response()
await msg.edit(embed=embed)
except Exception as e:
print(e)
You can read more about await interaction.original_response()
here:
https://discordpy.readthedocs.io/en/latest/interactions/api.html#discord.Interaction.original_response
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论