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 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)

确保您的类高于实际命令。这样就行了。

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

最新更新