2016-07-11 03:24:20 +12:00
from discord . ext import commands
from . utils import config
2016-07-11 03:55:26 +12:00
import urllib . request
import urllib . parse
2016-07-11 05:15:44 +12:00
import asyncio
2016-07-11 03:24:20 +12:00
import discord
2016-07-11 04:34:27 +12:00
import json
2016-07-11 03:24:20 +12:00
import re
2016-07-11 04:34:27 +12:00
def channelOnline ( channel : str ) :
url = " https://api.twitch.tv/kraken/streams/ {} " . format ( channel )
response = urllib . request . urlopen ( url )
data = json . loads ( response . read ( ) . decode ( ' utf-8 ' ) )
return data [ ' stream ' ] is not None
2016-07-11 05:16:53 +12:00
async def checkChannels ( bot ) :
2016-07-11 05:37:34 +12:00
await bot . wait_until_ready ( )
2016-07-11 05:15:44 +12:00
cursor = config . getCursor ( )
cursor . execute ( ' use {} ' . format ( config . db_default ) )
cursor . execute ( ' select * from twitch ' )
result = cursor . fetchall ( )
for r in result :
2016-07-11 05:24:10 +12:00
server = discord . utils . find ( lambda s : s . id == r [ ' server_id ' ] , bot . servers )
2016-07-11 05:15:44 +12:00
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 ) )
2016-07-11 06:06:56 +12:00
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 ) )
2016-07-11 05:15:44 +12:00
config . closeConnection ( )
await asyncio . sleep ( 180 )
2016-07-11 03:24:20 +12:00
class Twitch :
2016-07-11 06:10:14 +12:00
""" Class for some twitch integration
You can add or remove your twitch stream for your user
I will then notify the server when you have gone live or offline """
2016-07-11 03:24:20 +12:00
def __init__ ( self , bot ) :
self . bot = bot
2016-07-11 03:33:54 +12:00
@commands.group ( pass_context = True , no_pm = True , invoke_without_command = True )
2016-07-11 03:24:20 +12:00
async def twitch ( self , ctx , * , member : discord . Member = None ) :
2016-07-11 05:16:53 +12:00
""" Use this command to check the twitch stream of a user """
2016-07-11 03:24:20 +12:00
pass
if member is not None :
cursor = config . getCursor ( )
cursor . execute ( ' use {} ' . format ( config . db_default ) )
cursor . execute ( ' select twitch_url from twitch where user_id= " {} " ' . format ( member . id ) )
result = cursor . fetchone ( )
if result is not None :
2016-07-11 03:36:39 +12:00
await self . bot . say ( " {} ' s twitch URL is {} " . format ( member . name , result [ ' twitch_url ' ] ) )
2016-07-11 03:24:20 +12:00
config . closeConnection ( )
else :
2016-07-11 03:36:39 +12:00
await self . bot . say ( " {} has not saved their twitch URL yet! " . format ( member . name ) )
2016-07-11 03:24:20 +12:00
config . closeConnection ( )
@twitch.command ( name = ' add ' , pass_context = True , no_pm = True )
async def add_twitch_url ( self , ctx , url : str ) :
""" Saves your user ' s twitch URL """
try :
url = re . search ( " ((?<=://)?twitch.tv/)+(.*) " , url ) . group ( 0 )
except AttributeError :
url = " https://www.twitch.tv/ {} " . format ( url )
else :
url = " https://www. {} " . format ( url )
2016-07-11 03:50:12 +12:00
try :
urllib . request . urlopen ( url )
except urllib . request . HTTPError :
await self . bot . say ( " That twitch user does not exist! What would be the point of adding a nonexistant twitch user? Silly " )
return
2016-07-11 03:24:20 +12:00
cursor = config . getCursor ( )
cursor . execute ( ' use {} ' . format ( config . db_default ) )
cursor . execute ( ' select twitch_url from twitch where user_id= " {} " ' . format ( ctx . message . author . id ) )
result = cursor . fetchone ( )
if result is not None :
2016-07-11 04:00:37 +12:00
cursor . execute ( ' update twitch set twitch_url= " {} " where user_id= " {} " ' . format ( url , ctx . message . author . id ) )
2016-07-11 03:24:20 +12:00
else :
2016-07-11 05:15:44 +12:00
cursor . execute ( ' insert into twitch (user_id,server_id,twitch_url,notifications_on,live) values ( " {} " , " {} " , " {} " ,1,0) '
2016-07-11 03:24:20 +12:00
. 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 ( )
@twitch.command ( name = ' remove ' , aliases = [ ' delete ' ] , pass_context = True , no_pm = True )
2016-07-11 06:35:02 +12:00
async def remove_twitch_url ( self , ctx ) :
2016-07-11 03:24:20 +12:00
""" Removes your twitch URL """
cursor = config . getCursor ( )
cursor . execute ( ' use {} ' . format ( config . db_default ) )
cursor . execute ( ' select twitch_url from twitch where user_id= " {} " ' . format ( ctx . message . author . id ) )
result = cursor . fetchone ( )
if result is not None :
2016-07-11 03:36:39 +12:00
cursor . execute ( ' delete from twitch where user_id= " {} " ' . format ( ctx . message . author . id ) )
2016-07-11 03:40:23 +12:00
await self . bot . say ( " I am no longer saving your twitch URL {} " . format ( ctx . message . author . mention ) )
2016-07-11 03:41:48 +12:00
config . closeConnection ( )
2016-07-11 03:24:20 +12:00
else :
2016-07-11 06:35:02 +12:00
await self . bot . say ( " I do not have your twitch URL added {} . You can save your twitch url with !twitch add " . format ( ctx . message . author . mention ) )
2016-07-11 03:24:20 +12:00
config . closeConnection ( )
2016-07-11 06:35:02 +12:00
@commands.group ( pass_context = True , no_pm = True , invoke_without_command = True )
2016-07-11 06:35:27 +12:00
async def notify ( self , ctx ) :
2016-07-11 06:35:02 +12:00
""" This can be used to turn notifications on or off """
pass
@notify.command ( name = ' on ' , aliases = [ ' start,yes ' ] , pass_context = True , no_pm = True )
async def notify_on ( self , ctx ) :
""" Turns twitch notifications on """
cursor = config . getCursor ( )
cursor . execute ( ' use {} ' . format ( config . db_default ) )
cursor . execute ( ' select notifications_on from twitch where user_id= " {} " ' . format ( ctx . message . author . id ) )
result = cursor . fetchone ( )
if result is None :
await self . bot . say ( " I do not have your twitch URL added {} . You can save your twitch url with !twitch add " . format ( ctx . message . author . mention ) )
config . closeConnection ( )
return
2016-07-11 06:36:57 +12:00
elif result == " 1 " :
2016-07-11 06:35:02 +12:00
await self . bot . say ( " What do you want me to do, send two notifications? Not gonna happen {} " . format ( ctx . message . author . mention ) )
config . closeConnection ( )
return
else :
cursor . execute ( ' update twitch set notifications_on=1 where user_id= " {} " ' . format ( ctx . message . author . id ) )
await self . bot . say ( " I will notify if you go live {} , you ' ll get a bajillion followers I promise c: " . format ( ctx . message . author . mention ) )
config . closeConnection ( )
return
@notify.command ( name = ' off ' , aliases = [ ' stop,no ' ] , pass_context = True , no_pm = True )
async def notify_off ( self , ctx ) :
""" Turns twitch notifications off """
cursor = config . getCursor ( )
cursor . execute ( ' use {} ' . format ( config . db_default ) )
cursor . execute ( ' select notifications_on from twitch where user_id= " {} " ' . format ( ctx . message . author . id ) )
result = cursor . fetchone ( )
if result is None :
await self . bot . say ( " I do not have your twitch URL added {} . You can save your twitch url with !twitch add " . format ( ctx . message . author . mention ) )
config . closeConnection ( )
return
2016-07-11 06:36:57 +12:00
elif result == " 0 " :
2016-07-11 06:35:02 +12:00
await self . bot . say ( " I am already set to not notify if you go live! Pay attention brah {} " . format ( ctx . message . author . mention ) )
config . closeConnection ( )
return
else :
cursor . execute ( ' update twitch set notifications_on=0 where user_id= " {} " ' . format ( ctx . message . author . id ) )
await self . bot . say ( " I will not notify if you go live anymore {} , are you going to stream some lewd stuff you don ' t want people to see?~ " . format ( ctx . message . author . mention ) )
config . closeConnection ( )
return
2016-07-11 03:24:20 +12:00
2016-07-11 06:35:02 +12:00
2016-07-11 03:24:55 +12:00
def setup ( bot ) :
2016-07-11 03:24:20 +12:00
bot . add_cog ( Twitch ( bot ) )
2016-07-11 05:31:53 +12:00
config . loop . create_task ( checkChannels ( bot ) )