# Buggy code
async def play_query(
    self,
    query: str,
    guild: discord.Guild,
    channel: discord.VoiceChannel,
    is_autoplay: bool = True,
):
    if not self._player_check(guild.me):
        try:
            if (
                not channel.permissions_for(guild.me).connect
                or not channel.permissions_for(guild.me).move_members
                and userlimit(channel)
            ):
                log.error(
                    f"I don't have permission to connect to {channel} in {guild}."
                )

            await lavalink.connect(channel)
            player = lavalink.get_player(guild.id)
            player.store("connect", datetime.datetime.utcnow())
        except IndexError:
            log.debug(
                f"Connection to Lavalink has not yet been established"
                f" while trying to connect to to {channel} in {guild}."
            )
            return

    player = lavalink.get_player(guild.id)

    player.store("channel", channel.id)
    player.store("guild", guild.id)
    await self._data_check(guild.me)
    query = audio_dataclasses.Query.process_input(query)
    ctx = namedtuple("Context", "message")
    results, called_api = await self.music_cache.lavalink_query(
        ctx(guild), player, query
    )

    if not results.tracks:
        log.debug(f"Query returned no tracks.")
        return
    track = results.tracks[0]

    if not await is_allowed(
        guild, f"{track.title} {track.author} {track.uri} {str(query._raw)}"
    ):
        log.debug(f"Query is not allowed in {guild} ({guild.id})")
        return
    track.extras = {"autoplay": is_autoplay}
    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 play_query(
    self,
    query: str,
    guild: discord.Guild,
    channel: discord.VoiceChannel,
    is_autoplay: bool = True,
):
    if not self._player_check(guild.me):
        try:
            if (
                not channel.permissions_for(guild.me).connect
                or not channel.permissions_for(guild.me).move_members
                and userlimit(channel)
            ):
                log.error(
                    f"I don't have permission to connect to {channel} in {guild}."
                )

            await lavalink.connect(channel)
            player = lavalink.get_player(guild.id)
            player.store("connect", datetime.datetime.utcnow())
        except IndexError:
            log.debug(
                f"Connection to Lavalink has not yet been established"
                f" while trying to connect to to {channel} in {guild}."
            )
            return
    query = audio_dataclasses.Query.process_input(query)
    restrict = await self.config.restrict()
    if restrict and match_url(query):
        valid_url = url_check(query)
        if not valid_url:
            raise QueryUnauthorized(f"{query} is not an allowed query.")
    elif not await is_allowed(guild, f"{query}", query_obj=query):
        raise QueryUnauthorized(f"{query} is not an allowed query.")

    player = lavalink.get_player(guild.id)
    player.store("channel", channel.id)
    player.store("guild", guild.id)
    await self._data_check(guild.me)

    ctx = namedtuple("Context", "message")
    (results, called_api) = await self.music_cache.lavalink_query(
        ctx(guild), player, query
    )

    if not results.tracks:
        log.debug(f"Query returned no tracks.")
        return
    track = results.tracks[0]

    if not await is_allowed(
        guild, f"{track.title} {track.author} {track.uri} {str(query._raw)}"
    ):
        log.debug(f"Query is not allowed in {guild} ({guild.id})")
        return
    track.extras["autoplay"] = is_autoplay
    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()