如何在 discord.py 中推迟互动后编辑原始消息?

huangapple go评论54阅读模式
英文:

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=&quot;completion&quot;, description=&quot;Text completion and code generation from a prompt&quot;)
@app_commands.describe(prompt=&quot;The text generation prompt - no default&quot;, temperature=&quot;Sampling temperature - 0.5&quot;)
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&quot;Text Completion  &#183;  {temperature}/1&quot;)
    embed.description = f&quot;**{prompt}** {api_json}&quot;
    await interaction.followup.send(embed=embed, view=view)

button class

@ui.button(label=&quot;Regenerate&quot;, style=discord.ButtonStyle.blurple, emoji=&quot;&lt;:regen:1060690906547765409&gt;&quot;)
    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&quot;Text Completion  &#183;  {self.temperature}/1&quot;)
            embed.description = f&quot;**{self.prompt}** {api_json}&quot;
            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=&quot;Regenerate&quot;, style=discord.ButtonStyle.blurple, emoji=&quot;&lt;:regen:1060690906547765409&gt;&quot;)
    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&quot;Text Completion  &#183;  {self.temperature}/1&quot;)
    		embed.description = f&quot;**{self.prompt}** {api_json}&quot;
    		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

huangapple
  • 本文由 发表于 2023年1月9日 00:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定