discord.pyoperands

im making a economy bot like owo but im getting operand errors


i cant figure out the error it just says discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: unsupported operand type(s) for +=: 'int' and 'str'

what can i do here?

here is my code -

import discord
from discord.ext import commands
import json
import random

client = commands.Bot(command_prefix="owo ", intents=discord.Intents.all())

@client.event
async def on_ready():
    print("Logged in as {} lets rock!".format(client.user))


@client.command(aliases=['c'])
async def cash(ctx):
    await open_account(ctx.author)
    user = ctx.author

    users = await get_bank_data()

    wallet_amt = users[str(user.id)]["wallet"]

    await ctx.send(f"<:cowoncy:1048582461610799116> **|** {ctx.author.name} you currently have **__{wallet_amt}__** **cowoncy**!")
    # await ctx.send(embed= em)

@client.command(aliases=['sm'])
async def give(ctx,member : discord.Member,amount = None):
    await open_account(ctx.author)
    await open_account(member)
    if amount == None:
        await ctx.send(f"🚫 | {ctx.author.name}, Invalid arguments! :c", delete_after=5)
        return

    bal = await update_bank(ctx.author)

    amount = int(amount)

    if amount > bal[0]:
        await ctx.send(f'🚫 **|** {ctx.author.name}, you silly hooman! You don\'t have enough cowoncy!')
        return

    await update_bank(ctx.author,-1*amount,'wallet')
    await update_bank(member,amount,'wallet')
    await ctx.send(f'<:CreditCard:1048583346453757992> **|** **{ctx.author.name}** sent **{amount}')

@client.command(aliases=['d'])
async def add(ctx,member : discord.Member, amount):
    await open_account(ctx.author)
    await open_account(member)
    bal = await update_bank(member)

    await update_bank(member, amount,'wallet')
    await ctx.send(f'<:CreditCard:1048583346453757992> **|** {member} **{amount}** coins has been added to your balance!')

async def open_account(user):

    users = await get_bank_data()

    if str(user.id) in users:
        return False
    else:
        users[str(user.id)] = {}
        users[str(user.id)]["wallet"] = 0
        users[str(user.id)]["bank"] = 0

    with open('mainbank.json','w') as f:
        json.dump(users,f)

    return True


async def get_bank_data():
    with open('mainbank.json','r') as f:
        users = json.load(f)

    return users


async def update_bank(user,change=0,mode = 'wallet'):
    users = await get_bank_data()

    users[str(user.id)][mode] += change

    with open('mainbank.json','w') as f:
        json.dump(users,f)
    bal = users[str(user.id)]['wallet'],users[str(user.id)]['bank']
    return bal

client.run("TOKEN")

i was making a economy bot and i got operand error what can i do here???? any type of help will be appreciated!

also you can dm me here 𓆩†𓆪 𝐌𝐓メ ☯ɪ ᴀᴍ ɢᴏᴋᴜ☯†ᴰᶜ#0001 if you want to ask something about the code...


Solution

  • This is mainly because of your amount argument in the command callback. Since it doesn't have a type annotation (or typehint), it will always be a string.

    Lines 62 and 63

    users[str(user.id)]["wallet"] = 0
    users[str(user.id)]["bank"] = 0
    

    The values you gave them are integers, but the change argument you passed into the update_bank in lines 41, 42, and 51 is a string; you can't add a string to an integer.

    This can be fixed by adding int as the type annotation to the argument, so the library will convert the user input into an integer.

    Line 26

    async def give(ctx,member: discord.Member, amount: int = None):
    

    Line 46

    async def add(ctx, member: discord.Member, amount: int):