# Buggy code
async def spotify_enqueue(
    self,
    ctx: commands.Context,
    query_type: str,
    uri: str,
    enqueue: bool,
    player: lavalink.Player,
    lock: Callable,
    notifier: Optional[Notifier] = None,
) -> List[lavalink.Track]:
    track_list = []
    has_not_allowed = False
    try:
        current_cache_level = (
            CacheLevel(await self.config.cache_level())
            if HAS_SQL
            else CacheLevel.none()
        )
        guild_data = await self.config.guild(ctx.guild).all()

        # now = int(time.time())
        enqueued_tracks = 0
        consecutive_fails = 0
        queue_dur = await queue_duration(ctx)
        queue_total_duration = lavalink.utils.format_time(queue_dur)
        before_queue_length = len(player.queue)
        tracks_from_spotify = await self._spotify_fetch_tracks(
            query_type, uri, params=None, notifier=notifier
        )
        total_tracks = len(tracks_from_spotify)
        if total_tracks < 1:
            lock(ctx, False)
            embed3 = discord.Embed(
                colour=await ctx.embed_colour(),
                title=_("This doesn't seem to be a supported Spotify URL or code."),
            )
            await notifier.update_embed(embed3)

            return track_list
        database_entries = []
        time_now = str(datetime.datetime.now(datetime.timezone.utc))

        youtube_cache = CacheLevel.set_youtube().is_subset(current_cache_level)
        spotify_cache = CacheLevel.set_spotify().is_subset(current_cache_level)
        for track_count, track in enumerate(tracks_from_spotify):
            (
                song_url,
                track_info,
                uri,
                artist_name,
                track_name,
                _id,
                _type,
            ) = self._get_spotify_track_info(track)

            database_entries.append(
                {
                    "id": _id,
                    "type": _type,
                    "uri": uri,
                    "track_name": track_name,
                    "artist_name": artist_name,
                    "song_url": song_url,
                    "track_info": track_info,
                    "last_updated": time_now,
                    "last_fetched": time_now,
                }
            )
            val = None
            if youtube_cache:
                update = True
                with contextlib.suppress(SQLError):
                    val, update = await self.fetch_one(
                        "youtube", "youtube_url", {"track": track_info}
                    )
                if update:
                    val = None
            if val is None:
                val = await self._youtube_first_time_query(
                    ctx, track_info, current_cache_level=current_cache_level
                )
            if youtube_cache and val:
                task = ("update", ("youtube", {"track": track_info}))
                self.append_task(ctx, *task)

            if val:
                try:
                    result, called_api = await self.lavalink_query(
                        ctx, player, audio_dataclasses.Query.process_input(val)
                    )
                except (RuntimeError, aiohttp.ServerDisconnectedError):
                    lock(ctx, False)
                    error_embed = discord.Embed(
                        colour=await ctx.embed_colour(),
                        title=_("The connection was reset while loading the playlist."),
                    )
                    await notifier.update_embed(error_embed)
                    break
                except asyncio.TimeoutError:
                    lock(ctx, False)
                    error_embed = discord.Embed(
                        colour=await ctx.embed_colour(),
                        title=_("Player timedout, skipping remaning tracks."),
                    )
                    await notifier.update_embed(error_embed)
                    break
                track_object = result.tracks
            else:
                track_object = []
            if (track_count % 2 == 0) or (track_count == total_tracks):
                key = "lavalink"
                seconds = "???"
                second_key = None
                # if track_count == 2:
                #     five_time = int(time.time()) - now
                # if track_count >= 2:
                # remain_tracks = total_tracks - track_count
                # time_remain = (remain_tracks / 2) * five_time
                # if track_count < total_tracks:
                #     seconds = dynamic_time(int(time_remain))
                # if track_count == total_tracks:
                #     seconds = "0s"
                # second_key = "lavalink_time"
                await notifier.notify_user(
                    current=track_count,
                    total=total_tracks,
                    key=key,
                    seconds_key=second_key,
                    seconds=seconds,
                )

            if consecutive_fails >= 10:
                error_embed = discord.Embed(
                    colour=await ctx.embed_colour(),
                    title=_("Failing to get tracks, skipping remaining."),
                )
                await notifier.update_embed(error_embed)
                break
            if not track_object:
                consecutive_fails += 1
                continue
            consecutive_fails = 0
            single_track = track_object[0]
            if not await is_allowed(
                ctx.guild,
                (
                    f"{single_track.title} {single_track.author} {single_track.uri} "
                    f"{str(audio_dataclasses.Query.process_input(single_track))}"
                ),
            ):
                has_not_allowed = True
                log.debug(f"Query is not allowed in {ctx.guild} ({ctx.guild.id})")
                continue
            track_list.append(single_track)
            if enqueue:
                if guild_data["maxlength"] > 0:
                    if track_limit(single_track, guild_data["maxlength"]):
                        enqueued_tracks += 1
                        player.add(ctx.author, single_track)
                        self.bot.dispatch(
                            "red_audio_track_enqueue",
                            player.channel.guild,
                            single_track,
                            ctx.author,
                        )
                else:
                    enqueued_tracks += 1
                    player.add(ctx.author, single_track)
                    self.bot.dispatch(
                        "red_audio_track_enqueue",
                        player.channel.guild,
                        single_track,
                        ctx.author,
                    )

                if not player.current:
                    await player.play()
        if len(track_list) == 0:
            if not has_not_allowed:
                embed3 = discord.Embed(
                    colour=await ctx.embed_colour(),
                    title=_(
                        "Nothing found.\nThe YouTube API key may be invalid "
                        "or you may be rate limited on YouTube's search service.\n"
                        "Check the YouTube API key again and follow the instructions "
                        "at `{prefix}audioset youtubeapi`."
                    ).format(prefix=ctx.prefix),
                )
                await ctx.send(embed=embed3)
        player.maybe_shuffle()
        if enqueue and tracks_from_spotify:
            if total_tracks > enqueued_tracks:
                maxlength_msg = " {bad_tracks} tracks cannot be queued.".format(
                    bad_tracks=(total_tracks - enqueued_tracks)
                )
            else:
                maxlength_msg = ""

            embed = discord.Embed(
                colour=await ctx.embed_colour(),
                title=_("Playlist Enqueued"),
                description=_("Added {num} tracks to the queue.{maxlength_msg}").format(
                    num=enqueued_tracks, maxlength_msg=maxlength_msg
                ),
            )
            if not guild_data["shuffle"] and queue_dur > 0:
                embed.set_footer(
                    text=_(
                        "{time} until start of playlist"
                        " playback: starts at #{position} in queue"
                    ).format(
                        time=queue_total_duration, position=before_queue_length + 1
                    )
                )

            await notifier.update_embed(embed)
        lock(ctx, False)

        if spotify_cache:
            task = ("insert", ("spotify", database_entries))
            self.append_task(ctx, *task)
    except Exception as e:
        lock(ctx, False)
        raise e
    finally:
        lock(ctx, False)
    return track_list


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 79, in wrapped
ret = await coro(*args, **kwargs)
File "/home/martinebots/martinebotscogs/cogs_modified/audio/audio.py", line 3183, in play
await self._enqueue_tracks(ctx, query)
File "/home/martinebots/martinebotscogs/cogs_modified/audio/audio.py", line 3885, in _enqueue_tracks
result, called_api = await self.music_cache.lavalink_query(ctx, player, query)
File "/home/martinebots/martinebotscogs/cogs_modified/audio/apis.py", line 911, in lavalink_query
results = await player.load_tracks(query)
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/lavalink/rest_api.py", line 300, in load_tracks
self.__check_node_ready()
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/lavalink/rest_api.py", line 268, in __check_node_ready
raise RuntimeError("Cannot execute REST request when node not ready.")
RuntimeError: Cannot execute REST request when node not ready.

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

