diff --git a/changelog.d/audio/3176.misc.1.rst b/changelog.d/audio/3176.misc.1.rst new file mode 100644 index 000000000..d79fa147d --- /dev/null +++ b/changelog.d/audio/3176.misc.1.rst @@ -0,0 +1 @@ +Added an early exist to the `while` loop in the autoplay method, this is to that if a service is blacklisted it doesn't infinitely loop causing heartbeats. \ No newline at end of file diff --git a/redbot/cogs/audio/apis.py b/redbot/cogs/audio/apis.py index fa254ffc2..dd77733c1 100644 --- a/redbot/cogs/audio/apis.py +++ b/redbot/cogs/audio/apis.py @@ -33,7 +33,7 @@ from redbot.core import Config, commands from redbot.core.bot import Red from redbot.core.i18n import Translator, cog_i18n from . import audio_dataclasses -from .errors import InvalidTableError, SpotifyFetchError, YouTubeApiError +from .errors import InvalidTableError, SpotifyFetchError, YouTubeApiError, DatabaseError from .playlists import get_playlist from .utils import CacheLevel, Notifier, is_allowed, queue_duration, track_limit @@ -1098,10 +1098,14 @@ class MusicCache: 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(): diff --git a/redbot/cogs/audio/audio.py b/redbot/cogs/audio/audio.py index 07cbeb91a..209a571bb 100644 --- a/redbot/cogs/audio/audio.py +++ b/redbot/cogs/audio/audio.py @@ -39,7 +39,13 @@ from .apis import MusicCache, HAS_SQL, _ERROR from .checks import can_have_caching from .converters import ComplexScopeParser, ScopeParser, get_lazy_converter, get_playlist_converter from .equalizer import Equalizer -from .errors import LavalinkDownloadFailed, MissingGuild, SpotifyFetchError, TooManyMatches +from .errors import ( + DatabaseError, + LavalinkDownloadFailed, + MissingGuild, + SpotifyFetchError, + TooManyMatches, +) from .manager import ServerManager from .playlists import ( FakePlaylist, @@ -448,7 +454,16 @@ class Audio(commands.Cog): ) if autoplay and not player.queue and player.fetch("playing_song") is not None: if self.owns_autoplay is None: - await self.music_cache.autoplay(player) + try: + await self.music_cache.autoplay(player) + except DatabaseError: + notify_channel = player.fetch("channel") + if notify_channel: + notify_channel = self.bot.get_channel(notify_channel) + await self._embed_msg( + notify_channel, _("Autoplay: Couldn't get a valid track.") + ) + return else: self.bot.dispatch( "red_audio_should_auto_play", @@ -2725,7 +2740,16 @@ class Audio(commands.Cog): if not await self._currency_check(ctx, guild_data["jukebox_price"]): return if self.owns_autoplay is None: - await self.music_cache.autoplay(player) + try: + await self.music_cache.autoplay(player) + except DatabaseError: + notify_channel = player.fetch("channel") + if notify_channel: + notify_channel = self.bot.get_channel(notify_channel) + await self._embed_msg( + notify_channel, _("Autoplay: Couldn't get a valid track.") + ) + return else: self.bot.dispatch( "red_audio_should_auto_play",