From 3bd12851b80942c6cebc7a46dbe14e2f86fc5319 Mon Sep 17 00:00:00 2001 From: Phxntxm Date: Sat, 12 Nov 2016 18:10:36 -0600 Subject: [PATCH] Logging possible exceptions --- cogs/da.py | 84 +++++++++++++++++++----------------- cogs/picarto.py | 111 +++++++++++++++++++++++++----------------------- cogs/twitch.py | 105 +++++++++++++++++++++++---------------------- 3 files changed, 158 insertions(+), 142 deletions(-) diff --git a/cogs/da.py b/cogs/da.py index 67f3799..7959d6d 100644 --- a/cogs/da.py +++ b/cogs/da.py @@ -1,6 +1,7 @@ import aiohttp import asyncio import discord +import traceback from discord.ext import commands from .utils import config @@ -52,48 +53,53 @@ class Deviantart: # People might sub to the same person, so lets cache every person and their last update cache = {} - for entry in content: - user = discord.utils.get(self.bot.get_all_members(), id=entry['member_id']) + try: + for entry in content: + user = discord.utils.get(self.bot.get_all_members(), id=entry['member_id']) - # If we're sharded, we might not be able to find this user. - # If the bot is not in the server with the member either - if user is None: - continue + # If we're sharded, we might not be able to find this user. + # If the bot is not in the server with the member either + if user is None: + continue - params = self.params.copy() - # Now loop through the subscriptions - for da_name in entry['subbed']: - # Check what the last updated content we sent to this user was - # Since we cannot go back in time, if this doesn't match the last uploaded from the user - # Assume we need to notify the user of this post - last_updated_id = entry['last_updated'].get(da_name, None) - # Check if this user has been requested already, if so we don't need to make another request - result = cache.get(da_name, None) - if result is None: - params['username'] = da_name - async with self.session.get(self.base_url, headers=self.headers, params=params) as response: - data = await response.json() - result = data['results'][0] - cache[da_name] = result + params = self.params.copy() + # Now loop through the subscriptions + for da_name in entry['subbed']: + # Check what the last updated content we sent to this user was + # Since we cannot go back in time, if this doesn't match the last uploaded from the user + # Assume we need to notify the user of this post + last_updated_id = entry['last_updated'].get(da_name, None) + # Check if this user has been requested already, if so we don't need to make another request + result = cache.get(da_name, None) + if result is None: + params['username'] = da_name + async with self.session.get(self.base_url, headers=self.headers, params=params) as response: + data = await response.json() + result = data['results'][0] + cache[da_name] = result - # This means that our last update to this user, for this author, is not the same - if last_updated_id != result['deviationid']: - # First lets check if the last updated ID was None, if so...then we haven't alerted them yet - # We don't want to alert them in this case - # We just want to act like the artist's most recent update was the last notified - # So just notify the user if this is not None - if last_updated_id is not None: - fmt = "There has been a new post by an artist you are subscribed to!\n\n" \ - "**Title:** {}\n**User:** {}\n**URL:** {}".format( - result['title'], - result['author']['username'], - result['url']) - await self.bot.send_message(user, fmt) - # Now we can update the user's last updated for this DA - # We want to do this whether or not our last if statement was met - r_filter = {'member_id': user.id} - update = {'last_updated': {da_name: result['deviationid']}} - await config.update_content('deviantart', update, r_filter) + # This means that our last update to this user, for this author, is not the same + if last_updated_id != result['deviationid']: + # First lets check if the last updated ID was None, if so...then we haven't alerted them yet + # We don't want to alert them in this case + # We just want to act like the artist's most recent update was the last notified + # So just notify the user if this is not None + if last_updated_id is not None: + fmt = "There has been a new post by an artist you are subscribed to!\n\n" \ + "**Title:** {}\n**User:** {}\n**URL:** {}".format( + result['title'], + result['author']['username'], + result['url']) + await self.bot.send_message(user, fmt) + # Now we can update the user's last updated for this DA + # We want to do this whether or not our last if statement was met + r_filter = {'member_id': user.id} + update = {'last_updated': {da_name: result['deviationid']}} + await config.update_content('deviantart', update, r_filter) + except Exception as e: + tb = traceback.format_tb(e) + fmt = "{}\n{0.__class__.__name__}: {0}".format(tb, e) + log.error(fmt) @commands.group() @checks.custom_perms(send_messages=True) diff --git a/cogs/picarto.py b/cogs/picarto.py index 0281f9d..d2de907 100644 --- a/cogs/picarto.py +++ b/cogs/picarto.py @@ -46,61 +46,66 @@ class Picarto: async def check_channels(self): await self.bot.wait_until_ready() # This is a loop that runs every 30 seconds, checking if anyone has gone online - while not self.bot.is_closed: - r_filter = {'notifications_on': 1} - picarto = await config.get_content('picarto', r_filter) - # Get all online users before looping, so that only one request is needed - online_users_list = await online_users() - old_online_users = {data['member_id']: data for data in picarto if data['live']} - old_offline_users = {data['member_id']: data for data in picarto if not data['live']} + try: + while not self.bot.is_closed: + r_filter = {'notifications_on': 1} + picarto = await config.get_content('picarto', r_filter) + # Get all online users before looping, so that only one request is needed + online_users_list = await online_users() + old_online_users = {data['member_id']: data for data in picarto if data['live']} + old_offline_users = {data['member_id']: data for data in picarto if not data['live']} - for m_id, result in old_offline_users.items(): - # Get their url and their user based on that url - url = result['picarto_url'] - user = re.search("(?<=picarto.tv/)(.*)", url).group(1) - # Check if they are online right now - if check_online(online_users_list, user): - for server_id in result['servers']: - # Get the channel to send the message to, based on the saved alert's channel - server = self.bot.get_server(server_id) - if server is None: - continue - server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) - try: - channel_id = server_alerts[0] - except IndexError: - channel_id = server_id - channel = self.bot.get_channel(channel_id) - # Get the member that has just gone live - member = discord.utils.get(server.members, id=m_id) + for m_id, result in old_offline_users.items(): + # Get their url and their user based on that url + url = result['picarto_url'] + user = re.search("(?<=picarto.tv/)(.*)", url).group(1) + # Check if they are online right now + if check_online(online_users_list, user): + for server_id in result['servers']: + # Get the channel to send the message to, based on the saved alert's channel + server = self.bot.get_server(server_id) + if server is None: + continue + server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) + try: + channel_id = server_alerts[0] + except IndexError: + channel_id = server_id + channel = self.bot.get_channel(channel_id) + # Get the member that has just gone live + member = discord.utils.get(server.members, id=m_id) - fmt = "{} has just gone live! View their stream at {}".format(member.display_name, url) - await self.bot.send_message(channel, fmt) - await config.update_content('picarto', {'live': 1}, {'member_id': m_id}) - for m_id, result in old_online_users.items(): - # Get their url and their user based on that url - url = result['picarto_url'] - user = re.search("(?<=picarto.tv/)(.*)", url).group(1) - # Check if they are online right now - if not check_online(online_users_list, user): - for server_id in result['servers']: - # Get the channel to send the message to, based on the saved alert's channel - server = self.bot.get_server(server_id) - if server is None: - continue - server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) - try: - channel_id = server_alerts[0] - except IndexError: - channel_id = server_id - channel = self.bot.get_channel(channel_id) - # Get the member that has just gone live - member = discord.utils.get(server.members, id=m_id) - fmt = "{} has just gone offline! Catch them next time they stream at {}".format( - member.display_name, url) - await self.bot.send_message(channel, fmt) - await config.update_content('picarto', {'live': 0}, {'member_id': m_id}) - await asyncio.sleep(30) + fmt = "{} has just gone live! View their stream at {}".format(member.display_name, url) + await self.bot.send_message(channel, fmt) + await config.update_content('picarto', {'live': 1}, {'member_id': m_id}) + for m_id, result in old_online_users.items(): + # Get their url and their user based on that url + url = result['picarto_url'] + user = re.search("(?<=picarto.tv/)(.*)", url).group(1) + # Check if they are online right now + if not check_online(online_users_list, user): + for server_id in result['servers']: + # Get the channel to send the message to, based on the saved alert's channel + server = self.bot.get_server(server_id) + if server is None: + continue + server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) + try: + channel_id = server_alerts[0] + except IndexError: + channel_id = server_id + channel = self.bot.get_channel(channel_id) + # Get the member that has just gone live + member = discord.utils.get(server.members, id=m_id) + fmt = "{} has just gone offline! Catch them next time they stream at {}".format( + member.display_name, url) + await self.bot.send_message(channel, fmt) + await config.update_content('picarto', {'live': 0}, {'member_id': m_id}) + await asyncio.sleep(30) + except Exception as e: + tb = traceback.format_tb(e) + fmt = "{}\n{0.__class__.__name__}: {0}".format(tb, e) + log.error(fmt) @commands.group(pass_context=True, invoke_without_command=True, no_pm=True) @checks.custom_perms(send_messages=True) diff --git a/cogs/twitch.py b/cogs/twitch.py index c25b03c..87038ed 100644 --- a/cogs/twitch.py +++ b/cogs/twitch.py @@ -42,57 +42,62 @@ class Twitch: async def check_channels(self): await self.bot.wait_until_ready() # Loop through as long as the bot is connected - while not self.bot.is_closed: - twitch = await config.get_content('twitch', {'notifications_on': 1}) - # Online/offline is based on whether they are set to such, in the config file - # This means they were detected as online/offline before and we check for a change - online_users = {data['member_id']: data for data in twitch if data['live']} - offline_users = {data['member_id']: data for data in twitch if not data['live']} - for m_id, result in offline_users.items(): - # Get their url and their user based on that url - url = result['twitch_url'] - user = re.search("(?<=twitch.tv/)(.*)", url).group(1) - # Check if they are online right now - if await self.channel_online(user): - for server_id in result['servers']: - # Get the channel to send the message to, based on the saved alert's channel - server = self.bot.get_server(server_id) - if server is None: - continue - server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) - channel_id = server_id - if len(server_alerts) > 0: - channel_id = server_alerts[0].get('channel_id') - channel = self.bot.get_channel(channel_id) - # Get the member that has just gone live - member = discord.utils.get(server.members, id=m_id) + try: + while not self.bot.is_closed: + twitch = await config.get_content('twitch', {'notifications_on': 1}) + # Online/offline is based on whether they are set to such, in the config file + # This means they were detected as online/offline before and we check for a change + online_users = {data['member_id']: data for data in twitch if data['live']} + offline_users = {data['member_id']: data for data in twitch if not data['live']} + for m_id, result in offline_users.items(): + # Get their url and their user based on that url + url = result['twitch_url'] + user = re.search("(?<=twitch.tv/)(.*)", url).group(1) + # Check if they are online right now + if await self.channel_online(user): + for server_id in result['servers']: + # Get the channel to send the message to, based on the saved alert's channel + server = self.bot.get_server(server_id) + if server is None: + continue + server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) + channel_id = server_id + if len(server_alerts) > 0: + channel_id = server_alerts[0].get('channel_id') + channel = self.bot.get_channel(channel_id) + # Get the member that has just gone live + member = discord.utils.get(server.members, id=m_id) - fmt = "{} has just gone live! View their stream at {}".format(member.display_name, url) - await self.bot.send_message(channel, fmt) - await config.update_content('twitch', {'live': 1}, {'member_id': m_id}) - for m_id, result in online_users.items(): - # Get their url and their user based on that url - url = result['twitch_url'] - user = re.search("(?<=twitch.tv/)(.*)", url).group(1) - # Check if they are online right now - if not await self.channel_online(user): - for server_id in result['servers']: - # Get the channel to send the message to, based on the saved alert's channel - server = self.bot.get_server(server_id) - if server is None: - continue - server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) - channel_id = server_id - if len(server_alerts) > 0: - channel_id = server_alerts[0].get('channel_id') - channel = self.bot.get_channel(channel_id) - # Get the member that has just gone live - member = discord.utils.get(server.members, id=m_id) - fmt = "{} has just gone offline! Catch them next time they stream at {}".format( - member.display_name, url) - await self.bot.send_message(channel, fmt) - await config.update_content('twitch', {'live': 0}, {'member_id': m_id}) - await asyncio.sleep(30) + fmt = "{} has just gone live! View their stream at {}".format(member.display_name, url) + await self.bot.send_message(channel, fmt) + await config.update_content('twitch', {'live': 1}, {'member_id': m_id}) + for m_id, result in online_users.items(): + # Get their url and their user based on that url + url = result['twitch_url'] + user = re.search("(?<=twitch.tv/)(.*)", url).group(1) + # Check if they are online right now + if not await self.channel_online(user): + for server_id in result['servers']: + # Get the channel to send the message to, based on the saved alert's channel + server = self.bot.get_server(server_id) + if server is None: + continue + server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) + channel_id = server_id + if len(server_alerts) > 0: + channel_id = server_alerts[0].get('channel_id') + channel = self.bot.get_channel(channel_id) + # Get the member that has just gone live + member = discord.utils.get(server.members, id=m_id) + fmt = "{} has just gone offline! Catch them next time they stream at {}".format( + member.display_name, url) + await self.bot.send_message(channel, fmt) + await config.update_content('twitch', {'live': 0}, {'member_id': m_id}) + await asyncio.sleep(30) + except Exception as e: + tb = traceback.format_tb(e) + fmt = "{}\n{0.__class__.__name__}: {0}".format(tb, e) + log.error(fmt) @commands.group(no_pm=True, invoke_without_command=True, pass_context=True) @checks.custom_perms(send_messages=True)