# Buggy code
async def autoplay(self, player: lavalink.Player):
    autoplaylist = await self.config.guild(player.channel.guild).autoplaylist()
    current_cache_level = (
        CacheLevel(await self.config.cache_level()) if HAS_SQL else CacheLevel.none()
    )
    cache_enabled = CacheLevel.set_lavalink().is_subset(current_cache_level)
    playlist = None
    tracks = None
    if autoplaylist["enabled"]:
        with contextlib.suppress(Exception):
            playlist = await get_playlist(
                autoplaylist["id"],
                autoplaylist["scope"],
                self.bot,
                player.channel.guild,
                player.channel.guild.me,
            )
            tracks = playlist.tracks_obj

    if not tracks or not getattr(playlist, "tracks", None):
        if cache_enabled:
            tracks = await self.play_random()
        if not tracks:
            ctx = namedtuple("Context", "message")
            results, called_api = await self.lavalink_query(
                ctx(player.channel.guild),
                player,
                audio_dataclasses.Query.process_input(_TOP_100_US),
            )
            tracks = list(results.tracks)
    if tracks:
        multiple = len(tracks) > 1
        track = tracks[0]

        valid = not multiple
        tries = len(tracks)
        while valid is False and multiple:
            tries -= 1
            if tries <= 0:
                raise DatabaseError("No valid entry found")
            track = random.choice(tracks)
            query = audio_dataclasses.Query.process_input(track)
            await asyncio.sleep(0.001)
            if not query.valid:
                continue
            if query.is_local and not query.track.exists():
                continue
            if not await is_allowed(
                player.channel.guild,
                (
                    f"{track.title} {track.author} {track.uri} "
                    f"{str(audio_dataclasses.Query.process_input(track))}"
                ),
            ):
                log.debug(
                    "Query is not allowed in "
                    f"{player.channel.guild} ({player.channel.guild.id})"
                )
                continue
            valid = True

        track.extras = {"autoplay": True}
        player.add(player.channel.guild.me, track)
        self.bot.dispatch(
            "red_audio_track_auto_play",
            player.channel.guild,
            track,
            player.channel.guild.me,
        )
        if not player.current:
            await player.play()


# 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 autoplay(self, player: lavalink.Player):
    autoplaylist = await self.config.guild(player.channel.guild).autoplaylist()
    current_cache_level = CacheLevel(await self.config.cache_level())
    cache_enabled = CacheLevel.set_lavalink().is_subset(current_cache_level)
    playlist = None
    tracks = None
    if autoplaylist["enabled"]:
        with contextlib.suppress(Exception):
            playlist = await get_playlist(
                autoplaylist["id"],
                autoplaylist["scope"],
                self.bot,
                player.channel.guild,
                player.channel.guild.me,
            )
            tracks = playlist.tracks_obj

    if not tracks or not getattr(playlist, "tracks", None):
        if cache_enabled:
            tracks = await self.get_random_from_db()
        if not tracks:
            ctx = namedtuple("Context", "message")
            (results, called_api) = await self.lavalink_query(
                ctx(player.channel.guild),
                player,
                audio_dataclasses.Query.process_input(_TOP_100_US),
            )
            tracks = list(results.tracks)
    if tracks:
        multiple = len(tracks) > 1
        track = tracks[0]

        valid = not multiple
        tries = len(tracks)
        while valid is False and multiple:
            tries -= 1
            if tries <= 0:
                raise DatabaseError("No valid entry found")
            track = random.choice(tracks)
            query = audio_dataclasses.Query.process_input(track)
            await asyncio.sleep(0.001)
            if not query.valid:
                continue
            if query.is_local and not query.track.exists():
                continue
            if not await is_allowed(
                player.channel.guild,
                (
                    f"{track.title} {track.author} {track.uri} "
                    f"{str(audio_dataclasses.Query.process_input(track))}"
                ),
            ):
                log.debug(
                    "Query is not allowed in "
                    f"{player.channel.guild} ({player.channel.guild.id})"
                )
                continue
            valid = True

        track.extras["autoplay"] = True
        player.add(player.channel.guild.me, track)
        self.bot.dispatch(
            "red_audio_track_auto_play",
            player.channel.guild,
            track,
            player.channel.guild.me,
        )
        if not player.current:
            await player.play()