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

Added a notification feature that should send a message if one of the saved twitch channels goes live

This commit is contained in:
Phxntxm 2016-07-10 12:15:44 -05:00
parent ebc6ea4393
commit bccae0ebd3

View file

@ -2,6 +2,7 @@ from discord.ext import commands
from .utils import config from .utils import config
import urllib.request import urllib.request
import urllib.parse import urllib.parse
import asyncio
import discord import discord
import json import json
import re import re
@ -12,6 +13,26 @@ def channelOnline(channel: str):
data = json.loads(response.read().decode('utf-8')) data = json.loads(response.read().decode('utf-8'))
return data['stream'] is not None 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 Twitch:
"""Class for some twitch integration""" """Class for some twitch integration"""
def __init__(self, bot): def __init__(self, bot):
@ -55,7 +76,7 @@ class Twitch:
if result is not None: if result is not None:
cursor.execute('update twitch set twitch_url="{}" where user_id="{}"'.format(url,ctx.message.author.id)) cursor.execute('update twitch set twitch_url="{}" where user_id="{}"'.format(url,ctx.message.author.id))
else: 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)) .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)) await self.bot.say("I have just saved your twitch url {}".format(ctx.message.author.mention))
config.closeConnection() config.closeConnection()
@ -77,3 +98,4 @@ class Twitch:
def setup(bot): def setup(bot):
bot.add_cog(Twitch(bot)) bot.add_cog(Twitch(bot))
bot.loop.create_task(checkChannels(bot))