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