Repository: Cog-Creators/Red-DiscordBot

Issue:
[V3 Streams] streamalert messages not tracked between restarts
# Command bugs

<!-- 
Did you find a bug with a command? Fill out the following:
-->

#### Command name

`[p]streamalert twitch channel`

#### What cog is this command from?

`Streams`

#### What were you expecting to happen?

Stream alert messages should be tracked between reboots

#### What actually happened?

They're not tracked

#### How can we reproduce this issue?

1. Set up a stream alert
2. Wait for that channel to be live and the alert message to be posted
3. Restart the bot while the channel is live



Discussion:

Resolving patch:
diff --git a/redbot/cogs/streams/streams.py b/redbot/cogs/streams/streams.py
--- a/redbot/cogs/streams/streams.py
+++ b/redbot/cogs/streams/streams.py
@@ -473,6 +473,7 @@ async def check_streams(self):
                     except:
                         pass
                 stream._messages_cache.clear()
+                await self.save_streams()
             except:
                 pass
             else:
@@ -490,6 +491,7 @@ async def check_streams(self):
                     try:
                         m = await channel.send(content, embed=embed)
                         stream._messages_cache.append(m)
+                        await self.save_streams()
                     except:
                         pass
 
@@ -521,6 +523,7 @@ async def check_communities(self):
                     except:
                         pass
                 community._messages_cache.clear()
+                await self.save_communities()
             except:
                 pass
             else:
@@ -536,11 +539,13 @@ async def check_communities(self):
                         else:
                             msg = await chn.send(embed=emb)
                         community._messages_cache.append(msg)
+                        await self.save_communities()
                     else:
                         chn_msg = sorted(chn_msg, key=lambda x: x.created_at, reverse=True)[0]
                         community._messages_cache.remove(chn_msg)
                         await chn_msg.edit(embed=emb)
                         community._messages_cache.append(chn_msg)
+                        await self.save_communities()
 
     async def filter_streams(self, streams: list, channel: discord.TextChannel) -> list:
         filtered = []
@@ -561,7 +566,12 @@ async def load_streams(self):
             _class = getattr(StreamClasses, raw_stream["type"], None)
             if not _class:
                 continue
-
+            raw_msg_cache = raw_stream["messages"]
+            raw_stream["_messages_cache"] = []
+            for raw_msg in raw_msg_cache:
+                chn = self.bot.get_channel(raw_msg["channel"])
+                msg = await chn.get_message(raw_msg["message"])
+                raw_stream["_messages_cache"].append(msg)
             token = await self.db.tokens.get_raw(_class.__name__)
             streams.append(_class(token=token, **raw_stream))
 
@@ -581,7 +591,12 @@ async def load_communities(self):
             _class = getattr(StreamClasses, raw_community["type"], None)
             if not _class:
                 continue
-
+            raw_msg_cache = raw_community["messages"]
+            raw_community["_messages_cache"] = []
+            for raw_msg in raw_msg_cache:
+                chn = self.bot.get_channel(raw_msg["channel"])
+                msg = await chn.get_message(raw_msg["message"])
+                raw_community["_messages_cache"].append(msg)
             token = await self.db.tokens.get_raw(_class.__name__, default=None)
             communities.append(_class(token=token, **raw_community))
 
diff --git a/redbot/cogs/streams/streamtypes.py b/redbot/cogs/streams/streamtypes.py
--- a/redbot/cogs/streams/streamtypes.py
+++ b/redbot/cogs/streams/streamtypes.py
@@ -27,7 +27,7 @@ def __init__(self, **kwargs):
         self.name = kwargs.pop("name")
         self.id = kwargs.pop("id", None)
         self.channels = kwargs.pop("channels", [])
-        self._messages_cache = []
+        self._messages_cache = kwargs.pop("_messages_cache", [])
         self._token = kwargs.pop("token", None)
         self.type = self.__class__.__name__
 
@@ -115,6 +115,9 @@ def export(self):
         for k, v in self.__dict__.items():
             if not k.startswith("_"):
                 data[k] = v
+        data["messages"] = []
+        for m in self._messages_cache:
+            data["messages"].append({"channel": m.channel.id, "message": m.id})
         return data
 
     def __repr__(self):
@@ -126,7 +129,7 @@ def __init__(self, **kwargs):
         self.name = kwargs.pop("name", None)
         self.channels = kwargs.pop("channels", [])
         #self.already_online = kwargs.pop("already_online", False)
-        self._messages_cache = []
+        self._messages_cache = kwargs.pop("_messages_cache", [])
         self.type = self.__class__.__name__
 
     async def is_online(self):
@@ -140,6 +143,9 @@ def export(self):
         for k, v in self.__dict__.items():
             if not k.startswith("_"):
                 data[k] = v
+        data["messages"] = []
+        for m in self._messages_cache:
+            data["messages"].append({"channel": m.channel.id, "message": m.id})
         return data
 
     def __repr__(self):


Test patch: