【问题标题】:Problems with the economy system discord.py经济系统问题 discord.py
【发布时间】:2020-11-05 21:51:51
【问题描述】:

我试图确保在创建帐户时创建 2 个变量,即钱包和银行,这只会给我这个特定的错误:

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)

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    economy_system[id_author]["wallet"] = 100 尝试访问尚不存在的字典。
    如果你把economy_system[id_author] = {}放在上面一行,问题应该就解决了。
    "KeyError: '488826524791734275'" 表示在economy_system找不到作者

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 2021-05-25
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多