0
votes
@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()

When i try to use the pause, stop and resume command it gives me the KeyError. The whole code that triggers that error is up there. And the error lokks like this:

Ignoring exception in command stop: Traceback (most recent call last): File "C:\Users\emirs\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped ret = await coro(*args, **kwargs) File "C:/Users/emirs/PycharmProjects/discordmasterbot/MASTERBOT.py", line 163, in stop players[guild_id].stop() KeyError: 708748879079932016

And there is an another type of that error:

Traceback (most recent call last): File "C:\Users\emirs\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke await ctx.command.invoke(ctx) File "C:\Users\emirs\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke await injected(*ctx.args, **ctx.kwargs) File "C:\Users\emirs\PycharmProjects\discordmasterbot\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: 708748879079932016

1
How do items get inserted into players?Patrick Haugh

1 Answers

1
votes

A KeyError means that a key for this object was not found. In this case the guild ID 708748879079932016 does not exist in players.

Try adding a try/except block here to catch any of this.

@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 #