如何生成工作命令(PyCharm 2020.3.2 Python 3.9)

2024-10-03 17:19:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我为我的机器人创建了这个经济类,它目前有两个命令。平衡和转移。我正在尝试添加一个工作命令,我想出了以下方法:

@commands.command()
    async def work(self, ctx):
        id = str(ctx.message.author.id)
        amount = {random.choice(x)}
        amounts[id] += amount
        await ctx.send(f"You worked at a dumpster and earned {random.choice(x)}")

但是PyCharm犯了这个错误:

Ignoring exception in command work:
Traceback (most recent call last):
  File "C:\Users\MainAccount\PycharmProjects\Cat_Bot\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\MainAccount\PycharmProjects\Cat_Bot\cogs\cog_economy.py", line 85, in work
    amounts[id] += amount
TypeError: unsupported operand type(s) for +=: 'int' and 'set'

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

Traceback (most recent call last):
  File "C:\Users\MainAccount\PycharmProjects\Cat_Bot\venv\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\MainAccount\PycharmProjects\Cat_Bot\venv\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\MainAccount\PycharmProjects\Cat_Bot\venv\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: unsupported operand type(s) for +=: 'int' and 'set'

有人能帮我修一下吗?如果你有答案,请解释这个问题


Tags: inpyidbotlineawaitusersext
2条回答

首先,您有一个缩进问题。(如果该问题是由于复制粘贴引起的,则忽略此问题)

try amounts[id]+=int(金额)

另外,x没有定义

您的错误来自这里:

amount = {random.choice(x)}

您将amount定义为set,因此将其添加到int会导致错误。
您只需删除花括号即可:

@commands.command()
async def work(self, ctx):
    id = str(ctx.message.author.id)
    amount = random.choice(x)
    amounts[id] += amount
    await ctx.send(f"You worked at a dumpster and earned {amount}")

我还将您的消息中的random.choice(x)替换为amount,因此它显示的金额不会与该成员实际赚取的金额不同

相关问题 更多 >