2016-07-11 03:24:20 +12:00
|
|
|
from discord.ext import commands
|
2017-01-09 09:03:22 +13:00
|
|
|
|
|
|
|
from . import utils
|
2016-09-29 12:39:34 +13:00
|
|
|
|
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
|
|
|
|
import re
|
2016-09-29 12:39:34 +13:00
|
|
|
import rethinkdb as r
|
2016-11-13 13:22:43 +13:00
|
|
|
import traceback
|
2016-11-13 16:49:36 +13:00
|
|
|
import logging
|
2016-07-11 03:24:20 +12:00
|
|
|
|
2016-11-13 16:49:02 +13:00
|
|
|
log = logging.getLogger()
|
|
|
|
|
2016-11-29 21:05:22 +13: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
|
2017-01-09 09:03:22 +13:00
|
|
|
self.key = utils.twitch_key
|
2016-10-02 09:29:53 +13:00
|
|
|
self.params = {'client_id': self.key}
|
2016-09-21 10:21:48 +12:00
|
|
|
|
2017-06-18 11:04:36 +12:00
|
|
|
def _form_embed(self, data):
|
2017-07-04 14:02:07 +12:00
|
|
|
if not data:
|
|
|
|
return None
|
2017-06-18 11:04:36 +12:00
|
|
|
# I want to make the least API calls possible, however there's a few things to note here:
|
|
|
|
# 1) When requesting /streams and a channel is offline, the channel data is not provided
|
|
|
|
# 2) When requesting /streams and a channel is online, the channel data is provided
|
|
|
|
# 3) When requesting /channels, no data is provided about the channel being on or offline
|
|
|
|
# 4) The data provide in /streams matches /channels when they are online
|
|
|
|
# Due to this...the data "source" will be different based on if they are on or offline
|
|
|
|
# Instead of making an API call to see if they're online, then an API call to get the data
|
|
|
|
# The idea here is to separate creating the embed, and the getting of data
|
|
|
|
# With this method, I can get the data if they are online,then return the embed if applicable
|
|
|
|
embed = discord.Embed(title=data['display_name'], url=data['url'])
|
|
|
|
if data['logo']:
|
|
|
|
embed.set_thumbnail(url=data['logo'])
|
|
|
|
|
|
|
|
embed.add_field(name='Title', value=data['status'])
|
|
|
|
embed.add_field(name='Followers', value=data['followers'])
|
|
|
|
embed.add_field(name='Views', value=data['views'])
|
|
|
|
if data['game']:
|
|
|
|
embed.add_field(name='Game', value=data['game'])
|
|
|
|
embed.add_field(name='Language', value=data['broadcaster_language'])
|
|
|
|
|
|
|
|
return embed
|
|
|
|
|
2017-06-28 12:13:14 +12:00
|
|
|
async def online_embed(self, twitch_url):
|
|
|
|
# First make sure the twitch URL is actually given
|
|
|
|
if not twitch_url:
|
|
|
|
return None
|
|
|
|
|
2016-09-21 10:21:48 +12:00
|
|
|
# Check a specific channel's data, and get the response in text format
|
2017-03-13 10:59:56 +13:00
|
|
|
channel = re.search("(?<=twitch.tv/)(.*)", twitch_url).group(1)
|
2016-10-02 09:29:53 +13:00
|
|
|
url = "https://api.twitch.tv/kraken/streams/{}".format(channel)
|
2017-01-09 09:03:22 +13:00
|
|
|
|
|
|
|
response = await utils.request(url, payload=self.params)
|
2016-09-24 17:54:30 +12:00
|
|
|
|
2016-09-21 10:21:48 +12:00
|
|
|
# For some reason Twitch's API call is not reliable, sometimes it returns stream as None
|
|
|
|
# That is what we're checking specifically, sometimes it doesn't exist in the returned JSON at all
|
2017-01-09 09:03:22 +13:00
|
|
|
# Sometimes it returns something that cannot be decoded with JSON (which means we'll get None back)
|
2016-09-21 10:21:48 +12:00
|
|
|
# In either error case, just assume they're offline, the next check will most likely work
|
|
|
|
try:
|
2017-06-18 11:04:36 +12:00
|
|
|
data = response['stream']['channel']
|
|
|
|
embed = self._form_embed(data)
|
|
|
|
return embed
|
2017-01-09 09:03:22 +13:00
|
|
|
except (KeyError, TypeError):
|
2017-06-18 11:04:36 +12:00
|
|
|
return None
|
|
|
|
|
2017-06-28 12:13:14 +12:00
|
|
|
async def offline_embed(self, twitch_url):
|
|
|
|
# First make sure the twitch URL is actually given
|
|
|
|
if not twitch_url:
|
|
|
|
return None
|
2017-06-18 11:04:36 +12:00
|
|
|
|
|
|
|
# Check a specific channel's data, and get the response in text format
|
|
|
|
channel = re.search("(?<=twitch.tv/)(.*)", twitch_url).group(1)
|
|
|
|
url = "https://api.twitch.tv/kraken/channels/{}".format(channel)
|
|
|
|
|
|
|
|
data = await utils.request(url, payload=self.params)
|
|
|
|
return self._form_embed(data)
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2016-08-15 14:10:12 +12:00
|
|
|
async def check_channels(self):
|
2016-07-13 02:44:17 +12:00
|
|
|
await self.bot.wait_until_ready()
|
2017-06-18 11:04:36 +12:00
|
|
|
# This is a loop that runs every 30 seconds, checking if anyone has gone online
|
2016-11-13 13:10:36 +13:00
|
|
|
try:
|
2017-03-13 11:05:47 +13:00
|
|
|
while not self.bot.is_closed():
|
2017-06-18 11:04:36 +12:00
|
|
|
twitch = await self.bot.db.actual_load('twitch', table_filter={'notifications_on': 1})
|
2017-03-13 10:59:56 +13:00
|
|
|
for data in twitch:
|
|
|
|
m_id = int(data['member_id'])
|
|
|
|
url = data['twitch_url']
|
2017-06-18 11:04:36 +12:00
|
|
|
# Check if they are online by trying to get an displayed embed for this user
|
|
|
|
embed = await self.online_embed(url)
|
|
|
|
# If they're currently online, but saved as not then we'll let servers know they are now online
|
|
|
|
if embed and data['live'] == 0:
|
|
|
|
msg = "{member.display_name} has just gone live!"
|
|
|
|
self.bot.db.save('twitch', {'live': 1, 'member_id': str(m_id)})
|
|
|
|
# Otherwise our notification will say they've gone offline
|
|
|
|
elif not embed and data['live'] == 1:
|
|
|
|
msg = "{member.display_name} has just gone offline!"
|
|
|
|
embed = await self.offline_embed(url)
|
|
|
|
self.bot.db.save('twitch', {'live': 0, 'member_id': str(m_id)})
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Loop through each server that they are set to notify
|
|
|
|
for s_id in data['servers']:
|
|
|
|
server = self.bot.get_guild(int(s_id))
|
|
|
|
# If we can't find it, ignore this one
|
|
|
|
if server is None:
|
|
|
|
continue
|
|
|
|
member = server.get_member(m_id)
|
|
|
|
# If we can't find them in this server, also ignore
|
|
|
|
if member is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Get the notifications settings, get the twitch setting
|
|
|
|
notifications = self.bot.db.load('server_settings', key=s_id, pluck='notifications') or {}
|
|
|
|
# Set our default to either the one set, or the default channel of the server
|
|
|
|
default_channel_id = notifications.get('default') or s_id
|
|
|
|
# If it is has been overriden by twitch notifications setting, use this
|
|
|
|
channel_id = notifications.get('twitch') or default_channel_id
|
|
|
|
# Now get the channel
|
|
|
|
channel = server.get_channel(int(channel_id))
|
|
|
|
# Unfortunately we need one more check, to ensure a channel hasn't been chosen, then deleted
|
|
|
|
if channel is None:
|
|
|
|
channel = server.default_channel
|
|
|
|
|
|
|
|
# Then just send our message
|
|
|
|
try:
|
|
|
|
await channel.send(msg.format(member=member), embed=embed)
|
|
|
|
except (discord.Forbidden, discord.HTTPException):
|
|
|
|
pass
|
|
|
|
|
2016-11-13 13:10:36 +13:00
|
|
|
await asyncio.sleep(30)
|
|
|
|
except Exception as e:
|
2016-11-13 17:06:02 +13:00
|
|
|
tb = traceback.format_exc()
|
2017-01-09 09:03:22 +13:00
|
|
|
fmt = "{1}\n{0.__class__.__name__}: {0}".format(tb, e)
|
2016-11-13 13:10:36 +13:00
|
|
|
log.error(fmt)
|
2016-07-13 02:44:17 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.group(invoke_without_command=True)
|
|
|
|
@commands.guild_only()
|
2017-01-09 09:03:22 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-06-28 12:26:32 +12:00
|
|
|
@utils.check_restricted()
|
2016-08-31 10:33:46 +12:00
|
|
|
async def twitch(self, ctx, *, member: discord.Member = None):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Use this command to check the twitch info of a user
|
|
|
|
|
|
|
|
EXAMPLE: !twitch @OtherPerson
|
|
|
|
RESULT: Information about their twitch URL"""
|
2017-03-10 17:37:43 +13:00
|
|
|
await ctx.message.channel.trigger_typing()
|
2017-03-13 10:46:43 +13:00
|
|
|
|
2016-07-25 03:33:46 +12:00
|
|
|
if member is None:
|
|
|
|
member = ctx.message.author
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2017-06-07 20:30:19 +12:00
|
|
|
url = self.bot.db.load('twitch', key=member.id, pluck='twitch_url')
|
|
|
|
if url is None:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("{} has not saved their twitch URL yet!".format(member.name))
|
2016-07-25 03:33:46 +12:00
|
|
|
return
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2017-06-18 11:04:36 +12:00
|
|
|
embed = await self.offline_embed(url)
|
2017-03-09 10:12:40 +13:00
|
|
|
await ctx.send(embed=embed)
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@twitch.command(name='add')
|
|
|
|
@commands.guild_only()
|
2017-01-09 09:03:22 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-06-28 12:26:32 +12:00
|
|
|
@utils.check_restricted()
|
2016-07-11 03:24:20 +12:00
|
|
|
async def add_twitch_url(self, ctx, url: str):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Saves your user's twitch URL
|
|
|
|
|
|
|
|
EXAMPLE: !twitch add MyTwitchName
|
|
|
|
RESULT: Saves your twitch URL; notifications will be sent to this server when you go live"""
|
2017-03-10 17:37:43 +13:00
|
|
|
await ctx.message.channel.trigger_typing()
|
|
|
|
|
2016-08-19 08:55:04 +12:00
|
|
|
# This uses a lookbehind to check if twitch.tv exists in the url given
|
|
|
|
# If it does, it matches twitch.tv/user and sets the url as that
|
|
|
|
# Then (in the else) add https://www. to that
|
|
|
|
# Otherwise if it doesn't match, we'll hit an AttributeError due to .group(0)
|
|
|
|
# This means that the url was just given as a user (or something complete invalid)
|
|
|
|
# So set URL as https://www.twitch.tv/[url]
|
|
|
|
# Even if this was invalid such as https://www.twitch.tv/google.com/
|
|
|
|
# For example, our next check handles that
|
2016-07-11 03:24:20 +12:00
|
|
|
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-10-02 09:29:53 +13: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-08-31 10:33:46 +12:00
|
|
|
|
2016-08-19 08:55:04 +12:00
|
|
|
# Try to find the channel provided, we'll get a 404 response if it does not exist
|
2017-03-08 11:35:30 +13:00
|
|
|
status = await utils.request(url, attr='status')
|
|
|
|
if not status == 200:
|
|
|
|
await ctx.send("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
|
|
|
|
2017-03-08 20:55:22 +13:00
|
|
|
key = str(ctx.message.author.id)
|
2017-06-07 20:30:19 +12:00
|
|
|
|
|
|
|
# Check if it exists first, if it does we don't want to override some of the settings
|
|
|
|
result = self.bot.db.load('twitch', key=key)
|
|
|
|
if result:
|
|
|
|
entry = {
|
|
|
|
'twitch_url': url,
|
|
|
|
'member_id': key
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
entry = {
|
|
|
|
'twitch_url': url,
|
|
|
|
'servers': [str(ctx.message.guild.id)],
|
|
|
|
'notifications_on': 1,
|
|
|
|
'live': 0,
|
|
|
|
'member_id': key
|
|
|
|
}
|
|
|
|
self.bot.db.save('twitch', entry)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I have just saved your twitch url {}".format(ctx.message.author.mention))
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@twitch.command(name='remove', aliases=['delete'])
|
|
|
|
@commands.guild_only()
|
2017-01-09 09:03:22 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-06-28 12:26:32 +12:00
|
|
|
@utils.check_restricted()
|
2016-07-11 06:35:02 +12:00
|
|
|
async def remove_twitch_url(self, ctx):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Removes your twitch URL
|
|
|
|
|
|
|
|
EXAMPLE: !twitch remove
|
|
|
|
RESULT: I stop saving your twitch URL"""
|
2017-06-07 20:30:19 +12:00
|
|
|
entry = {
|
|
|
|
'twitch_url': None,
|
|
|
|
'member_id': str(ctx.message.author.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.bot.db.save('twitch', entry)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I am no longer saving your twitch URL {}".format(ctx.message.author.mention))
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2017-06-18 11:04:36 +12:00
|
|
|
@twitch.command(name='alerts', aliases=['notifications'])
|
|
|
|
@commands.guild_only()
|
|
|
|
@utils.custom_perms(manage_guild=True)
|
2017-06-28 12:26:32 +12:00
|
|
|
@utils.check_restricted()
|
2017-06-18 11:04:36 +12:00
|
|
|
async def twitch_alerts_channel(self, ctx, channel: discord.TextChannel):
|
|
|
|
"""Sets the notifications channel for twitch notifications
|
|
|
|
|
|
|
|
EXAMPLE: !twitch alerts #twitch
|
|
|
|
RESULT: Twitch notifications will go to this channel
|
|
|
|
"""
|
|
|
|
entry = {
|
|
|
|
'server_id': str(ctx.message.guild.id),
|
|
|
|
'notifications': {
|
|
|
|
'twitch': str(channel.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.bot.db.save('server_settings', entry)
|
|
|
|
await ctx.send("All Twitch notifications will now go to {}".format(channel.mention))
|
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@twitch.group(invoke_without_command=True)
|
|
|
|
@commands.guild_only()
|
2017-01-09 09:03:22 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-06-28 12:26:32 +12:00
|
|
|
@utils.check_restricted()
|
2016-07-11 06:35:27 +12:00
|
|
|
async def notify(self, ctx):
|
2016-08-19 08:55:04 +12:00
|
|
|
"""This can be used to modify notification settings for your twitch user
|
2016-11-29 17:55:55 +13:00
|
|
|
Call this command by itself to add 'this' server as one that will be notified when you on/offline
|
|
|
|
|
|
|
|
EXAMPLE: !twitch notify
|
|
|
|
RESULT: This server will now be notified when you go live"""
|
2017-03-08 20:55:22 +13:00
|
|
|
key = str(ctx.message.author.id)
|
2017-06-07 20:30:19 +12:00
|
|
|
servers = self.bot.db.load('twitch', key=key, pluck='servers')
|
2016-08-19 08:55:04 +12:00
|
|
|
# Check if this user is saved at all
|
2017-06-07 20:30:19 +12:00
|
|
|
if servers is None:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send(
|
2016-08-19 08:55:04 +12:00
|
|
|
"I do not have your twitch URL added {}. You can save your twitch url with !twitch add".format(
|
|
|
|
ctx.message.author.mention))
|
2016-09-29 12:39:34 +13:00
|
|
|
# Then check if this server is already added as one to notify in
|
2017-06-07 20:30:19 +12:00
|
|
|
elif str(ctx.message.guild.id) in servers:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I am already set to notify in this server...")
|
2016-08-19 08:55:04 +12:00
|
|
|
else:
|
2017-06-07 20:30:19 +12:00
|
|
|
servers.append(str(ctx.message.guild.id))
|
|
|
|
entry = {
|
|
|
|
'member_id': key,
|
|
|
|
'servers': servers
|
|
|
|
}
|
|
|
|
self.bot.db.save('twitch', entry)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("This server will now be notified if you go live")
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@notify.command(name='on', aliases=['start,yes'])
|
|
|
|
@commands.guild_only()
|
2017-01-09 09:03:22 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-06-28 12:26:32 +12:00
|
|
|
@utils.check_restricted()
|
2016-07-11 06:35:02 +12:00
|
|
|
async def notify_on(self, ctx):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Turns twitch notifications on
|
|
|
|
|
|
|
|
EXAMPLE: !twitch notify on
|
|
|
|
RESULT: Notifications will be sent when you go live"""
|
2017-06-07 20:30:19 +12:00
|
|
|
key = str(ctx.message.author.id)
|
|
|
|
result = self.bot.db.load('twitch', key=key)
|
|
|
|
if result:
|
|
|
|
entry = {
|
|
|
|
'member_id': key,
|
|
|
|
'notifications_on': 1
|
|
|
|
}
|
|
|
|
self.bot.db.save('twitch', entry)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("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-09-29 12:39:34 +13:00
|
|
|
else:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I can't notify if you go live if I don't know your twitch URL yet!")
|
2016-07-11 09:57:52 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@notify.command(name='off', aliases=['stop,no'])
|
|
|
|
@commands.guild_only()
|
2017-01-09 09:03:22 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-06-28 12:26:32 +12:00
|
|
|
@utils.check_restricted()
|
2016-07-11 06:35:02 +12:00
|
|
|
async def notify_off(self, ctx):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Turns twitch notifications off
|
|
|
|
|
|
|
|
EXAMPLE: !twitch notify off
|
|
|
|
RESULT: Notifications will not be sent when you go live"""
|
2017-06-07 20:30:19 +12:00
|
|
|
key = str(ctx.message.author.id)
|
|
|
|
result = self.bot.db.load('twitch', key=key)
|
|
|
|
if result:
|
|
|
|
entry = {
|
|
|
|
'member_id': key,
|
|
|
|
'notifications_on': 0
|
|
|
|
}
|
|
|
|
self.bot.db.save('twitch', entry)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send(
|
2016-08-14 08:16:52 +12:00
|
|
|
"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))
|
2016-09-29 12:39:34 +13:00
|
|
|
else:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send(
|
2016-09-29 12:39:34 +13:00
|
|
|
"I mean, I'm already not going to notify anyone, because I don't have your twitch URL saved...")
|
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)
|
2016-09-21 10:21:48 +12:00
|
|
|
bot.loop.create_task(t.check_channels())
|
2016-07-11 03:24:20 +12:00
|
|
|
bot.add_cog(Twitch(bot))
|