1
0
Fork 0
mirror of synced 2024-06-28 03:00:55 +12:00

Logging possible exceptions

This commit is contained in:
Phxntxm 2016-11-12 18:10:36 -06:00
parent 6f2a8895d0
commit 3bd12851b8
3 changed files with 158 additions and 142 deletions

View file

@ -1,6 +1,7 @@
import aiohttp import aiohttp
import asyncio import asyncio
import discord import discord
import traceback
from discord.ext import commands from discord.ext import commands
from .utils import config 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 # People might sub to the same person, so lets cache every person and their last update
cache = {} cache = {}
for entry in content: try:
user = discord.utils.get(self.bot.get_all_members(), id=entry['member_id']) 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 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 the bot is not in the server with the member either
if user is None: if user is None:
continue continue
params = self.params.copy() params = self.params.copy()
# Now loop through the subscriptions # Now loop through the subscriptions
for da_name in entry['subbed']: for da_name in entry['subbed']:
# Check what the last updated content we sent to this user was # 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 # 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 # Assume we need to notify the user of this post
last_updated_id = entry['last_updated'].get(da_name, None) 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 # Check if this user has been requested already, if so we don't need to make another request
result = cache.get(da_name, None) result = cache.get(da_name, None)
if result is None: if result is None:
params['username'] = da_name params['username'] = da_name
async with self.session.get(self.base_url, headers=self.headers, params=params) as response: async with self.session.get(self.base_url, headers=self.headers, params=params) as response:
data = await response.json() data = await response.json()
result = data['results'][0] result = data['results'][0]
cache[da_name] = result cache[da_name] = result
# This means that our last update to this user, for this author, is not the same # This means that our last update to this user, for this author, is not the same
if last_updated_id != result['deviationid']: 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 # 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 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 # 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 # So just notify the user if this is not None
if last_updated_id 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" \ fmt = "There has been a new post by an artist you are subscribed to!\n\n" \
"**Title:** {}\n**User:** {}\n**URL:** {}".format( "**Title:** {}\n**User:** {}\n**URL:** {}".format(
result['title'], result['title'],
result['author']['username'], result['author']['username'],
result['url']) result['url'])
await self.bot.send_message(user, fmt) await self.bot.send_message(user, fmt)
# Now we can update the user's last updated for this DA # 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 # We want to do this whether or not our last if statement was met
r_filter = {'member_id': user.id} r_filter = {'member_id': user.id}
update = {'last_updated': {da_name: result['deviationid']}} update = {'last_updated': {da_name: result['deviationid']}}
await config.update_content('deviantart', update, r_filter) 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() @commands.group()
@checks.custom_perms(send_messages=True) @checks.custom_perms(send_messages=True)

View file

@ -46,61 +46,66 @@ class Picarto:
async def check_channels(self): async def check_channels(self):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
# This is a loop that runs every 30 seconds, checking if anyone has gone online # This is a loop that runs every 30 seconds, checking if anyone has gone online
while not self.bot.is_closed: try:
r_filter = {'notifications_on': 1} while not self.bot.is_closed:
picarto = await config.get_content('picarto', r_filter) r_filter = {'notifications_on': 1}
# Get all online users before looping, so that only one request is needed picarto = await config.get_content('picarto', r_filter)
online_users_list = await online_users() # Get all online users before looping, so that only one request is needed
old_online_users = {data['member_id']: data for data in picarto if data['live']} online_users_list = await online_users()
old_offline_users = {data['member_id']: data for data in picarto if not data['live']} 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(): for m_id, result in old_offline_users.items():
# Get their url and their user based on that url # Get their url and their user based on that url
url = result['picarto_url'] url = result['picarto_url']
user = re.search("(?<=picarto.tv/)(.*)", url).group(1) user = re.search("(?<=picarto.tv/)(.*)", url).group(1)
# Check if they are online right now # Check if they are online right now
if check_online(online_users_list, user): if check_online(online_users_list, user):
for server_id in result['servers']: for server_id in result['servers']:
# Get the channel to send the message to, based on the saved alert's channel # Get the channel to send the message to, based on the saved alert's channel
server = self.bot.get_server(server_id) server = self.bot.get_server(server_id)
if server is None: if server is None:
continue continue
server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) server_alerts = await config.get_content('server_alerts', {'server_id': server_id})
try: try:
channel_id = server_alerts[0] channel_id = server_alerts[0]
except IndexError: except IndexError:
channel_id = server_id channel_id = server_id
channel = self.bot.get_channel(channel_id) channel = self.bot.get_channel(channel_id)
# Get the member that has just gone live # Get the member that has just gone live
member = discord.utils.get(server.members, id=m_id) member = discord.utils.get(server.members, id=m_id)
fmt = "{} has just gone live! View their stream at {}".format(member.display_name, url) fmt = "{} has just gone live! View their stream at {}".format(member.display_name, url)
await self.bot.send_message(channel, fmt) await self.bot.send_message(channel, fmt)
await config.update_content('picarto', {'live': 1}, {'member_id': m_id}) await config.update_content('picarto', {'live': 1}, {'member_id': m_id})
for m_id, result in old_online_users.items(): for m_id, result in old_online_users.items():
# Get their url and their user based on that url # Get their url and their user based on that url
url = result['picarto_url'] url = result['picarto_url']
user = re.search("(?<=picarto.tv/)(.*)", url).group(1) user = re.search("(?<=picarto.tv/)(.*)", url).group(1)
# Check if they are online right now # Check if they are online right now
if not check_online(online_users_list, user): if not check_online(online_users_list, user):
for server_id in result['servers']: for server_id in result['servers']:
# Get the channel to send the message to, based on the saved alert's channel # Get the channel to send the message to, based on the saved alert's channel
server = self.bot.get_server(server_id) server = self.bot.get_server(server_id)
if server is None: if server is None:
continue continue
server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) server_alerts = await config.get_content('server_alerts', {'server_id': server_id})
try: try:
channel_id = server_alerts[0] channel_id = server_alerts[0]
except IndexError: except IndexError:
channel_id = server_id channel_id = server_id
channel = self.bot.get_channel(channel_id) channel = self.bot.get_channel(channel_id)
# Get the member that has just gone live # Get the member that has just gone live
member = discord.utils.get(server.members, id=m_id) member = discord.utils.get(server.members, id=m_id)
fmt = "{} has just gone offline! Catch them next time they stream at {}".format( fmt = "{} has just gone offline! Catch them next time they stream at {}".format(
member.display_name, url) member.display_name, url)
await self.bot.send_message(channel, fmt) await self.bot.send_message(channel, fmt)
await config.update_content('picarto', {'live': 0}, {'member_id': m_id}) await config.update_content('picarto', {'live': 0}, {'member_id': m_id})
await asyncio.sleep(30) 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) @commands.group(pass_context=True, invoke_without_command=True, no_pm=True)
@checks.custom_perms(send_messages=True) @checks.custom_perms(send_messages=True)

