Creating Button Roles | TypeError: button_callback() missing 1 required positional argument: 'member' | Discord.py/Pycord
Creating Button Roles | TypeError: button_callback() missing 1 required positional argument: 'member' | Discord.py/Pycord
我正在尝试为我的 Discord 服务器创建按钮角色,但在这样做时遇到了错误。
代码:
@client.command()
async def test(ctx):
button= Button(label= "Click Below", style= discord.ButtonStyle.green, emoji= "<a:Tada:944187121495863327>")
async def button_callback(interaction, member: discord.Member):
role = discord.utils.get(ctx.guild.roles, id = 944152100823261205)
member.id= interaction.user.id
await member.add_role(role)
button.callback= button_callback
view= View(button)
await ctx.send("React", view=view)
终端输出:
Ignoring exception in view for item
<Button style=<ButtonStyle.success: 3> url=None disabled=False
label='Click Below' emoji= row=None>: Traceback (most recent call last):
File
"C:\Users\USER\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ui\view.py",
line 371, in _scheduled_task
await item.callback(interaction) TypeError: button_callback() missing 1 required positional argument: 'member'
过去 2 天我一直在努力解决这个问题,非常感谢您的帮助:)
说明
您收到错误是因为您尝试向回调添加参数,而当 discord 调用它时,它没有通过 member
。
按钮回调只传递一个参数:与之关联的 Interaction
。您可以通过Interaction.user
.
访问会员
代码
@client.command()
async def test(ctx: commands.Context):
button = Button(label="Click Below", style=discord.ButtonStyle.green, emoji="<a:Tada:944187121495863327>")
async def button_callback(interaction: discord.Interaction):
role = ctx.guild.get_role(944152100823261205)
member = ctx.guild.get_member(interaction.user.id)
await member.add_roles(role)
button.callback = button_callback
view = View(button)
await ctx.send("React", view=view)
参考
我正在尝试为我的 Discord 服务器创建按钮角色,但在这样做时遇到了错误。
代码:
@client.command()
async def test(ctx):
button= Button(label= "Click Below", style= discord.ButtonStyle.green, emoji= "<a:Tada:944187121495863327>")
async def button_callback(interaction, member: discord.Member):
role = discord.utils.get(ctx.guild.roles, id = 944152100823261205)
member.id= interaction.user.id
await member.add_role(role)
button.callback= button_callback
view= View(button)
await ctx.send("React", view=view)
终端输出:
Ignoring exception in view for item <Button style=<ButtonStyle.success: 3> url=None disabled=False label='Click Below' emoji= row=None>: Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ui\view.py", line 371, in _scheduled_task await item.callback(interaction) TypeError: button_callback() missing 1 required positional argument: 'member'
过去 2 天我一直在努力解决这个问题,非常感谢您的帮助:)
说明
您收到错误是因为您尝试向回调添加参数,而当 discord 调用它时,它没有通过 member
。
按钮回调只传递一个参数:与之关联的 Interaction
。您可以通过Interaction.user
.
代码
@client.command()
async def test(ctx: commands.Context):
button = Button(label="Click Below", style=discord.ButtonStyle.green, emoji="<a:Tada:944187121495863327>")
async def button_callback(interaction: discord.Interaction):
role = ctx.guild.get_role(944152100823261205)
member = ctx.guild.get_member(interaction.user.id)
await member.add_roles(role)
button.callback = button_callback
view = View(button)
await ctx.send("React", view=view)