如何制作工作命令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",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'

有人可以帮我解决这个问题吗?如果你有答案,请解释问题

解决方法

您的错误来自这里:

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

,

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

尝试数量[id] += int(amount)

此外,x 未定义...