View file

@ -42,57 +42,62 @@ class Twitch:
async def check_channels(self): async def check_channels(self):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
# Loop through as long as the bot is connected # Loop through as long as the bot is connected
while not self.bot.is_closed: try:
twitch = await config.get_content('twitch', {'notifications_on': 1}) while not self.bot.is_closed:
# Online/offline is based on whether they are set to such, in the config file twitch = await config.get_content('twitch', {'notifications_on': 1})
# This means they were detected as online/offline before and we check for a change # Online/offline is based on whether they are set to such, in the config file
online_users = {data['member_id']: data for data in twitch if data['live']} # This means they were detected as online/offline before and we check for a change
offline_users = {data['member_id']: data for data in twitch if not data['live']} online_users = {data['member_id']: data for data in twitch if data['live']}
for m_id, result in offline_users.items(): offline_users = {data['member_id']: data for data in twitch if not data['live']}
# Get their url and their user based on that url for m_id, result in offline_users.items():
url = result['twitch_url'] # Get their url and their user based on that url
user = re.search("(?<=twitch.tv/)(.*)", url).group(1) url = result['twitch_url']
# Check if they are online right now user = re.search("(?<=twitch.tv/)(.*)", url).group(1)
if await self.channel_online(user): # Check if they are online right now
for server_id in result['servers']: if await self.channel_online(user):
# Get the channel to send the message to, based on the saved alert's channel for server_id in result['servers']:
server = self.bot.get_server(server_id) # Get the channel to send the message to, based on the saved alert's channel
if server is None: server = self.bot.get_server(server_id)
continue if server is None:
server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) continue
channel_id = server_id server_alerts = await config.get_content('server_alerts', {'server_id': server_id})
if len(server_alerts) > 0: channel_id = server_id
channel_id = server_alerts[0].get('channel_id') if len(server_alerts) > 0:
channel = self.bot.get_channel(channel_id) channel_id = server_alerts[0].get('channel_id')
# Get the member that has just gone live channel = self.bot.get_channel(channel_id)
member = discord.utils.get(server.members, id=m_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) fmt = "{} has just gone live! View their stream at {}".format(member.display_name, url)
await self.bot.send_message(channel, fmt) await self.bot.send_message(channel, fmt)
await config.update_content('twitch', {'live': 1}, {'member_id': m_id}) await config.update_content('twitch', {'live': 1}, {'member_id': m_id})
for m_id, result in online_users.items(): for m_id, result in online_users.items():
# Get their url and their user based on that url # Get their url and their user based on that url
url = result['twitch_url'] url = result['twitch_url']
user = re.search("(?<=twitch.tv/)(.*)", url).group(1) user = re.search("(?<=twitch.tv/)(.*)", url).group(1)
# Check if they are online right now # Check if they are online right now
if not await self.channel_online(user): if not await self.channel_online(user):
for server_id in result['servers']: for server_id in result['servers']:
# Get the channel to send the message to, based on the saved alert's channel # Get the channel to send the message to, based on the saved alert's channel
server = self.bot.get_server(server_id) server = self.bot.get_server(server_id)
if server is None: if server is None:
continue continue
server_alerts = await config.get_content('server_alerts', {'server_id': server_id}) server_alerts = await config.get_content('server_alerts', {'server_id': server_id})
channel_id = server_id channel_id = server_id
if len(server_alerts) > 0: if len(server_alerts) > 0:
channel_id = server_alerts[0].get('channel_id') channel_id = server_alerts[0].get('channel_id')
channel = self.bot.get_channel(channel_id) channel = self.bot.get_channel(channel_id)
# Get the member that has just gone live # Get the member that has just gone live
member = discord.utils.get(server.members, id=m_id) member = discord.utils.get(server.members, id=m_id)
fmt = "{} has just gone offline! Catch them next time they stream at {}".format( fmt = "{} has just gone offline! Catch them next time they stream at {}".format(
member.display_name, url) member.display_name, url)
await self.bot.send_message(channel, fmt) await self.bot.send_message(channel, fmt)
await config.update_content('twitch', {'live': 0}, {'member_id': m_id}) await config.update_content('twitch', {'live': 0}, {'member_id': m_id})
await asyncio.sleep(30) 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) @commands.group(no_pm=True, invoke_without_command=True, pass_context=True)
@checks.custom_perms(send_messages=True) @checks.custom_perms(send_messages=True)