Inlowik
Inlowik

Reputation: 49

Im a noob at discord.py and idk what's wrong in my code

I keep getting an error every time I send meme bal, or meme beg. Idk whats happening and i don't know how to fix it. This is the error im getting:

Ignoring exception in command bal:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, 
in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 90, in ball
    amt_in_vault = users[str(user.id)]['bank']
KeyError: 'bank'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, 
in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 859, 
in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/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: KeyError: 'bank'

I'm new to discord.py so could someone pls help me. I tried changing the name of bank but it still doesn't work. I honestly don't know what's happening. My bot on discord isn't responding either. \

This is my code:

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

TOKEN = os.getenv('TOKEN')

client = commands.Bot(command_prefix = "meme ")

@client.command()
async def bal(ctx):
    await open_account(ctx.author)
    user = ctx.author
    users = await get_vault_data()

    wallet_amt = users[str(user.id)]['wallet']
    amt_in_vault = users[str(user.id)]['bank']

    em = discord.Embed(title = f"{ctx.author.name}'s balance", color = discord.Color.red())
    em.add_field(name = "Wallet Balance", value = wallet_amt)
    em.add_field(name = "Vault Balance", value = amt_in_vault)
    await ctx.send(embed = em)


@client.command()
async def beg(ctx):
    await open_account(ctx.author)

    users = await get_vault_data()

    user = ctx.author

    earnings = random.randrange(800)

    await ctx.send(str(random.choice(people) + "gave"  + user.author + earnings + " coins!"))

    users[str(user.id)]['wallet'] += earnings

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


async def open_account(user):
    users = await get_vault_data()

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


    with open("mainvault.json", "w") as f:
        json.dump(users, f)
    return True


async def get_vault_data():
    with open("mainvault.json", "r") as f:
        users = json.load(f)

    return users


@client.event
async def on_ready():
    print("Login Successful")
    print(client.user.name + ' is now online.')
    print('------')
    await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, 
    name='Meme - Prefix: meme'))


client.run(TOKEN)

Upvotes: 1

Views: 441

Answers (2)

Makonede
Makonede

Reputation: 450

Under each user stored in mainvault.json, there is no bank key. In other words, say you're trying to get the abc key of a JSON file containing {"123": 456, "xyz": "abc"}. This is similar to what you're trying to do, except obviously with different keys/values. You'll want to change whatever key name you use in your mainvault.json that stores the amount in a user's bank to bank, or you'll need to change amt_in_vault = users[str(user.id)]['bank'] to amt_in_vault = users[str(user.id)]['your key name for storing the amount in the bank']. My best guess would be that you store the amount in the bank under the key vault. If so, just change it to amt_in_vault = users[str(user.id)]['vault'].

Upvotes: 1

Polliog
Polliog

Reputation: 44

In the mainvault.json is missing the value bank for the user

Upvotes: 0

Related Questions