【问题标题】:KeyError on Discord.PyDiscord.Py 上的 KeyError
【发布时间】:2020-09-17 11:41:04
【问题描述】:
@client.command(description="Pauses the current playing track. Can be resumed with `!resume`.", 
brief="Pauses current track.",aliases=['PAUSE'])
async def pause(ctx):
    guild_id = ctx.message.guild.id
    players[guild_id].pause()

@client.command(description="Resumes the current playing track. Can only be used if current track has 
been paused.",
    brief="Resumes current track.",
    aliases=['RESUME','continue','CONTINUE'])
async def resume(ctx):
    guild_id = ctx.message.guild.id
    players[guild_id].resume()

@client.command(description="Stops the current playing track.",
     brief="Stops current track.",
     aliases=['STOP'])
async def stop(ctx):
    guild_id = ctx.message.guild.id
    players[guild_id].stop()

当我尝试使用暂停、停止和恢复命令时,它给了我 KeyError。触发该错误的整个代码都在那里。错误是这样的:

忽略命令停止中的异常: 回溯(最近一次通话最后): 文件“C:\Users\emirs\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\ext\commands\core.py”,第 83 行,已包装 ret = 等待 coro(*args, **kwargs) 文件“C:/Users/emirs/PycharmProjects/discordmasterbot/MASTERBOT.py”,第 163 行,停止 玩家[guild_id].stop() 密钥错误:708748879079932016

还有另一种类型的错误:

Traceback(最近一次调用最后一次): 文件“C:\Users\emirs\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\ext\commands\bot.py”,第 892 行,在调用中 等待 ctx.command.invoke(ctx) 文件“C:\Users\emirs\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\ext\commands\core.py”,第 797 行,在调用中 等待注入(*ctx.args,**ctx.kwargs) 文件“C:\Users\emirs\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\ext\commands\core.py”,第 92 行,已包装 从 exc 引发 CommandInvokeError(exc) discord.ext.commands.errors.CommandInvokeError:命令引发异常:KeyError:708748879079932016

【问题讨论】:

  • 项目如何插入players

标签: python discord.py keyerror


【解决方案1】:

KeyError 表示未找到此对象的键。在这种情况下,公会 ID 708748879079932016players 中不存在。

尝试在此处添加一个 try/except 块来捕获其中任何一个。

@client.command(description="Pauses the current playing track. Can be resumed with `!resume`.", 
brief="Pauses current track.",aliases=['PAUSE'])
async def pause(ctx):
    try:
        guild_id = ctx.message.guild.id
        players[guild_id].pause()
    except KeyError:
        # do something that adds the guild ID to players #

@client.command(description="Resumes the current playing track. Can only be used if current track has 
been paused.",
    brief="Resumes current track.",
    aliases=['RESUME','continue','CONTINUE'])
async def resume(ctx):
    try:
        guild_id = ctx.message.guild.id
        players[guild_id].resume()
    except KeyError:
        # do something that adds the guild ID to players #

@client.command(description="Stops the current playing track.",
     brief="Stops current track.",
     aliases=['STOP'])
async def stop(ctx):
    try:
        guild_id = ctx.message.guild.id
        players[guild_id].stop()
    except KeyError:
        # do something that adds the guild ID to players #

【讨论】:

  • 你说的“做一些给玩家添加公会ID的事情”是什么意思?
  • 类似players[guildid] = PlayMusic(),或者任何你用来在players中创建你的第一个字典键的东西,换掉公会ID
猜你喜欢
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 2022-12-10
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
相关资源
最近更新 更多