单击第一个按钮后按钮不起作用

问题描述 投票:0回答:1

为了进一步解释标题,我有一个 lfg 系统,并且我有一个嵌入按钮的配置,允许 vc 所有者按照他们想要的方式更改它。第一个按钮“lock vc”工作得很好,但此后其他按钮都不起作用,并且没有错误或回调,只是说交互失败。仅当我单击第一个按钮时才会发生这种情况,如果我首先单击另一个按钮,则一切正常,直到我单击第一个按钮。

代码片段:

class VCView(ui.View):
    def __init__(self):
        super().__init__()

    @ui.button(label="Lock", style=discord.ButtonStyle.primary)
    async def lock_vc(self, interaction:discord.Interaction, button: discord.ui.Button):
        voice_channel = interaction.user.voice.channel
        canned = discord.Embed(description="Voice channel locked.",color=discord.Color.random())
        await voice_channel.set_permissions(interaction.guild.default_role, connect=False)
        await interaction.response.send_message(embed=canned)

    @ui.button(label="Unlock", style=discord.ButtonStyle.primary)
    async def unlock_vc(self, interaction:discord.Interaction, button: discord.ui.Button):
        voice_channel = interaction.user.voice.channel
        chan = discord.Embed(description="Voice channel unlocked.",color=discord.Color.random())
        await voice_channel.set_permissions(interaction.guild.default_role, connect=True)
        await interaction.response.send_message(embed=chan)

    @ui.button(label="Rename", style=discord.ButtonStyle.primary)
    async def rename_vc(self, interaction:discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_modal(RenameModal())

对于我的踢配置,还有什么比按钮更好的选择,因为按钮将有一个成员参数?

python discord.py
1个回答
0
投票

您应该通过为每个按钮创建一个类来创建按钮,以便您可以使用内置回调。只要交互处于活动状态,它们就可以被按下。我最近在一个项目中使用了类似的东西。

@bot.tree.command(name="register", description="Registration command")
async def register(interaction: discord.Interaction):
   
    await interaction.response.defer()
    view = RegistrationView(interaction) #make a class for the view
    message = await interaction.followup.send("Registration (Continued)", view=view) 
    #send the view


这是我的观点的课程:


class RegistrationView(View):
    def __init__(self, interaction: discord.Interaction):
        super().__init__()
        #have to add buttons, when you use your decorator method they do that automatically
        self.add_item(SubmitRegistrationButton(self))
        self.add_item(CancelRegistrationButton(self))

        self.interaction = interaction

带有回调的按钮:

class SubmitRegistrationButton(discord.ui.Button):
    def __init__(self, registration_view):
        super().__init__(label="Submit Registration", style=discord.ButtonStyle.green, custom_id="registrationsubmitbutton")
        self.registration_view = registration_view

#you used callbacks with the decorators, here is the second way to make a callback
    async def callback(self, interaction: discord.Interaction): 
        await self.registration_view.submit_registration_button(interaction)


class CancelRegistrationButton(discord.ui.Button):
    def __init__(self, registration_view):
        super().__init__(label="Cancel", style=discord.ButtonStyle.red, custom_id="registrationcancelbutton", row=2)
        self.registration_view = registration_view

    async def callback(self, interaction: discord.Interaction):
        await interaction.response.send_message("User Cancelled")
        self.registration_view.stop() 
#ends the interaction. until then you can press the buttons as much as you want
© www.soinside.com 2019 - 2024. All rights reserved.