From 6c488ea951ba305bf8a41c9c113ad32f790c347b Mon Sep 17 00:00:00 2001 From: Will Tekulve Date: Sat, 9 Apr 2016 19:05:06 -0400 Subject: [PATCH] pep8 and not send messages when we don't have permissions --- cogs/streams.py | 161 +++++++++++++++++++++++++++++++----------------- 1 file changed, 104 insertions(+), 57 deletions(-) diff --git a/cogs/streams.py b/cogs/streams.py index ed69f628c..7e0e84c13 100644 --- a/cogs/streams.py +++ b/cogs/streams.py @@ -9,6 +9,7 @@ import asyncio from copy import deepcopy import logging + class Streams: """Streams @@ -21,40 +22,42 @@ class Streams: self.beam_streams = fileIO("data/streams/beam.json", "load") @commands.command() - async def hitbox(self, stream : str): + async def hitbox(self, stream: str): """Checks if hitbox stream is online""" online = await self.hitbox_online(stream) if online is True: - await self.bot.say("http://www.hitbox.tv/{}/ is online!".format(stream)) - elif online == False: + await self.bot.say("http://www.hitbox.tv/{}/" + " is online!".format(stream)) + elif online is False: await self.bot.say(stream + " is offline.") - elif online == None: + elif online is None: await self.bot.say("That stream doesn't exist.") else: await self.bot.say("Error.") @commands.command() - async def twitch(self, stream : str): + async def twitch(self, stream: str): """Checks if twitch stream is online""" online = await self.twitch_online(stream) if online is True: - await self.bot.say("http://www.twitch.tv/{} is online!".format(stream)) - elif online == False: + await self.bot.say("http://www.twitch.tv/{} " + "is online!".format(stream)) + elif online is False: await self.bot.say(stream + " is offline.") - elif online == None: + elif online is None: await self.bot.say("That stream doesn't exist.") else: await self.bot.say("Error.") @commands.command() - async def beam(self, stream : str): + async def beam(self, stream: str): """Checks if beam stream is online""" online = await self.beam_online(stream) if online is True: await self.bot.say("https://beam.pro/{} is online!".format(stream)) - elif online == False: + elif online is False: await self.bot.say(stream + " is offline.") - elif online == None: + elif online is None: await self.bot.say("That stream doesn't exist.") else: await self.bot.say("Error.") @@ -67,7 +70,7 @@ class Streams: await self.bot.say("Type help streamalert for info.") @streamalert.command(name="twitch", pass_context=True) - async def twitch_alert(self, ctx, stream : str): + async def twitch_alert(self, ctx, stream: str): """Adds/removes twitch alerts from the current channel""" channel = ctx.message.channel check = await self.twitch_exists(stream) @@ -77,7 +80,7 @@ class Streams: elif check == "error": await self.bot.say("Couldn't contact Twitch API. Try again later.") return - + done = False for i, s in enumerate(self.twitch_streams): @@ -85,35 +88,42 @@ class Streams: if channel.id in s["CHANNELS"]: if len(s["CHANNELS"]) == 1: self.twitch_streams.remove(s) - await self.bot.say("Alert has been removed from this channel.") + await self.bot.say("Alert has been removed " + "from this channel.") done = True else: self.twitch_streams[i]["CHANNELS"].remove(channel.id) - await self.bot.say("Alert has been removed from this channel.") + await self.bot.say("Alert has been removed " + "from this channel.") done = True else: self.twitch_streams[i]["CHANNELS"].append(channel.id) - await self.bot.say("Alert activated. I will notify this channel everytime {} is live.".format(stream)) + await self.bot.say("Alert activated. I will notify this " + + "channel everytime {}".format(stream) + + " is live.") done = True if not done: - self.twitch_streams.append({"CHANNELS" : [channel.id], "NAME" : stream, "ALREADY_ONLINE" : False}) - await self.bot.say("Alert activated. I will notify this channel everytime {} is live.".format(stream)) + self.twitch_streams.append( + {"CHANNELS": [channel.id], + "NAME": stream, "ALREADY_ONLINE": False}) + await self.bot.say("Alert activated. I will notify this channel " + "everytime {} is live.".format(stream)) fileIO("data/streams/twitch.json", "save", self.twitch_streams) @streamalert.command(name="hitbox", pass_context=True) - async def hitbox_alert(self, ctx, stream : str): + async def hitbox_alert(self, ctx, stream: str): """Adds/removes hitbox alerts from the current channel""" channel = ctx.message.channel check = await self.hitbox_online(stream) - if check == None: + if check is None: await self.bot.say("That stream doesn't exist.") return elif check == "error": await self.bot.say("Error.") return - + done = False for i, s in enumerate(self.hitbox_streams): @@ -121,35 +131,42 @@ class Streams: if channel.id in s["CHANNELS"]: if len(s["CHANNELS"]) == 1: self.hitbox_streams.remove(s) - await self.bot.say("Alert has been removed from this channel.") + await self.bot.say("Alert has been removed from this " + "channel.") done = True else: self.hitbox_streams[i]["CHANNELS"].remove(channel.id) - await self.bot.say("Alert has been removed from this channel.") + await self.bot.say("Alert has been removed from this " + "channel.") done = True else: self.hitbox_streams[i]["CHANNELS"].append(channel.id) - await self.bot.say("Alert activated. I will notify this channel everytime {} is live.".format(stream)) + await self.bot.say("Alert activated. I will notify this " + "channel everytime " + "{} is live.".format(stream)) done = True if not done: - self.hitbox_streams.append({"CHANNELS" : [channel.id], "NAME" : stream, "ALREADY_ONLINE" : False}) - await self.bot.say("Alert activated. I will notify this channel everytime {} is live.".format(stream)) + self.hitbox_streams.append( + {"CHANNELS": [channel.id], "NAME": stream, + "ALREADY_ONLINE": False}) + await self.bot.say("Alert activated. I will notify this channel " + "everytime {} is live.".format(stream)) fileIO("data/streams/hitbox.json", "save", self.hitbox_streams) @streamalert.command(name="beam", pass_context=True) - async def beam_alert(self, ctx, stream : str): + async def beam_alert(self, ctx, stream: str): """Adds/removes beam alerts from the current channel""" channel = ctx.message.channel check = await self.beam_online(stream) - if check == None: + if check is None: await self.bot.say("That stream doesn't exist.") return elif check == "error": await self.bot.say("Error.") return - + done = False for i, s in enumerate(self.beam_streams): @@ -157,20 +174,27 @@ class Streams: if channel.id in s["CHANNELS"]: if len(s["CHANNELS"]) == 1: self.beam_streams.remove(s) - await self.bot.say("Alert has been removed from this channel.") + await self.bot.say("Alert has been removed from this " + "channel.") done = True else: self.beam_streams[i]["CHANNELS"].remove(channel.id) - await self.bot.say("Alert has been removed from this channel.") + await self.bot.say("Alert has been removed from this " + "channel.") done = True else: self.beam_streams[i]["CHANNELS"].append(channel.id) - await self.bot.say("Alert activated. I will notify this channel everytime {} is live.".format(stream)) + await self.bot.say("Alert activated. I will notify this " + "channel everytime " + "{} is live.".format(stream)) done = True if not done: - self.beam_streams.append({"CHANNELS" : [channel.id], "NAME" : stream, "ALREADY_ONLINE" : False}) - await self.bot.say("Alert activated. I will notify this channel everytime {} is live.".format(stream)) + self.beam_streams.append( + {"CHANNELS": [channel.id], "NAME": stream, + "ALREADY_ONLINE": False}) + await self.bot.say("Alert activated. I will notify this channel " + "everytime {} is live.".format(stream)) fileIO("data/streams/beam.json", "save", self.beam_streams) @@ -219,8 +243,8 @@ class Streams: fileIO("data/streams/hitbox.json", "save", self.hitbox_streams) fileIO("data/streams/beam.json", "save", self.beam_streams) - await self.bot.say("There will be no more stream alerts in this channel.") - + await self.bot.say("There will be no more stream alerts in this " + "channel.") async def hitbox_online(self, stream): url = "https://api.hitbox.tv/user/" + stream @@ -231,13 +255,13 @@ class Streams: return False elif data["is_live"] == "1": return True - elif data["is_live"] == None: + elif data["is_live"] is None: return None except: return "error" async def twitch_online(self, stream): - url = "https://api.twitch.tv/kraken/streams?channel=" + stream + url = "https://api.twitch.tv/kraken/streams?channel=" + stream try: async with aiohttp.get(url) as r: data = await r.json() @@ -250,12 +274,12 @@ class Streams: return "error" async def beam_online(self, stream): - url = "https://beam.pro/api/v1/channels/" + stream + url = "https://beam.pro/api/v1/channels/" + stream try: async with aiohttp.get(url) as r: data = await r.json() if "online" in data: - if data["online"] == True: + if data["online"] is True: return True else: return False @@ -266,7 +290,7 @@ class Streams: return "error" async def twitch_exists(self, stream): - url = "https://api.twitch.tv/channels/" + stream + url = "https://api.twitch.tv/channels/" + stream try: async with aiohttp.get(url) as r: data = await r.json() @@ -279,31 +303,44 @@ class Streams: async def stream_checker(self): CHECK_DELAY = 60 - + while self == self.bot.get_cog("Streams"): - - old = (deepcopy(self.twitch_streams), deepcopy(self.hitbox_streams), deepcopy(self.beam_streams)) + + old = (deepcopy(self.twitch_streams), deepcopy( + self.hitbox_streams), deepcopy(self.beam_streams)) for stream in self.twitch_streams: online = await self.twitch_online(stream["NAME"]) if online is True and not stream["ALREADY_ONLINE"]: stream["ALREADY_ONLINE"] = True for channel in stream["CHANNELS"]: - if self.bot.get_channel(channel): - await self.bot.send_message(self.bot.get_channel(channel), "http://www.twitch.tv/{} is online!".format(stream["NAME"])) + channel_obj = self.bot.get_channel(channel) + can_speak = channel_obj.permissions_for(channel_obj.server.me).send_messages + if channel_obj and can_speak: + await self.bot.send_message( + self.bot.get_channel(channel), + "http://www.twitch.tv/" + "{} is online!".format(stream["NAME"])) else: - if stream["ALREADY_ONLINE"] and not online: stream["ALREADY_ONLINE"] = False + if stream["ALREADY_ONLINE"] and not online: + stream["ALREADY_ONLINE"] = False await asyncio.sleep(0.5) - + for stream in self.hitbox_streams: online = await self.hitbox_online(stream["NAME"]) if online is True and not stream["ALREADY_ONLINE"]: stream["ALREADY_ONLINE"] = True for channel in stream["CHANNELS"]: - if self.bot.get_channel(channel): - await self.bot.send_message(self.bot.get_channel(channel), "http://www.hitbox.tv/{} is online!".format(stream["NAME"])) + channel_obj = self.bot.get_channel(channel) + can_speak = channel_obj.permissions_for(channel_obj.server.me).send_messages + if channel_obj and can_speak: + await self.bot.send_message( + self.bot.get_channel(channel), + "http://www.hitbox.tv/" + "{} is online!".format(stream["NAME"])) else: - if stream["ALREADY_ONLINE"] and not online: stream["ALREADY_ONLINE"] = False + if stream["ALREADY_ONLINE"] and not online: + stream["ALREADY_ONLINE"] = False await asyncio.sleep(0.5) for stream in self.beam_streams: @@ -311,24 +348,33 @@ class Streams: if online is True and not stream["ALREADY_ONLINE"]: stream["ALREADY_ONLINE"] = True for channel in stream["CHANNELS"]: - if self.bot.get_channel(channel): - await self.bot.send_message(self.bot.get_channel(channel), "https://beam.pro/{} is online!".format(stream["NAME"])) + channel_obj = self.bot.get_channel(channel) + can_speak = channel_obj.permissions_for(channel_obj.server.me).send_messages + if channel_obj and can_speak: + await self.bot.send_message( + self.bot.get_channel(channel), + "https://beam.pro/" + "{} is online!".format(stream["NAME"])) else: - if stream["ALREADY_ONLINE"] and not online: stream["ALREADY_ONLINE"] = False + if stream["ALREADY_ONLINE"] and not online: + stream["ALREADY_ONLINE"] = False await asyncio.sleep(0.5) - if old != (self.twitch_streams, self.hitbox_streams, self.beam_streams): + if old != (self.twitch_streams, self.hitbox_streams, + self.beam_streams): fileIO("data/streams/twitch.json", "save", self.twitch_streams) fileIO("data/streams/hitbox.json", "save", self.hitbox_streams) fileIO("data/streams/beam.json", "save", self.beam_streams) - + await asyncio.sleep(CHECK_DELAY) + def check_folders(): if not os.path.exists("data/streams"): print("Creating data/streams folder...") os.makedirs("data/streams") + def check_files(): f = "data/streams/twitch.json" if not fileIO(f, "check"): @@ -345,9 +391,10 @@ def check_files(): print("Creating empty beam.json...") fileIO(f, "save", []) + def setup(bot): logger = logging.getLogger('aiohttp.client') - logger.setLevel(50) #Stops warning spam + logger.setLevel(50) # Stops warning spam check_folders() check_files() n = Streams(bot)