1
0
Fork 0
mirror of synced 2024-06-02 18:54:33 +12:00

Moved checkchannels to be a class of Twitch, this should make accessing bot more relaible

This commit is contained in:
phxntxm 2016-07-12 09:44:17 -05:00
parent b35aed381b
commit b61e6aacc7

View file

@ -15,32 +15,6 @@ def channelOnline(channel: str):
return data['stream'] is not None return data['stream'] is not None
async def checkChannels(bot):
await bot.wait_until_ready()
while not bot.is_closed:
cursor = config.getCursor()
cursor.execute('use {}'.format(config.db_default))
cursor.execute('select * from twitch')
result = cursor.fetchall()
for r in result:
server = discord.utils.find(lambda s: s.id == r['server_id'], bot.servers)
member = discord.utils.find(lambda m: m.id == r['user_id'], server.members)
url = r['twitch_url']
live = int(r['live'])
notify = int(r['notifications_on'])
user = re.search("(?<=twitch.tv/)(.*)", url).group(1)
if not live and notify and channelOnline(user):
cursor.execute('update twitch set live=1 where user_id="{}"'.format(r['user_id']))
await bot.send_message(server, "{} has just gone live! "
"View their stream at {}".format(member.name, url))
elif live and not channelOnline(user):
cursor.execute('update twitch set live=0 where user_id="{}"'.format(r['user_id']))
await bot.send_message(server, "{} has just gone offline! Catch them next time they stream at {}"
.format(member.name, url))
config.closeConnection()
await asyncio.sleep(30)
class Twitch: class Twitch:
"""Class for some twitch integration """Class for some twitch integration
You can add or remove your twitch stream for your user You can add or remove your twitch stream for your user
@ -49,6 +23,32 @@ class Twitch:
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
async def checkChannels(self):
await self.bot.wait_until_ready()
while not self.bot.is_closed:
cursor = config.getCursor()
cursor.execute('use {}'.format(config.db_default))
cursor.execute('select * from twitch')
result = cursor.fetchall()
for r in result:
server = discord.utils.find(lambda s: s.id == r['server_id'], self.bot.servers)
member = discord.utils.find(lambda m: m.id == r['user_id'], server.members)
url = r['twitch_url']
live = r['live']
notify = r['notifications_on']
user = re.search("(?<=twitch.tv/)(.*)", url).group(1)
if not live and notify and channelOnline(user):
cursor.execute('update twitch set live=1 where user_id="{}"'.format(r['user_id']))
await self.bot.send_message(server, "{} has just gone live! "
"View their stream at {}".format(member.name, url))
elif live and not channelOnline(user):
cursor.execute('update twitch set live=0 where user_id="{}"'.format(r['user_id']))
await self.bot.send_message(server,
"{} has just gone offline! Catch them next time they stream at {}"
.format(member.name, url))
config.closeConnection()
await asyncio.sleep(30)
@commands.group(no_pm=True, invoke_without_command=True) @commands.group(no_pm=True, invoke_without_command=True)
async def twitch(self, *, member: discord.Member=None): async def twitch(self, *, member: discord.Member=None):
"""Use this command to check the twitch info of a user""" """Use this command to check the twitch info of a user"""
@ -178,5 +178,6 @@ class Twitch:
def setup(bot): def setup(bot):
t = Twitch(bot)
config.loop.create_task(t.checkChannels())
bot.add_cog(Twitch(bot)) bot.add_cog(Twitch(bot))
config.loop.create_task(checkChannels(bot))