如何为单独的按钮进行单独的交互 | discord.py pycord
How to have separate interactions for separate buttons | discord.py pycord
如何在 pycord/discord.py.
中对不同的按钮进行不同的交互响应
代码:
@client.command()
async def test(ctx: commands.Context):
button = Button(label="Click Below", custom_id="freefire",style=discord.ButtonStyle.green, emoji="<:freefire:944183849779335198>")
button1 = Button(label="Click Below", custom_id="bgmi",style=discord.ButtonStyle.green, emoji="<:bgmi:944184219528208384>")
async def free_fire(interaction: discord.Interaction):
role = ctx.guild.get_role(944152201784336414)
member = ctx.guild.get_member(interaction.user.id)
if role in interaction.user.roles and interaction.custom_id== "freefire":
await interaction.user.remove_roles(role)
await interaction.response.send_message(f"{role} role has been taken from you", ephemeral=True)
else:
await member.add_roles(role)
await interaction.response.send_message(f"{role} role has been given to you", ephemeral=True)
button.callback = free_fire
async def bgmi(interaction: discord.Interaction):
role = ctx.guild.get_role(944152314200088667)
member = ctx.guild.get_member(interaction.user.id)
if role in interaction.user.roles and interaction.custom_id== "bgmi":
await interaction.user.remove_roles(role)
await interaction.response.send_message(f"{role} role has been taken from you", ephemeral=True)
else:
await member.add_roles(role)
await interaction.response.send_message(f"{role} role has been given to you", ephemeral=True)
button.callback = bgmi
当我单击第一个按钮时,它只是给出了最后一个按钮的角色,而其他按钮根本没有响应,控制台或任何地方都没有错误。
问题是您首先将 button.callback
设置为 free_fire
,然后您将 button.callback
设置为 bgmi
。这可以通过将第二个 button.callback = ...
更改为 button1.callback = ...
来解决
如何在 pycord/discord.py.
中对不同的按钮进行不同的交互响应代码:
@client.command()
async def test(ctx: commands.Context):
button = Button(label="Click Below", custom_id="freefire",style=discord.ButtonStyle.green, emoji="<:freefire:944183849779335198>")
button1 = Button(label="Click Below", custom_id="bgmi",style=discord.ButtonStyle.green, emoji="<:bgmi:944184219528208384>")
async def free_fire(interaction: discord.Interaction):
role = ctx.guild.get_role(944152201784336414)
member = ctx.guild.get_member(interaction.user.id)
if role in interaction.user.roles and interaction.custom_id== "freefire":
await interaction.user.remove_roles(role)
await interaction.response.send_message(f"{role} role has been taken from you", ephemeral=True)
else:
await member.add_roles(role)
await interaction.response.send_message(f"{role} role has been given to you", ephemeral=True)
button.callback = free_fire
async def bgmi(interaction: discord.Interaction):
role = ctx.guild.get_role(944152314200088667)
member = ctx.guild.get_member(interaction.user.id)
if role in interaction.user.roles and interaction.custom_id== "bgmi":
await interaction.user.remove_roles(role)
await interaction.response.send_message(f"{role} role has been taken from you", ephemeral=True)
else:
await member.add_roles(role)
await interaction.response.send_message(f"{role} role has been given to you", ephemeral=True)
button.callback = bgmi
当我单击第一个按钮时,它只是给出了最后一个按钮的角色,而其他按钮根本没有响应,控制台或任何地方都没有错误。
问题是您首先将 button.callback
设置为 free_fire
,然后您将 button.callback
设置为 bgmi
。这可以通过将第二个 button.callback = ...
更改为 button1.callback = ...