python Discord.py 在单击按钮后禁用按钮

w6lpcovy  于 2023-06-28  发布在  Python
关注(0)|答案(1)|浏览(135)

我有两个按钮,我希望他们都被禁用的原始消息,它是在其中一个被点击后点击。甚至比只是禁用它们更好,我希望他们从原始邮件中删除,但这不是我需要的东西。

我的当前编码:

import discord
from discord.ext import commands
from discord.ui import Button, button, View

# Class
class MyView(View):
    def __init__(self):
        super().__init__(timeout=None)

    @button(label="Hello!", style=discord.ButtonStyle.blurple)
    async def hello(self, interaction: discord.Interaction, button: Button):
        button.disabled=True
        embedHelloButton=discord.Embed(title="Hi There!", color=0x5865F2)
        embedHelloButton.set_author(name=f"Requested by {interaction.user.name}", icon_url=interaction.user.avatar.url)
        await interaction.response.send_message(embed=embedHelloButton, ephemeral=False)
    
    @button(label="No. Bye.", style=discord.ButtonStyle.red)
    async def bye(self, interaction: discord.Interaction, button: Button):
        button.disabled=True
        embedByeButton=discord.Embed(title="Bye :(", color=0xff0000)
        embedByeButton.set_author(name=f"Requested by {interaction.user.name}", icon_url=interaction.user.avatar.url)
        await interaction.response.send_message(embed=embedByeButton, ephemeral=False)

# Command
@client.hybrid_command(name="hello", description="Say hello to the bot!")
async def hello(ctx):
    embedHello=discord.Embed(title="Say hello to me!", color=0xff0000)
    embedHello.set_author(name=f"Requested by {ctx.author.name}", icon_url=ctx.author.avatar.url)
    await ctx.send(embed=embedHello, view=MyView())

我的输出:The output of my code

第一条消息上的按钮仍然是可点击的,我想要么在第一条消息上禁用按钮,要么将它们完全删除。有什么办法吗?

e0bqpujr

e0bqpujr1#

您可以简单地对视图类进行这些更改以禁用这两个按钮

class MyView(View):
  # do what you just did
  @button(label="Hello!", style=discord.ButtonStyle.blurple)
  async def hello(self, interaction: discord.Interaction, button: Button):
    # do sth
    # example to disable both buttons by clicking 'hello' button
    button.disabled = True  # disable the 'hello' button
    bye_btn = discord.utils.get(self.children, label="No. Bye.")  # get the 'bye' button
    bye_btn.disabled = True
    # send a msg or sth else

如果您想删除所有按钮,这甚至比禁用按钮更容易

class MyView(View):
  @button(label="Hello!", style=discord.ButtonStyle.blurple)
  async def hello(self, interaction: discord.Interaction, button: Button):
    # do sth
    await interaction.response.edit_message(view=None)  # this will remove all the components in the class

我建议你稍微修改一下斜线命令

@client.hybrid_command(name="hello", description="Say hello to the bot!")
async def hello(ctx):
    # do your embed stuff
    # the good way to do this is that you could access your message (in your view class) by 'self.message'
    # because functions like 'on_timeout' does not have 'interaction' param.
    view = MyView()
    view.message = await ctx.send(embed=embedHello, view=view)

并且作为一个快速的提醒,你只能对交互进行一次响应,以避免触发交互响应异常。因此,如果您想发送消息并编辑视图,则可以切换到followup

class MyView(View):
  @button(label="Hello!", style=discord.ButtonStyle.blurple)
  async def hello(self, interaction: discord.Interaction, button: Button):
    await interaction.response.defer()
    # do sth
    await interaction.followup.send(content='...', ...)  # send the message you want
    await interaction.followup.edit_message(message_id=self.message.id, view=None)  # message_id is mandatory here

希望这能帮上忙。

相关问题