Home > front end >  How do I add a interaction to a button in pycord?
How do I add a interaction to a button in pycord?

Time:12-21

I'm getting an error constantly from some code from a couple of years ago that needed fixing. Back then, I didn't think that comments were useful, so I didn't make any. Now, I'm getting errors from that piece of code, and I need help. For context, I'm making a discord bot using python for a server for friends. Here's the code:

    def calculator(Input_List):
    try:
        global numvar1
        global numvar2
        global signal
        numvar1 = ""
        numvar2 = ""
        if " " in Input_List:
            signpos = int(Input_List.index(" "))
            for i in range(0, signpos):
                numvar1 = numvar1   Input_List[i]
            for i in range(signpos   1, len(Input_List)):
                numvar2 = numvar2   Input_List[i]
            return int(numvar1)   int(numvar2)
        elif "-" in Input_List:
            signpos = int(Input_List.index("-"))
            for i in range(0, signpos):
                numvar1 = numvar1   Input_List[i]
            for i in range(signpos   1, len(Input_List)):
                numvar2 = numvar2   Input_List[i]
            return int(numvar1) - int(numvar2)
        elif "/" in Input_List:
            signpos = int(Input_List.index("/"))
            for i in range(0, signpos):
                numvar1 = numvar1   Input_List[i]
            for i in range(signpos   1, len(Input_List)):
                numvar2 = numvar2   Input_List[i]
            return int(numvar1) / int(numvar2)
        elif "*" in Input_List:
            signpos = int(Input_List.index("*"))
            for i in range(0, signpos):
                numvar1 = numvar1   Input_List[i]
            for i in range(signpos   1, len(Input_List)):
                numvar2 = numvar2   Input_List[i]
            return int(numvar1) * int(numvar2)
        equationlist.clear()
    except Exception as e:
        print(e)
        return "No Answer, If you did a valid calculation, please contact the owner of this bot at Mintysharky#1496 and send him the dev stuff | DEV STUFF: "   str(
            e)


class CalculatorButton(Button):
    try:
        global equation
        equation = "0"

        def __init__(self, number, style=discord.ButtonStyle.gray):
            super().__init__(label=number, style=style)

        async def callback(self, interaction):
            global equation
            if not self.label == "Done":
                equationlist.append(str(self.label))
            for i in equationlist:
                equation  = str(i)
            await interaction.message.edit(content=equation)
            equation = ""
            if self.label == "Clear":
                equationlist.clear()
                await interaction.message.edit(content=equation)
            if self.label == "Done":
                print(equationlist)
                CalculatorButton.callback()
                await interaction.message.edit(content=calculator(equationlist))
    except:
        pass



@bot.command(description="Calculates something  ( ,-,/,*) One instance at a time[BETA]")
async def calculate(ctx):
  calcbuttons = []
  view = View()
  for i in range(0, 10):
    calcbuttons.append(CalculatorButton(i))
  calcbuttons.append(CalculatorButton(" ", discord.ButtonStyle.blurple))
  calcbuttons.append(CalculatorButton("-", discord.ButtonStyle.blurple))
  calcbuttons.append(CalculatorButton("/", discord.ButtonStyle.blurple))
  calcbuttons.append(CalculatorButton("*", discord.ButtonStyle.blurple))
  calcbuttons.append(CalculatorButton("Clear", discord.ButtonStyle.red))
  calcbuttons.append(CalculatorButton("Done", discord.ButtonStyle.green))
  for i in calcbuttons:
    view.add_item(i)
  await ctx.respond("0", view=view)

Token = os.environ['TOKEN']
bot.run(Token)
keep_alive()

I'm getting this error:

Ignoring exception in view <View timeout=180.0 children=16> for item <CalculatorButton style=<ButtonStyle.success: 3> url=None disabled=False label='Done' emoji=None row=None>:
Traceback (most recent call last):
  File "/home/runner/roboticraft/venv/lib/python3.8/site-packages/discord/ui/view.py", line 414, in _scheduled_task
    await item.callback(interaction)
  File "main.py", line 123, in callback
    CalculatorButton.callback()
TypeError: callback() missing 2 required positional arguments: 'self' and 'interaction'

Please help.

CodePudding user response:

It looks straightforward - you're calling a func that expects two arguments, but you're not passing any in. You define:

async def callback(self, interaction):

but then you call:

CalculatorButton.callback()

Without truly understanding your code, my quick thought is change:

CalculatorButton.callback()

to:

CalculatorButton.callback(self, interaction)

That may be enough to solve your issue.

  • Related