2016-07-11 03:24:20 +12:00
|
|
|
from discord.ext import commands
|
|
|
|
from .utils import config
|
2016-07-17 04:49:14 +12:00
|
|
|
from .utils import checks
|
2016-07-25 02:09:12 +12:00
|
|
|
import aiohttp
|
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 09:57:52 +12:00
|
|
|
|
2016-07-25 03:33:46 +12:00
|
|
|
async def channel_online(channel: str):
|
2016-07-11 04:34:27 +12:00
|
|
|
url = "https://api.twitch.tv/kraken/streams/{}".format(channel)
|
2016-07-25 02:09:12 +12:00
|
|
|
with aiohttp.ClientSession() as s:
|
|
|
|
async with s.get(url) as r:
|
2016-07-25 02:13:23 +12:00
|
|
|
response = await r.text()
|
2016-07-25 02:09:12 +12:00
|
|
|
data = json.loads(response)
|
2016-07-31 23:36:00 +12:00
|
|
|
try:
|
|
|
|
return data['stream'] is not None
|
|
|
|
except KeyError:
|
|
|
|
return False
|
2016-07-11 09:57:52 +12:00
|
|
|
|
|
|
|
|
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 09:57:52 +12:00
|
|
|
|
2016-07-11 03:24:20 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2016-07-13 02:44:17 +12:00
|
|
|
async def checkChannels(self):
|
|
|
|
await self.bot.wait_until_ready()
|
|
|
|
while not self.bot.is_closed:
|
2016-07-18 03:17:47 +12:00
|
|
|
twitch = config.getContent('twitch')
|
2016-07-18 09:03:07 +12:00
|
|
|
for m_id, r in twitch.items():
|
2016-07-13 02:44:17 +12:00
|
|
|
url = r['twitch_url']
|
|
|
|
live = r['live']
|
|
|
|
notify = r['notifications_on']
|
|
|
|
user = re.search("(?<=twitch.tv/)(.*)", url).group(1)
|
2016-07-30 00:36:27 +12:00
|
|
|
online = await channel_online(user)
|
|
|
|
if not live and notify and online:
|
2016-07-25 03:33:46 +12:00
|
|
|
server = discord.utils.find(lambda s: s.id == r['server_id'], self.bot.servers)
|
|
|
|
member = discord.utils.find(lambda m: m.id == m_id, server.members)
|
2016-07-18 09:03:07 +12:00
|
|
|
twitch[m_id]['live'] = 1
|
2016-07-25 03:33:46 +12:00
|
|
|
fmt = "{} has just gone live! View their stream at {}".format(member.name, url)
|
|
|
|
await self.bot.send_message(server, fmt)
|
2016-07-31 12:20:55 +12:00
|
|
|
config.saveContent('twitch', twitch)
|
2016-07-30 00:36:27 +12:00
|
|
|
elif live and not online:
|
2016-07-25 03:33:46 +12:00
|
|
|
server = discord.utils.find(lambda s: s.id == r['server_id'], self.bot.servers)
|
|
|
|
member = discord.utils.find(lambda m: m.id == m_id, server.members)
|
2016-07-18 09:03:07 +12:00
|
|
|
twitch[m_id]['live'] = 0
|
2016-07-25 03:33:46 +12:00
|
|
|
fmt = "{} has just gone offline! Catch them next time they stream at {}".format(member.name, url)
|
2016-07-31 12:20:55 +12:00
|
|
|
await self.bot.send_message(server, fmt)
|
|
|
|
config.saveContent('twitch', twitch)
|
2016-07-13 02:44:17 +12:00
|
|
|
await asyncio.sleep(30)
|
|
|
|
|
2016-07-18 09:10:12 +12:00
|
|
|
@commands.group(no_pm=True, invoke_without_command=True, pass_context=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
2016-07-18 03:48:42 +12:00
|
|
|
async def twitch(self, ctx, *, member: discord.Member=None):
|
2016-07-11 06:56:55 +12:00
|
|
|
"""Use this command to check the twitch info of a user"""
|
2016-07-25 03:33:46 +12:00
|
|
|
if member is None:
|
|
|
|
member = ctx.message.author
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-29 00:08:44 +12:00
|
|
|
twitch_channels = config.getContent('twitch') or {}
|
|
|
|
result = twitch_channels.get(ctx.message.author.id)
|
2016-07-25 03:33:46 +12:00
|
|
|
if result is None:
|
|
|
|
await self.bot.say("{} has not saved their twitch URL yet!".format(member.name))
|
|
|
|
return
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-25 03:33:46 +12:00
|
|
|
url = result['twitch_url']
|
|
|
|
user = re.search("(?<=twitch.tv/)(.*)", url).group(1)
|
|
|
|
with aiohttp.ClientSession() as s:
|
|
|
|
async with s.get("https://api.twitch.tv/kraken/channels/{}".format(user)) as r:
|
|
|
|
response = await r.text()
|
|
|
|
data = json.loads(response)
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-25 03:33:46 +12:00
|
|
|
fmt = "Username: {}".format(data['display_name'])
|
|
|
|
fmt += "\nStatus: {}".format(data['status'])
|
|
|
|
fmt += "\nFollowers: {}".format(data['followers'])
|
|
|
|
fmt += "\nURL: {}".format(url)
|
2016-08-01 07:11:05 +12:00
|
|
|
await self.bot.say("```\n{}```".format(fmt))
|
2016-07-11 09:57:52 +12:00
|
|
|
|
|
|
|
@twitch.command(name='add', pass_context=True, no_pm=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
2016-07-11 03:24:20 +12:00
|
|
|
async def add_twitch_url(self, ctx, url: str):
|
|
|
|
"""Saves your user's twitch URL"""
|
|
|
|
try:
|
2016-07-11 09:57:52 +12:00
|
|
|
url = re.search("((?<=://)?twitch.tv/)+(.*)", url).group(0)
|
2016-07-11 03:24:20 +12:00
|
|
|
except AttributeError:
|
2016-07-11 09:57:52 +12:00
|
|
|
url = "https://www.twitch.tv/{}".format(url)
|
2016-07-11 03:24:20 +12:00
|
|
|
else:
|
2016-07-11 09:57:52 +12:00
|
|
|
url = "https://www.{}".format(url)
|
|
|
|
|
2016-07-25 02:09:12 +12:00
|
|
|
with aiohttp.ClientSession() as s:
|
|
|
|
async with s.get(url) as r:
|
2016-07-31 12:20:55 +12:00
|
|
|
if not r.status == 200:
|
2016-07-25 02:09:12 +12:00
|
|
|
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 09:57:52 +12:00
|
|
|
|
2016-07-29 00:08:44 +12:00
|
|
|
twitch = config.getContent('twitch') or {}
|
2016-07-18 03:17:47 +12:00
|
|
|
result = twitch.get(ctx.message.author.id)
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-11 03:24:20 +12:00
|
|
|
if result is not None:
|
2016-07-18 03:59:37 +12:00
|
|
|
twitch[ctx.message.author.id]['twitch_url'] = url
|
2016-07-11 03:24:20 +12:00
|
|
|
else:
|
2016-07-18 09:10:12 +12:00
|
|
|
twitch[ctx.message.author.id] = {'twitch_url': url, 'server_id': ctx.message.server.id,
|
|
|
|
'notifications_on': 1, 'live': 0}
|
2016-07-29 11:15:02 +12:00
|
|
|
config.saveContent('twitch', twitch)
|
2016-07-29 00:08:44 +12:00
|
|
|
await self.bot.say("I have just saved your twitch url {}".format(ctx.message.author.mention))
|
2016-07-11 09:57:52 +12:00
|
|
|
|
|
|
|
@twitch.command(name='remove', aliases=['delete'], pass_context=True, no_pm=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(send_messages=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"""
|
2016-07-18 03:17:47 +12:00
|
|
|
twitch = config.getContent('twitch')
|
|
|
|
if twitch.get(ctx.message.author.id) is not None:
|
|
|
|
del twitch[ctx.message.author.id]
|
2016-07-29 11:15:02 +12:00
|
|
|
config.saveContent('twitch', twitch)
|
2016-07-29 00:08:44 +12:00
|
|
|
await self.bot.say("I am no longer saving your twitch URL {}".format(ctx.message.author.mention))
|
2016-07-11 03:24:20 +12:00
|
|
|
else:
|
2016-07-11 09:57:52 +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-12 00:40:03 +12:00
|
|
|
@twitch.group(pass_context=True, no_pm=True, invoke_without_command=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(send_messages=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
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2016-07-11 06:35:02 +12:00
|
|
|
@notify.command(name='on', aliases=['start,yes'], pass_context=True, no_pm=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
2016-07-11 06:35:02 +12:00
|
|
|
async def notify_on(self, ctx):
|
|
|
|
"""Turns twitch notifications on"""
|
2016-07-18 03:17:47 +12:00
|
|
|
twitch = config.getContent('twitch')
|
|
|
|
result = twitch.get(ctx.message.author.id)
|
2016-07-11 06:35:02 +12:00
|
|
|
if result is None:
|
2016-07-11 09:57:52 +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 06:39:22 +12:00
|
|
|
elif result['notifications_on']:
|
2016-07-11 09:57:52 +12:00
|
|
|
await self.bot.say("What do you want me to do, send two notifications? Not gonna happen {}".format(
|
|
|
|
ctx.message.author.mention))
|
2016-07-11 06:35:02 +12:00
|
|
|
else:
|
2016-07-18 03:17:47 +12:00
|
|
|
twitch[ctx.message.author.id]['notifications_on'] = 1
|
2016-07-29 11:15:02 +12:00
|
|
|
config.saveContent('twitch', twitch)
|
2016-07-29 00:08:44 +12:00
|
|
|
await self.bot.say("I will notify if you go live {}, you'll get a bajillion followers I promise c:".format(
|
2016-07-31 12:20:55 +12:00
|
|
|
ctx.message.author.mention))
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2016-07-11 06:35:02 +12:00
|
|
|
@notify.command(name='off', aliases=['stop,no'], pass_context=True, no_pm=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
2016-07-11 06:35:02 +12:00
|
|
|
async def notify_off(self, ctx):
|
|
|
|
"""Turns twitch notifications off"""
|
2016-07-18 03:17:47 +12:00
|
|
|
twitch = config.getContent('twitch')
|
|
|
|
if twitch.get(ctx.message.author.id) is None:
|
2016-07-11 09:57:52 +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-18 09:10:12 +12:00
|
|
|
elif not twitch.get(ctx.message.author.id)['notifications_on']:
|
2016-07-11 09:57:52 +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))
|
2016-07-11 06:35:02 +12:00
|
|
|
else:
|
2016-07-18 03:17:47 +12:00
|
|
|
twitch[ctx.message.author.id]['notifications_on'] = 0
|
2016-07-19 00:06:13 +12:00
|
|
|
if config.saveContent('twitch', twitch):
|
|
|
|
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))
|
|
|
|
else:
|
|
|
|
await self.bot.say("I was unable to save this data")
|
2016-07-11 09:57:52 +12:00
|
|
|
|
|
|
|
|
2016-07-11 03:24:55 +12:00
|
|
|
def setup(bot):
|
2016-07-13 02:44:17 +12:00
|
|
|
t = Twitch(bot)
|
|
|
|
config.loop.create_task(t.checkChannels())
|
2016-07-11 03:24:20 +12:00
|
|
|
bot.add_cog(Twitch(bot))
|