M
M
MARCUS272022-01-07 00:45:20
Python
MARCUS27, 2022-01-07 00:45:20

Weird KeyError, how to fix?

When I try to add a new song to the queue, I get an error

Connected
Ignoring exception in command play:
Traceback (most recent call last):
File "C:\Usersarina\Desktop\python\lib\site-packages\discord\ext\commands\cor
e.py", line 85, in wrapped
ret = await coro(args, **kwargs)
File "C:\Usersarina\Desktop\workbot\1-tester\musicAV.py", line 76, in play
queue_len = len(self.song_queue[ctx.guild.id])
KeyError: 902227097647468664

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

Traceback (most recent call last):
File "C:\Usersarina\Desktop\python\lib\site-packages\discord\ext\commands\bot
.py" , line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Usersarina\Desktop\python\lib\site-packages\discord\ext\commands\cor
e.py", line 863, in invoke
await injected(ctx.args, **ctx.kwargs)
File "C :\Usersarina\Desktop\python\lib\site-packages\discord\ext\commands\cor
e.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Key
Error: 902227097647468664
Task exception was never retrieved
future: at C:\Users\arina\Desktop\work\bot\1-tester\musicAV.py:23> exception=KeyError(90
2227097647468664)>
Traceback (most recent call last):
File "C:\Usersarina\Desktop\workbot\1-tester\musicAV.py", line 24,in check
queue
if len(self.song_queue[ctx.guild.id]) > 0:
KeyError: 902227097647468664

async def check_queue(self, ctx):
        if len(self.song_queue[ctx.guild.id]) > 0:
            await self.play_song(ctx, self.song_queue[ctx.guild.id][0])
            self.song_queue[ctx.guild.id].pop(0)


@commands.command()
    async def play(self, ctx, *, song=None):
        if song is None:
            return await ctx.send("You must include a song to play.")

        if ctx.voice_client is None:
            return await ctx.send("I must be in a voice channel to play a song.")

        # handle song where song isn't url
        if not ("youtube.com/watch?" in song or "https://youtu.be/" in song):
            await ctx.send("Searching for song, this may take a few seconds.")

            result = await self.search_song(1, song, get_url=True)

            if result is None:
                return await ctx.send("Sorry, I could not find the given song, try using my search command.")

            song = result[0]

        if ctx.voice_client.source is not None:
            queue_len = len(self.song_queue[ctx.guild.id])

            if queue_len < 10:
                self.song_queue[ctx.guild.id].append(song)
                return await ctx.send(f"I am currently playing a song, this song has been added to the queue at position: {queue_len+1}.")

            else:
                return await ctx.send("Sorry, I can only queue up to 10 songs, please wait for the current song to finish.")

        await self.play_song(ctx, song)
        await ctx.send(f"Now playing: {song}")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-01-07
@Vindicar

queue_len = len(self.song_queue[ctx.guild.id])
KeyError: 902227097647468664

The number looks like an id in discord. KeyError usually happens when you try to access a non-existent key in a dictionary. The specified line of code contains a piece that looks like a call to a dictionary: self.song_queue[ctx.guild.id]
Conclusion: the self.song_queue dictionary does not contain a key for the server (ctx.guild) from which the command came to the bot. But why it is not there, and why there is no logic that provides for this scenario - this is already a question for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question