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

How do I make a work command (PyCharm 2020.3.2 Python 3.9)

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

@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'

有人可以帮我解决这个问题吗?有答案请说明问题

首先,你有一个缩进问题。 (如果是复制粘贴问题请忽略)

尝试金额[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,因此显示的金额不会与会员实际赚取的金额不同。