discord.py 2.0 中的回调未触发
Callback in discord.py 2.0 not firing
我有一个交互角色系统,但我的回调不起作用。我没有从中得到任何错误。这是我使用的代码:
class IRRoleButton(discord.ui.Button["InteractionRoles"]):
def __init__(self, role: discord.Role):
super().__init__(style=discord.ButtonStyle.primary, label=role.name, custom_id=role.id)
# role = discord.utils.get(interaction.guild.roles, id=id)
self.role = role
async def callback(self, interaction: discord.Interaction):
print("wow")
try:
await interaction.user.add_roles(self.role)
embed = discord.Embed(title="IR role added", description=f"You have been given the IR role {self.role.name}.", color=bot.success)
except Exception as e:
embed = discord.Embed(title="Something went wrong", description=f"Contact the server administrator.", color=bot.error)
print(e)
await interaction.response.send_message(embed=embed, ephemeral=True)
class InteractionRoles(discord.ui.View):
def __init__(self, interaction: discord.Interaction, irroles: list):
super().__init__()
for button in irroles:
role = discord.utils.get(interaction.guild.roles, id=button["rid"])
if role:
self.add_item(IRRoleButton(role))
@apptree.command(description="Opens a public IR panel.", guild=discord.Object(id=956522017983725588))
@app_commands.describe(group="The ID of the group to open.")
async def iropen(interaction: discord.Interaction, group: int):
await interaction.response.defer()
group = await bot.db.fetchrow("SELECT * FROM irgroups WHERE grid = ", group)
if not group:
embed = discord.Embed(title="IR group not found", description=f"An IR group with the ID `{group}` does not exist.", color=bot.error)
return await interaction.followup.send(embed=embed)
irroles = await bot.db.fetch("SELECT * FROM irroles WHERE grid = ", group["grid"])
await interaction.channel.send(embed=discord.Embed(title=str(group['gname']), description="Press a button to get the corresponding role.", color=bot.accent), view=InteractionRoles(interaction, irroles))
await interaction.followup.send(embed=discord.Embed(title="IR panel opened", description=f"The IR panel for {group['gname']} has been opened.", color=bot.success))
我知道回调不起作用,因为根本没有打印“wow”。
我正在使用 discord.py 2.0 版
预计:
赋予用户的角色
结果:
什么都没发生
显然我用一个 int 传递了 custom_id,但它需要一个 str。
我有一个交互角色系统,但我的回调不起作用。我没有从中得到任何错误。这是我使用的代码:
class IRRoleButton(discord.ui.Button["InteractionRoles"]):
def __init__(self, role: discord.Role):
super().__init__(style=discord.ButtonStyle.primary, label=role.name, custom_id=role.id)
# role = discord.utils.get(interaction.guild.roles, id=id)
self.role = role
async def callback(self, interaction: discord.Interaction):
print("wow")
try:
await interaction.user.add_roles(self.role)
embed = discord.Embed(title="IR role added", description=f"You have been given the IR role {self.role.name}.", color=bot.success)
except Exception as e:
embed = discord.Embed(title="Something went wrong", description=f"Contact the server administrator.", color=bot.error)
print(e)
await interaction.response.send_message(embed=embed, ephemeral=True)
class InteractionRoles(discord.ui.View):
def __init__(self, interaction: discord.Interaction, irroles: list):
super().__init__()
for button in irroles:
role = discord.utils.get(interaction.guild.roles, id=button["rid"])
if role:
self.add_item(IRRoleButton(role))
@apptree.command(description="Opens a public IR panel.", guild=discord.Object(id=956522017983725588))
@app_commands.describe(group="The ID of the group to open.")
async def iropen(interaction: discord.Interaction, group: int):
await interaction.response.defer()
group = await bot.db.fetchrow("SELECT * FROM irgroups WHERE grid = ", group)
if not group:
embed = discord.Embed(title="IR group not found", description=f"An IR group with the ID `{group}` does not exist.", color=bot.error)
return await interaction.followup.send(embed=embed)
irroles = await bot.db.fetch("SELECT * FROM irroles WHERE grid = ", group["grid"])
await interaction.channel.send(embed=discord.Embed(title=str(group['gname']), description="Press a button to get the corresponding role.", color=bot.accent), view=InteractionRoles(interaction, irroles))
await interaction.followup.send(embed=discord.Embed(title="IR panel opened", description=f"The IR panel for {group['gname']} has been opened.", color=bot.success))
我知道回调不起作用,因为根本没有打印“wow”。
我正在使用 discord.py 2.0 版
预计: 赋予用户的角色
结果: 什么都没发生
显然我用一个 int 传递了 custom_id,但它需要一个 str。