首页 > 解决方案 > 我的我的世界机器人 discord.py 中的错误。键错误

问题描述

@bot.command()
async def profile(ctx):
    await open_account(ctx.author)
    user = ctx.author

    users = await get_shards_data()
    shards_amt = users[str(user.id)]["shards"]

    em = discord.Embed(title=f'{ctx.author.name} SHARDS',color = discord.Color.red())
    em.add_field(name="SHARDS ", value=shards_amt, inline = False)
    em.add_field(name ="TOTAL ITEMS", value = "`stone` `coal` `iron` `gold` `redstone` `emerald` `diamond`", inline = False)
    em.add_field(name = "YOUR ITEMS", value = "Type s;inventory or s;inv to view your items list")             
    await ctx.send(embed= em)

@bot.command(aliases=['inv'])
async def inventory(ctx):
    await open_account(ctx.author)
    user = ctx.author
    
    users = await get_items_data()
    
    stone_amt = users[str(user.id)]["stone"]
    coal_amt = users[str(user.id)]["coal"]
    iron_amt = users[str(user.id)]["iron"]
    gold_amt = users[str(user.id)]["gold"]
    redstone_amt = users[str(user.id)]["redstone"]
    emerald_amt = users[str(user.id)]["emerald"]
    diamond_amt = users[str(user.id)]["diamond"]
    
    em = discord.Embed(title=f'{ctx.author.name} INVENTORY',color = discord.Color.red())
    em.add_field(name="STONES", value=stone_amt, inline = False)
    em.add_field(name="COAL", value=coal_amt, inline = False)
    em.add_field(name="IRON", value=iron_amt, inline = False)
    em.add_field(name="GOLD", value=gold_amt, inline = False)
    em.add_field(name="REDSTONE", value=redstone_amt, inline = False)
    em.add_field(name="EMERALD", value= emerald_amt, inline = False)
    em.add_field(name="DIAMOND", value=diamond_amt, inline = False)

    await ctx.send(embed = em)
    

@bot.command()
async def mine(ctx):
    await open_account(ctx.author)
    user = ctx.author

    users = await get_shards_data()
    a = 9, 10, 16, 23, 11, 26
    b = "stone", "coal", "iron", "gold", "redstone", "emerald", "diamond","stone", "coal" ,"iron", "stone"
    c = random.choice(a)
    d = random.choice(b)
    e = 2, 3, 4, 5, 6, 2, 8, 9, 14, 13, 16, 18,20
    f = random.choice(e)
    em = discord.Embed(title = f'{ctx.author.name} you mined the ground')
    em.add_field(name = "SHARDS", value = f"`You got {c} shards`", inline = False)
    em.add_field(name = "ITEMS", value = f"`You got {f} {d}`")
    await ctx.send(embed =em)

    users[str(user.id)]["shards"] += c

    if d == "stone":
        users[str(user.id)]["stone"] += f

    elif d == "coal":
        users[str(user.id)]["coal"] += f

    elif d == "iron":
        users[str(user.id)]["iron"] += f

    else:
        users[str(user.id)]["gold"] += f
        
        
    with open("items.json", 'r') as f:
        json.dump(users,f)

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

我正在尝试制作一个 minecraft 机器人,在其中我在挖掘后获得随机碎片和物品,碎片正在我的碎片余额中更新,但是物品没有更新,并且在库存命令中它给出了错误。请告诉我该怎么做面临困难

Ignoring exception in command mine:
Traceback (most recent call last):
  File "C:\Users\manoj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\manoj\OneDrive\Desktop\Coding and editing\discord bot\Mezuhashi.py", line 79, in mine
    users[str(user.id)]["coal"] += f
KeyError: 'coal'

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

Traceback (most recent call last):
  File "C:\Users\manoj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\manoj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\manoj\AppData\Local\Programs\Python\Python39\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: KeyError: 'coal'

地雷命令错误

Ignoring exception in command inventory:
Traceback (most recent call last):
  File "C:\Users\manoj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\manoj\OneDrive\Desktop\Coding and editing\discord bot\Mezuhashi.py", line 36, in inventory
    stone_amt = users[str(user.id)]["stone"]
KeyError: '773913180271280129'

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

Traceback (most recent call last):
  File "C:\Users\manoj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\manoj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\manoj\AppData\Local\Programs\Python\Python39\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: KeyError: '773913180271280129'

库存命令错误

标签: pythondiscorddiscord.py

解决方案


推荐阅读