Traceback (most recent call last):
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 863, in invoke
await ctx.command.invoke(ctx)
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 728, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 88, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: RuntimeError: Cannot execute REST request when node not ready.```

# Fixed code
async def spotify_enqueue(
    self,
    ctx: commands.Context,
    query_type: str,
    uri: str,
    enqueue: bool,
    player: lavalink.Player,
    lock: Callable,
    notifier: Optional[Notifier] = None,
) -> List[lavalink.Track]:
    track_list = []
    has_not_allowed = False
    try:
        current_cache_level = CacheLevel(await self.config.cache_level())
        guild_data = await self.config.guild(ctx.guild).all()

        # now = int(time.time())
        enqueued_tracks = 0
        consecutive_fails = 0
        queue_dur = await queue_duration(ctx)
        queue_total_duration = lavalink.utils.format_time(queue_dur)
        before_queue_length = len(player.queue)
        tracks_from_spotify = await self._spotify_fetch_tracks(
            query_type, uri, params=None, notifier=notifier
        )
        total_tracks = len(tracks_from_spotify)
        if total_tracks < 1:
            lock(ctx, False)
            embed3 = discord.Embed(
                colour=await ctx.embed_colour(),
                title=_("This doesn't seem to be a supported Spotify URL or code."),
            )
            await notifier.update_embed(embed3)

            return track_list
        database_entries = []
        time_now = int(datetime.datetime.now(datetime.timezone.utc).timestamp())

        youtube_cache = CacheLevel.set_youtube().is_subset(current_cache_level)
        spotify_cache = CacheLevel.set_spotify().is_subset(current_cache_level)
        for track_count, track in enumerate(tracks_from_spotify):
            (
                song_url,
                track_info,
                uri,
                artist_name,
                track_name,
                _id,
                _type,
            ) = self._get_spotify_track_info(track)

            database_entries.append(
                {
                    "id": _id,
                    "type": _type,
                    "uri": uri,
                    "track_name": track_name,
                    "artist_name": artist_name,
                    "song_url": song_url,
                    "track_info": track_info,
                    "last_updated": time_now,
                    "last_fetched": time_now,
                }
            )
            val = None
            if youtube_cache:
                update = True
                with contextlib.suppress(SQLError):
                    (val, update) = await self.database.fetch_one(
                        "youtube", "youtube_url", {"track": track_info}
                    )
                if update:
                    val = None
            if val is None:
                val = await self._youtube_first_time_query(
                    ctx, track_info, current_cache_level=current_cache_level
                )
            if youtube_cache and val:
                task = ("update", ("youtube", {"track": track_info}))
                self.append_task(ctx, *task)

            if val:
                try:
                    (result, called_api) = await self.lavalink_query(
                        ctx, player, audio_dataclasses.Query.process_input(val)
                    )
                except (RuntimeError, aiohttp.ServerDisconnectedError):
                    lock(ctx, False)
                    error_embed = discord.Embed(
                        colour=await ctx.embed_colour(),
                        title=_("The connection was reset while loading the playlist."),
                    )
                    await notifier.update_embed(error_embed)
                    break
                except asyncio.TimeoutError:
                    lock(ctx, False)
                    error_embed = discord.Embed(
                        colour=await ctx.embed_colour(),
                        title=_("Player timeout, skipping remaining tracks."),
                    )
                    await notifier.update_embed(error_embed)
                    break
                track_object = result.tracks
            else:
                track_object = []
            if (track_count % 2 == 0) or (track_count == total_tracks):
                key = "lavalink"
                seconds = "???"
                second_key = None
                await notifier.notify_user(
                    current=track_count,
                    total=total_tracks,
                    key=key,
                    seconds_key=second_key,
                    seconds=seconds,
                )

            if consecutive_fails >= 10:
                error_embed = discord.Embed(
                    colour=await ctx.embed_colour(),
                    title=_("Failing to get tracks, skipping remaining."),
                )
                await notifier.update_embed(error_embed)
                break
            if not track_object:
                consecutive_fails += 1
                continue
            consecutive_fails = 0
            single_track = track_object[0]
            if not await is_allowed(
                ctx.guild,
                (
                    f"{single_track.title} {single_track.author} {single_track.uri} "
                    f"{str(audio_dataclasses.Query.process_input(single_track))}"
                ),
            ):
                has_not_allowed = True
                log.debug(f"Query is not allowed in {ctx.guild} ({ctx.guild.id})")
                continue
            track_list.append(single_track)
            if enqueue:
                if guild_data["maxlength"] > 0:
                    if track_limit(single_track, guild_data["maxlength"]):
                        enqueued_tracks += 1
                        player.add(ctx.author, single_track)
                        self.bot.dispatch(
                            "red_audio_track_enqueue",
                            player.channel.guild,
                            single_track,
                            ctx.author,
                        )
                else:
                    enqueued_tracks += 1
                    player.add(ctx.author, single_track)
                    self.bot.dispatch(
                        "red_audio_track_enqueue",
                        player.channel.guild,
                        single_track,
                        ctx.author,
                    )

                if not player.current:
                    await player.play()
        if len(track_list) == 0:
            if not has_not_allowed:
                raise SpotifyFetchError(
                    message=_(
                        "Nothing found.\nThe YouTube API key may be invalid "
                        "or you may be rate limited on YouTube's search service.\n"
                        "Check the YouTube API key again and follow the instructions "
                        "at `{prefix}audioset youtubeapi`."
                    ).format(prefix=ctx.prefix)
                )
        player.maybe_shuffle()
        if enqueue and tracks_from_spotify:
            if total_tracks > enqueued_tracks:
                maxlength_msg = " {bad_tracks} tracks cannot be queued.".format(
                    bad_tracks=(total_tracks - enqueued_tracks)
                )
            else:
                maxlength_msg = ""

            embed = discord.Embed(
                colour=await ctx.embed_colour(),
                title=_("Playlist Enqueued"),
                description=_("Added {num} tracks to the queue.{maxlength_msg}").format(
                    num=enqueued_tracks, maxlength_msg=maxlength_msg
                ),
            )
            if not guild_data["shuffle"] and queue_dur > 0:
                embed.set_footer(
                    text=_(
                        "{time} until start of playlist"
                        " playback: starts at #{position} in queue"
                    ).format(
                        time=queue_total_duration, position=before_queue_length + 1
                    )
                )

            await notifier.update_embed(embed)
        lock(ctx, False)

        if spotify_cache:
            task = ("insert", ("spotify", database_entries))
            self.append_task(ctx, *task)
    except Exception as e:
        lock(ctx, False)
        raise e
    finally:
        lock(ctx, False)
    return track_list