From bccae0ebd3675885410f5d80182d2f93c3766113 Mon Sep 17 00:00:00 2001 From: Phxntxm Date: Sun, 10 Jul 2016 12:15:44 -0500 Subject: [PATCH] Added a notification feature that should send a message if one of the saved twitch channels goes live --- cogs/twitch.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/cogs/twitch.py b/cogs/twitch.py index fa3cd2f..046cfaa 100644 --- a/cogs/twitch.py +++ b/cogs/twitch.py @@ -2,6 +2,7 @@ from discord.ext import commands from .utils import config import urllib.request import urllib.parse +import asyncio import discord import json import re @@ -12,6 +13,26 @@ def channelOnline(channel: str): data = json.loads(response.read().decode('utf-8')) return data['stream'] is not None +async def checkChannels(bot) + await client.wait_until_ready() + cursor = config.getCursor() + cursor.execute('use {}'.format(config.db_default)) + cursor.execute('select * from twitch') + result = cursor.fetchall() + for r in result: + server = r['server_id'] + 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)) + config.closeConnection() + await asyncio.sleep(180) + + class Twitch: """Class for some twitch integration""" def __init__(self, bot): @@ -55,7 +76,7 @@ class Twitch: if result is not None: cursor.execute('update twitch set twitch_url="{}" where user_id="{}"'.format(url,ctx.message.author.id)) else: - cursor.execute('insert into twitch (user_id,server_id,twitch_url,notifications_on) values ("{}","{}","{}",1)' + cursor.execute('insert into twitch (user_id,server_id,twitch_url,notifications_on,live) values ("{}","{}","{}",1,0)' .format(ctx.message.author.id,ctx.message.server.id,url)) await self.bot.say("I have just saved your twitch url {}".format(ctx.message.author.mention)) config.closeConnection() @@ -77,3 +98,4 @@ class Twitch: def setup(bot): bot.add_cog(Twitch(bot)) + bot.loop.create_task(checkChannels(bot))