pythondiscorddiscord.py

TypeError: to_components() missing 1 required positional argument: 'self'


I'm trying to get a button with the label "Open Channel" and the primary color underneath my embed, but I get the error below. This doesn't happen when there's only the embed:

class NegotiationButton(discord.ui.View):

    @discord.ui.button(label="Open Channel", style=discord.ButtonStyle.primary)
    async def negotiate(self, interaction: discord.Interaction, button: discord.ui.Button):
        button.disabled=True
        button.style=discord.ButtonStyle.danger
        button.label="Already Taken"

        ntcnameverify=True

        while ntcnameverify:
            newchannelname=f"trade-{random.randint(0,99999)}"
            for channel in mainguild.text_channels:
                if newchannelname==channel.name:
                    continue
                else:
                    ntcnameverify=False

        await mainguild.create_text_channel(
            name=newchannelname,
            overwrites={ #TODO Change these IDs
                mainguild.get_member(702419006010949693): discord.PermissionOverwrite(read_messages=True, send_messages=True)

            },
            category=bot.get_channel(1000099256998297681),
            postition=500
        )

I send it with this (inside a larger function to define the embed):

await bot.get_channel(876620293249048666).send(embed=embed, view=NegotiationButton)

I get this error:

Traceback (most recent call last):
  File "C:\Users\User\anaconda3\lib\site-packages\discord\ext\commands\core.py", line 200, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\User\OneDrive\Documents\Python Projects\Other Stuff\uc_systems_bot\bot.py", line 77, in offer
    await bot.get_channel(876620293249048666).send(embed=embed, view=NegotiationButton) #TODO Change channel ID and add logic for buy and sell channels
  File "C:\Users\User\anaconda3\lib\site-packages\discord\abc.py", line 1521, in send
    with handle_message_parameters(
  File "C:\Users\User\anaconda3\lib\site-packages\discord\http.py", line 188, in handle_message_parameters
    payload['components'] = view.to_components()

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\User\anaconda3\lib\site-packages\discord\ext\commands\bot.py", line 1330, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\User\anaconda3\lib\site-packages\discord\ext\commands\core.py", line 995, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
  File "C:\Users\User\anaconda3\lib\site-packages\discord\ext\commands\core.py", line 209, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: to_components() missing 1 required positional argument: 'self'

Solution

  • You are sending the class reference but it requires an instance. Try this:

    ..., view=NegotiationButton()) # ()