diff options
| author | 2019-02-08 17:33:38 +0100 | |
|---|---|---|
| committer | 2019-02-08 17:33:38 +0100 | |
| commit | 6a3a54a83d0f01064615353364d475e875cbde64 (patch) | |
| tree | 69bc03c3fea83f934f3bbff6837c3d0cc0cf7a3e | |
| parent | Adding mandatory unwatch reason for both bb and talent-pool (diff) | |
Adding method to update watched cache so it's reusable
| -rw-r--r-- | bot/cogs/bigbrother.py | 55 |
1 files changed, 26 insertions, 29 deletions
diff --git a/bot/cogs/bigbrother.py b/bot/cogs/bigbrother.py index caf194b4c..36fd90ce9 100644 --- a/bot/cogs/bigbrother.py +++ b/bot/cogs/bigbrother.py @@ -73,6 +73,16 @@ class BigBrother: data = await response.json() self.update_cache(data) + async def update_watched_users(self): + async with self.bot.http_session.get(URLs.site_bigbrother_api, headers=self.HEADERS) as response: + if response.status == 200: + data = await response.json() + self.update_cache(data) + log.trace("Updated watch list cache") + return True + else: + return False + async def get_watch_information(self, user_id: int) -> WatchInformation: """ Fetches and returns the latest watch reason for a user using the infraction API """ @@ -309,35 +319,22 @@ class BigBrother: By default, the users are returned from the cache. If this is not desired, `from_cache` can be given as a falsy value, e.g. e.g. 'no'. """ - - if from_cache: - lines = tuple( - f"• <@{user_id}> in <#{self.watched_users[user_id].id}>" - for user_id in self.watched_users - ) - await LinePaginator.paginate( - lines or ("There's nothing here yet.",), - ctx, - Embed(title="Watched users (cached)", color=Color.blue()), - empty=False - ) - - else: - async with self.bot.http_session.get(URLs.site_bigbrother_api, headers=self.HEADERS) as response: - if response.status == 200: - data = await response.json() - self.update_cache(data) - lines = tuple(f"• <@{entry['user_id']}> in <#{entry['channel_id']}>" for entry in data) - - await LinePaginator.paginate( - lines or ("There's nothing here yet.",), - ctx, - Embed(title="Watched users", color=Color.blue()), - empty=False - ) - - else: - await ctx.send(f":x: got non-200 response from the API") + if not from_cache: + updated = await self.update_watched_users() + if not updated: + await ctx.send(f":x: Failed to update cache: non-200 response from the API") + return + + lines = tuple( + f"• <@{user_id}> in <#{self.watched_users[user_id].id}>" + for user_id in self.watched_users + ) + await LinePaginator.paginate( + lines or ("There's nothing here yet.",), + ctx, + Embed(title="Watched users (cached)", color=Color.blue()), + empty=False + ) @bigbrother_group.command(name='watch', aliases=('w',)) @with_role(Roles.owner, Roles.admin, Roles.moderator) |