diff --git a/changelog.d/audio/3328.hotfix.2.rst b/changelog.d/audio/3328.hotfix.2.rst new file mode 100644 index 000000000..039b506e0 --- /dev/null +++ b/changelog.d/audio/3328.hotfix.2.rst @@ -0,0 +1 @@ +Check data before it is inserted into the database to avoid corruption. \ No newline at end of file diff --git a/redbot/cogs/audio/apis.py b/redbot/cogs/audio/apis.py index 87745d5ce..5bc359efc 100644 --- a/redbot/cogs/audio/apis.py +++ b/redbot/cogs/audio/apis.py @@ -746,13 +746,13 @@ class MusicCache: (val, update) = await self.database.fetch_one("lavalink", "data", {"query": query}) if update: val = None - if val and not isinstance(val, str): + if val and isinstance(val, dict): log.debug(f"Querying Local Database for {query}") task = ("update", ("lavalink", {"query": query})) self.append_task(ctx, *task) else: val = None - if val and not forced: + if val and not forced and isinstance(val, dict): data = val data["query"] = query results = LoadResult(data) @@ -780,21 +780,25 @@ class MusicCache: ): with contextlib.suppress(SQLError): time_now = int(datetime.datetime.now(datetime.timezone.utc).timestamp()) - task = ( - "insert", - ( - "lavalink", - [ - { - "query": query, - "data": json.dumps(results._raw), - "last_updated": time_now, - "last_fetched": time_now, - } - ], - ), - ) - self.append_task(ctx, *task) + data = json.dumps(results._raw) + if all( + k in data for k in ["loadType", "playlistInfo", "isSeekable", "isStream"] + ): + task = ( + "insert", + ( + "lavalink", + [ + { + "query": query, + "data": data, + "last_updated": time_now, + "last_fetched": time_now, + } + ], + ), + ) + self.append_task(ctx, *task) return results, called_api async def run_tasks(self, ctx: Optional[commands.Context] = None, _id=None): @@ -855,7 +859,7 @@ class MusicCache: query_data["maxage"] = maxage_int vals = await self.database.fetch_all("lavalink", "data", query_data) - recently_played = [r.tracks for r in vals if r] + recently_played = [r.tracks for r in vals if r if isinstance(tracks, dict)] if recently_played: track = random.choice(recently_played) diff --git a/redbot/cogs/audio/audio.py b/redbot/cogs/audio/audio.py index f29da5c6d..356d23924 100644 --- a/redbot/cogs/audio/audio.py +++ b/redbot/cogs/audio/audio.py @@ -246,14 +246,19 @@ class Audio(commands.Cog): uri = t.get("info", {}).get("uri") if uri: t = {"loadType": "V2_COMPACT", "tracks": [t], "query": uri} - database_entries.append( - { - "query": uri, - "data": json.dumps(t), - "last_updated": time_now, - "last_fetched": time_now, - } - ) + data = json.dumps(t) + if all( + k in data + for k in ["loadType", "playlistInfo", "isSeekable", "isStream"] + ): + database_entries.append( + { + "query": uri, + "data": data, + "last_updated": time_now, + "last_fetched": time_now, + } + ) await asyncio.sleep(0) if guild_playlist: all_playlist[str(guild_id)] = guild_playlist @@ -5883,14 +5888,16 @@ class Audio(commands.Cog): uri = t.get("info", {}).get("uri") if uri: t = {"loadType": "V2_COMPACT", "tracks": [t], "query": uri} - database_entries.append( - { - "query": uri, - "data": json.dumps(t), - "last_updated": time_now, - "last_fetched": time_now, - } - ) + data = json.dumps(t) + if all(k in data for k in ["loadType", "playlistInfo", "isSeekable", "isStream"]): + database_entries.append( + { + "query": uri, + "data": data, + "last_updated": time_now, + "last_fetched": time_now, + } + ) if database_entries: await self.music_cache.database.insert("lavalink", database_entries)