Search code examples
pythonnextcord

I am making a bot in nextcord and I need to figure out how to disable a view


I am trying to make an economy bot and one of the games is RPS. I am trying to figure out how to disable a view after a button is pressed. My code:

    import nextcord
    import random
    client=nextcord.Client(intents=nextcord.Intents.all())
    def checkwin(useroption):
        optionIndex=random.randint(1,3)
        option=""
        if optionIndex==1:
            option="Rock"
        elif optionIndex==2:
            option="Paper"
        elif optionIndex==3:
            option="Scissors"
    
    
        if option==useroption:
            return "Tie"
        elif option=="Rock" and useroption=="Paper":
            return "Loss"
        elif option=="Rock" and useroption=="Scissors":
            return "Win"
        elif option=="Paper" and useroption=="Rock":
            return "Win"
        elif option=="Paper" and useroption=="Scissors":
            return "Loss"
        elif option=="Scissors" and useroption=="Rock":
            return "Loss"
        elif option=="Scissors" and useroption=="Paper":
            return "Win"
    
    class RPSBTNS(nextcord.ui.View):
        @nextcord.ui.button(label="Rock", style=nextcord.ButtonStyle.red, emoji="🪨")
        async def rock(self, button:nextcord.ui.Button, interaction:nextcord.Interaction):
            victory=checkwin("Rock")
            if victory=="Win":
                await interaction.send("You lose.")
            elif victory=="Loss":
                await interaction.send("You Win!")
            else:
                await interaction.send("Tie.")
    
        @nextcord.ui.button(label="Paper", style=nextcord.ButtonStyle.green, emoji="📃")
        async def Paper(self, button:nextcord.ui.Button, interaction:nextcord.Interaction):
            victory=checkwin("Paper")
            if victory=="Win":
                await interaction.send("You lose.")
            elif victory=="Loss":
                await interaction.send("You Win!")
            else:
                await interaction.send("Tie.")
    
        @nextcord.ui.button(label="Scissors", style=nextcord.ButtonStyle.blurple, emoji="✂️")
        async def scissors(self, button:nextcord.ui.Button, interaction:nextcord.Interaction):
            victory=checkwin("Scissors")
            if victory=="Win":
                await interaction.send("You lose.")
            elif victory=="Loss":
                await interaction.send("You Win!")
            else:
                await interaction.send("Tie.")
    
    @client.slash_command(name="test")
    async def test(interaction:nextcord.Interaction):
        await interaction.send("Rock Paper Scissors and shoot!", view=RPSBTNS())
    
    client.run("token")


Solution

  • To remove all the buttons, you can edit the message with view=None.

    await interaction.edit(view=None)
    

    To disable all buttons, you can iterate over the children, setting disabled to True, then editing the message with the updated view.

    for child in self.children:
        child.disabled = True
    await interaction.edit(view=self)