NEXTCORD 上的按钮

Buttons On NEXTCORD

为什么这段代码不工作?代码的目的是让按钮永远工作,因为一段时间后,它停止工作,有人可以给我一些调皮的帮助吗? xD

@client.command()
async def teste(ctx, role : nextcord.Role):
    class buttons(nextcord.ui.View(timeout = 0)):
        def __init__(self):
            super().__init__()
            self.value = None
        @nextcord.ui.button(label = "teste", style = nextcord.ButtonStyle.blurple)
        async def teste(self, button : nextcord.ui.Button, interaction : nextcord.Interaction):
            if role in interaction.user.roles:
                await interaction.user.remove_roles(role)
            else:
                await interaction.user.add_roles(role)
    view = buttons()
    await ctx.send("teste", view = view)
    await view.wait()

您不能在命令中构建 class。

class buttons(nextcord.ui.View(timeout = 0)):

        def __init__(self):
            super().__init__()
            self.value = None

        @nextcord.ui.button(label = "teste", style = nextcord.ButtonStyle.blurple)
        async def teste(self, button : nextcord.ui.Button, interaction : nextcord.Interaction):
            if role in interaction.user.roles:
                await interaction.user.remove_roles(role)
            else:
                await interaction.user.add_roles(role)

确保 class 高于实际命令。这样就可以了。

@client.command()
async def teste(ctx, role : nextcord.Role):
    view = buttons()
    await ctx.send("teste", view=view)
    await view.wait()