pep8 and check for permissions to send message for alerts

pep8 and check for permissions to send messages for alerts
This commit is contained in:
Twentysix 2016-04-10 15:34:58 +02:00
commit 54fb3ba818

View File

@ -9,6 +9,7 @@ import asyncio
from copy import deepcopy from copy import deepcopy
import logging import logging
class Streams: class Streams:
"""Streams """Streams
@ -21,40 +22,42 @@ class Streams:
self.beam_streams = fileIO("data/streams/beam.json", "load") self.beam_streams = fileIO("data/streams/beam.json", "load")
@commands.command() @commands.command()
async def hitbox(self, stream : str): async def hitbox(self, stream: str):
"""Checks if hitbox stream is online""" """Checks if hitbox stream is online"""
online = await self.hitbox_online(stream) online = await self.hitbox_online(stream)
if online is True: if online is True:
await self.bot.say("http://www.hitbox.tv/{}/ is online!".format(stream)) await self.bot.say("http://www.hitbox.tv/{}/"
elif online == False: " is online!".format(stream))
elif online is False:
await self.bot.say(stream + " is offline.") await self.bot.say(stream + " is offline.")
elif online == None: elif online is None:
await self.bot.say("That stream doesn't exist.") await self.bot.say("That stream doesn't exist.")
else: else:
await self.bot.say("Error.") await self.bot.say("Error.")
@commands.command() @commands.command()
async def twitch(self, stream : str): async def twitch(self, stream: str):
"""Checks if twitch stream is online""" """Checks if twitch stream is online"""
online = await self.twitch_online(stream) online = await self.twitch_online(stream)
if online is True: if online is True:
await self.bot.say("http://www.twitch.tv/{} is online!".format(stream)) await self.bot.say("http://www.twitch.tv/{} "
elif online == False: "is online!".format(stream))
elif online is False:
await self.bot.say(stream + " is offline.") await self.bot.say(stream + " is offline.")
elif online == None: elif online is None:
await self.bot.say("That stream doesn't exist.") await self.bot.say("That stream doesn't exist.")
else: else:
await self.bot.say("Error.") await self.bot.say("Error.")
@commands.command() @commands.command()
async def beam(self, stream : str): async def beam(self, stream: str):
"""Checks if beam stream is online""" """Checks if beam stream is online"""
online = await self.beam_online(stream) online = await self.beam_online(stream)
if online is True: if online is True:
await self.bot.say("https://beam.pro/{} is online!".format(stream)) 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.") await self.bot.say(stream + " is offline.")
elif online == None: elif online is None:
await self.bot.say("That stream doesn't exist.") await self.bot.say("That stream doesn't exist.")
else: else:
await self.bot.say("Error.") await self.bot.say("Error.")
@ -67,7 +70,7 @@ class Streams:
await self.bot.say("Type help streamalert for info.") await self.bot.say("Type help streamalert for info.")
@streamalert.command(name="twitch", pass_context=True) @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""" """Adds/removes twitch alerts from the current channel"""
channel = ctx.message.channel channel = ctx.message.channel
check = await self.twitch_exists(stream) check = await self.twitch_exists(stream)
@ -85,29 +88,36 @@ class Streams:
if channel.id in s["CHANNELS"]: if channel.id in s["CHANNELS"]:
if len(s["CHANNELS"]) == 1: if len(s["CHANNELS"]) == 1:
self.twitch_streams.remove(s) 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 done = True
else: else:
self.twitch_streams[i]["CHANNELS"].remove(channel.id) 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 done = True
else: else:
self.twitch_streams[i]["CHANNELS"].append(channel.id) 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 done = True
if not done: if not done:
self.twitch_streams.append({"CHANNELS" : [channel.id], "NAME" : stream, "ALREADY_ONLINE" : False}) self.twitch_streams.append(
await self.bot.say("Alert activated. I will notify this channel everytime {} is live.".format(stream)) {"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) fileIO("data/streams/twitch.json", "save", self.twitch_streams)
@streamalert.command(name="hitbox", pass_context=True) @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""" """Adds/removes hitbox alerts from the current channel"""
channel = ctx.message.channel channel = ctx.message.channel
check = await self.hitbox_online(stream) check = await self.hitbox_online(stream)
if check == None: if check is None:
await self.bot.say("That stream doesn't exist.") await self.bot.say("That stream doesn't exist.")
return return
elif check == "error": elif check == "error":
@ -121,29 +131,36 @@ class Streams:
if channel.id in s["CHANNELS"]: if channel.id in s["CHANNELS"]:
if len(s["CHANNELS"]) == 1: if len(s["CHANNELS"]) == 1:
self.hitbox_streams.remove(s) 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 done = True
else: else:
self.hitbox_streams[i]["CHANNELS"].remove(channel.id) 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 done = True
else: else:
self.hitbox_streams[i]["CHANNELS"].append(channel.id) 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 done = True
if not done: if not done:
self.hitbox_streams.append({"CHANNELS" : [channel.id], "NAME" : stream, "ALREADY_ONLINE" : False}) self.hitbox_streams.append(
await self.bot.say("Alert activated. I will notify this channel everytime {} is live.".format(stream)) {"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) fileIO("data/streams/hitbox.json", "save", self.hitbox_streams)
@streamalert.command(name="beam", pass_context=True) @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""" """Adds/removes beam alerts from the current channel"""
channel = ctx.message.channel channel = ctx.message.channel
check = await self.beam_online(stream) check = await self.beam_online(stream)
if check == None: if check is None:
await self.bot.say("That stream doesn't exist.") await self.bot.say("That stream doesn't exist.")
return return
elif check == "error": elif check == "error":
@ -157,20 +174,27 @@ class Streams:
if channel.id in s["CHANNELS"]: if channel.id in s["CHANNELS"]:
if len(s["CHANNELS"]) == 1: if len(s["CHANNELS"]) == 1:
self.beam_streams.remove(s) 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 done = True
else: else:
self.beam_streams[i]["CHANNELS"].remove(channel.id) 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 done = True
else: else:
self.beam_streams[i]["CHANNELS"].append(channel.id) 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 done = True
if not done: if not done:
self.beam_streams.append({"CHANNELS" : [channel.id], "NAME" : stream, "ALREADY_ONLINE" : False}) self.beam_streams.append(
await self.bot.say("Alert activated. I will notify this channel everytime {} is live.".format(stream)) {"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) 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/hitbox.json", "save", self.hitbox_streams)
fileIO("data/streams/beam.json", "save", self.beam_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): async def hitbox_online(self, stream):
url = "https://api.hitbox.tv/user/" + stream url = "https://api.hitbox.tv/user/" + stream
@ -231,7 +255,7 @@ class Streams:
return False return False
elif data["is_live"] == "1": elif data["is_live"] == "1":
return True return True
elif data["is_live"] == None: elif data["is_live"] is None:
return None return None
except: except:
return "error" return "error"
@ -255,7 +279,7 @@ class Streams:
async with aiohttp.get(url) as r: async with aiohttp.get(url) as r:
data = await r.json() data = await r.json()
if "online" in data: if "online" in data:
if data["online"] == True: if data["online"] is True:
return True return True
else: else:
return False return False
@ -282,17 +306,24 @@ class Streams:
while self == self.bot.get_cog("Streams"): 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: for stream in self.twitch_streams:
online = await self.twitch_online(stream["NAME"]) online = await self.twitch_online(stream["NAME"])
if online is True and not stream["ALREADY_ONLINE"]: if online is True and not stream["ALREADY_ONLINE"]:
stream["ALREADY_ONLINE"] = True stream["ALREADY_ONLINE"] = True
for channel in stream["CHANNELS"]: for channel in stream["CHANNELS"]:
if self.bot.get_channel(channel): channel_obj = self.bot.get_channel(channel)
await self.bot.send_message(self.bot.get_channel(channel), "http://www.twitch.tv/{} is online!".format(stream["NAME"])) 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: 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) await asyncio.sleep(0.5)
for stream in self.hitbox_streams: for stream in self.hitbox_streams:
@ -300,10 +331,16 @@ class Streams:
if online is True and not stream["ALREADY_ONLINE"]: if online is True and not stream["ALREADY_ONLINE"]:
stream["ALREADY_ONLINE"] = True stream["ALREADY_ONLINE"] = True
for channel in stream["CHANNELS"]: for channel in stream["CHANNELS"]:
if self.bot.get_channel(channel): channel_obj = self.bot.get_channel(channel)
await self.bot.send_message(self.bot.get_channel(channel), "http://www.hitbox.tv/{} is online!".format(stream["NAME"])) 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: 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) await asyncio.sleep(0.5)
for stream in self.beam_streams: for stream in self.beam_streams:
@ -311,24 +348,33 @@ class Streams:
if online is True and not stream["ALREADY_ONLINE"]: if online is True and not stream["ALREADY_ONLINE"]:
stream["ALREADY_ONLINE"] = True stream["ALREADY_ONLINE"] = True
for channel in stream["CHANNELS"]: for channel in stream["CHANNELS"]:
if self.bot.get_channel(channel): channel_obj = self.bot.get_channel(channel)
await self.bot.send_message(self.bot.get_channel(channel), "https://beam.pro/{} is online!".format(stream["NAME"])) 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: 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) 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/twitch.json", "save", self.twitch_streams)
fileIO("data/streams/hitbox.json", "save", self.hitbox_streams) fileIO("data/streams/hitbox.json", "save", self.hitbox_streams)
fileIO("data/streams/beam.json", "save", self.beam_streams) fileIO("data/streams/beam.json", "save", self.beam_streams)
await asyncio.sleep(CHECK_DELAY) await asyncio.sleep(CHECK_DELAY)
def check_folders(): def check_folders():
if not os.path.exists("data/streams"): if not os.path.exists("data/streams"):
print("Creating data/streams folder...") print("Creating data/streams folder...")
os.makedirs("data/streams") os.makedirs("data/streams")
def check_files(): def check_files():
f = "data/streams/twitch.json" f = "data/streams/twitch.json"
if not fileIO(f, "check"): if not fileIO(f, "check"):
@ -345,9 +391,10 @@ def check_files():
print("Creating empty beam.json...") print("Creating empty beam.json...")
fileIO(f, "save", []) fileIO(f, "save", [])
def setup(bot): def setup(bot):
logger = logging.getLogger('aiohttp.client') logger = logging.getLogger('aiohttp.client')
logger.setLevel(50) #Stops warning spam logger.setLevel(50) # Stops warning spam
check_folders() check_folders()
check_files() check_files()
n = Streams(bot) n = Streams(bot)