简体   繁体   中英

Discord py give command error. I am making economy discord py bot, but give command doesn't work

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


os.chdir("")

there is correct path, i checked this

Bot = commands.Bot(command_prefix = "$")

token = 'my token'

which is also correct

@Bot.command()
async def give(ctx, member:discord.Member, amount = None):
    await open_account(ctx.author)
    await open_account(member)

    amount = int(amount)
    if amount == None:
        await ctx.send("Please enter the amount")
        return

    bal = await update_bank(ctx.author)

    if amount>bal[1]:
        await ctx.send("You don't have that much money")
        return

    if amount<=0:
        await ctx.send("Amount must be positive")
        return

    await update_bank(ctx.author, -1*amount, "wallet")
    await update_bank(member, 1*amount, "wallet")

    emb = discord.Embed(description = f"You gave {member.name} {amount} gold coins", color = 0x2ecc71)
    await ctx.send(embed = emb)

There is whole give command

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

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

    return True


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"]]

    return bal

As you can see, there is give command, which doesn't work. I also know, that problem isn't with open_account function, but I am not sure about update_bank.

There is an error:

if amount>bal[1]:
IndexError: list index out of range

If you have idea, please write.

You are nesting the list for the bal inside another list: [users[str(user.id)]["wallet"]] . So if users[str(user.id)]["wallet"] has the value [0, 1] , it will actually be [[0, 1]] . There is no index 1, there is only index 0 with a nested list that goes up to the index 1. You can fix this by simply removing the extra brackets: users[str(user.id)]["wallet"] .

You could have easily discovered this by print ing bal . Knowing how to debug is an incredibly useful skill.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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