简体   繁体   English

如何在 pycord 中为按钮添加交互?

[英]How do I add a interaction to a button in pycord?

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.对于上下文,我正在使用 python 为朋友的服务器制作一个不和谐的机器人。 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.请帮忙。

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.这可能足以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM