IDK
IDK

Reputation: 131

Creating Button Roles | TypeError: button_callback() missing 1 required positional argument: 'member' | Discord.py/Pycord

I am trying to make Button Roles for my Discord server but I hopped into an error while doing so.

Code:

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

Terminal Output:

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'

I have been trying to fix this for past 2 days your help would be appreciated :)

Upvotes: 1

Views: 1190

Answers (1)

TheFungusAmongUs
TheFungusAmongUs

Reputation: 1516

Explanation

You received the error because you tried to add a parameter to the callback and when discord called it, it did not pass member.

Button callbacks pass only one argument: the Interaction associated with it. You can access the member through Interaction.user.

Code

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

Reference

Interaction.user

Button.callback

Upvotes: 1

Related Questions