简体   繁体   English

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

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

I created this Economy category for my bot which currently has 2 commands.我为我的机器人创建了这个经济类别,它目前有 2 个命令。 Balance and Transfer.余额和转移。 I am trying to add a work command and I came up with this:我正在尝试添加一个工作命令,我想出了这个:

@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)}")

but PyCharm came up with this error:但是 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'

Could someone help me to fix this?有人可以帮我解决这个问题吗? And please explain the problem if you have the answer如果你有答案,请解释问题

Your error comes from here:您的错误来自这里:

amount = {random.choice(x)}

You're defining amount as a set so adding it to an int causes an error.您将amount定义为一个set ,因此将其添加到int会导致错误。
What you simply have to do is removing the curly brackets:您只需要删除大括号即可:

@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}")

I've also replaced random.choice(x) to amount in your message so it won't display a different amount than the money the member really earned.我还将random.choice(x)替换为您的消息中的amount ,因此它不会显示与会员实际赚取的金额不同的金额。

First of all, you have an indentation problem.首先,你有一个缩进问题。 (Ignore if that problem was caused due to copy-pasting) (忽略该问题是否是由于复制粘贴引起的)

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

Also, x is not defined...此外,x 未定义...

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

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