我正在使用disord .py与一个按钮进行斜杠命令,该按钮首先将交互推迟到思考(以防止交互超时),然后我想编辑原始消息(具有按钮视图的消息),但我似乎无法做到
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)
我所期望的是编辑msg
对象将编辑原始消息中的后续响应,但它编辑按钮的后续响应,而不是创建一个新消息。
在按钮交互中,您可以使用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