Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

discord.py - 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. 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:

Ignoring exception in command work:
Traceback (most recent call last):
  File "C:UsersMainAccountPycharmProjectsCat_Botvenvlibsite-packagesdiscordextcommandscore.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:UsersMainAccountPycharmProjectsCat_Botcogscog_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:UsersMainAccountPycharmProjectsCat_Botvenvlibsite-packagesdiscordextcommandsot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:UsersMainAccountPycharmProjectsCat_Botvenvlibsite-packagesdiscordextcommandscore.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:UsersMainAccountPycharmProjectsCat_Botvenvlibsite-packagesdiscordextcommandscore.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

question from:https://stackoverflow.com/questions/65868309/how-do-i-make-a-work-command-pycharm-2020-3-2-python-3-9

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...