[V3 Streams] cache stream alert messages across restarts (#1630)

* [V3 Streams] cache stream alert messages across restarts

* Add some stuff to debug this

* More debug stuff

* More debug stuff

* Actually save when updating a stream alert

* Remove debug stuff

Fixes #1620
This commit is contained in:
palmtree5 2018-05-13 15:42:28 -08:00 committed by Tobotimus
parent a7d7b90ae8
commit 35001107e0
2 changed files with 25 additions and 4 deletions

View File

@ -473,6 +473,7 @@ class Streams:
except:
pass
stream._messages_cache.clear()
await self.save_streams()
except:
pass
else:
@ -490,6 +491,7 @@ class Streams:
try:
m = await channel.send(content, embed=embed)
stream._messages_cache.append(m)
await self.save_streams()
except:
pass
@ -521,6 +523,7 @@ class Streams:
except:
pass
community._messages_cache.clear()
await self.save_communities()
except:
pass
else:
@ -536,11 +539,13 @@ class Streams:
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 @@ class Streams:
_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 @@ class Streams:
_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))

View File

@ -27,7 +27,7 @@ class TwitchCommunity:
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 @@ class TwitchCommunity:
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 @@ class Stream:
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 @@ class Stream:
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):