Search code examples
pythonpycord

I gave 1 argument to the function, but it says that I gave it 2 in pycord


I'm trying to make a calculator that recieves button presses in discord as input and then edits the message to reflect what the user pressed. I didn't put in more than 1 input into await interaction.response.edit_message(equation), but it says I did... here's the code and at the bottom is the error. [EDIT] I put in all of the code now

import os
import discord
import random
import math as rmath
import cmath
from discord.ui import Button,View
from threading import Thread
from keep_alive import keep_alive
global calcList
global equationlist


TOKEN = os.environ['TOKEN']
var12 = 0
calcList = []
equationlist=[]
equation = 0
bot = discord.Bot(debug_guilds=[875871768622014504,773981569861419008])
max_chance_to_hail = 10
amt_msg_to_hail = random.randint(1, max_chance_to_hail - 1)
never_counter = 1


class CalculatorButton(Button):
  global equation
  def __init__(self, number, style=discord.ButtonStyle.gray):
    super().__init__(label=number, style=style)
  async def callback(self, interaction):
    global equation
    equationlist.append(self.label)
    for thingy in equationlist:
      equation += thingy
    print(equation)
    await interaction.response.edit_message(equation)
    equation = 0
    
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)
  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)


@bot.command(description="Calculates something  (+,-,/,*) One instance at a time[BETA]")
async def calculate(ctx):
  equation = 0
  calcbuttons = []
  view = View()
  for i in range(0,10):
    calcbuttons.append(CalculatorButton(i))
  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)
  await ctx.respond(calculator(calcList))


@bot.command(description="Sings Rick Asley's Never gonna give you up with you")
async def never(ctx):
  global never_counter
  if never_counter == 1:
    await ctx.respond("gonna give you up")
    never_counter += 1
  elif never_counter == 2:
    await ctx.respond("gonna let you down")
    never_counter = 1



@bot.command(description="Hello!")
async def hello(ctx):
  await ctx.respond("Hello!")


@bot.event
async def on_ready():
    if bot.user == None:
        print("something is wrong")
    else:
        print("Logged in to {}".format(bot.user))



@bot.event
async def on_message(message):       
#HAIL ME!
      if str(message.author.id) == "725875148938412104" or "515382967565287445" and not "983380913671008326":
  
          global var12
          global amt_msg_to_hail
          if var12 >= amt_msg_to_hail:
              var12 = 0
              amt_msg_to_hail = random.randint(1, max_chance_to_hail - 1)
              await message.channel.send("Hello Owner!")
          else:
              var12 += 1

keep_alive()
bot.run(TOKEN)
Ignoring exception in view <View timeout=180.0 children=12> for item <CalculatorButton style=<ButtonStyle.secondary: 2> url=None disabled=False label=9 emoji=None row=None>:
Traceback (most recent call last):
  File "/home/runner/Roboticraft/venv/lib/python3.8/site-packages/discord/ui/view.py", line 375, in _scheduled_task
    await item.callback(interaction)
  File "main.py", line 36, in callback
    await interaction.response.edit_message(equation)
TypeError: edit_message() takes 1 positional argument but 2 were given

Solution

  • You have to pass the content as a keyword argument. (See here)

    So instead of

    await interaction.response.edit_message(equation)
    

    You need

    await interaction.response.edit_message(content=equation)