经济体系不和谐问题

时间:2020-11-04 20:18:13

标签: discord.py

我正在尝试确保在创建帐户时创建两个仅是钱包和银行的变量,以确保产生此特定错误:

Ignoring exception in command create:
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot\venv\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:/Users/PC GIUSEPPE/PycharmProjects/LMIIBot/LMII-Bot.py", line 7042, in create
    economy_system[id_author]["wallet"] = 100
KeyError: '488826524791734275'

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

Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot\venv\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot\venv\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot\venv\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: '488826524791734275'

经过多次尝试,我无法以任何方式解决,该如何解决? 代码:

@client.event
async def on_ready():
    global economy_system
    try:
        with open('economy.json') as f:
            economy_system = json.load(f)
    except FileNotFoundError:
        print("Impossibile caricare amounts.json")
        economy_system = {}

@client.command(pass_context=True, aliases=["Create", "CREATE", "register", "Register", "REGISTER", "crea", "Crea", "CREA"])
async def create(ctx):
    id_author = str(ctx.author.id)
    embed = discord.Embed(
        color=0x003399
    )
    if id_author not in economy_system:
        economy_system[id_author]["wallet"] = 100
        economy_system[id_author]["bank"] = 0
        embed.set_author(
            name="Hai creato il tuo account!"
        )
        await ctx.send(embed=embed, delete_after=10)
        _save()
    else:
        embed.set_author(
            name="Hai già registrato un account!"
        )
        await ctx.send(embed=embed, delete_after=10)

def _save():
    with open('economy.json', 'w+') as f:
        json.dump(economy_system, f)

1 个答案:

答案 0 :(得分:1)

economy_system[id_author]["wallet"] = 100 尝试访问尚不存在的字典。
如果将economy_system[id_author] = {}放在第一行,则该问题应得到解决。
“ KeyError:'488826524791734275'”表示在economy_system

中找不到作者