Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 🙂

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading