2016-08-08 08:20:43 +12:00
|
|
|
import asyncio
|
2016-08-09 09:27:46 +12:00
|
|
|
import discord
|
2016-11-13 13:22:43 +13:00
|
|
|
import traceback
|
2016-08-08 08:20:43 +12:00
|
|
|
|
2018-10-30 14:00:37 +13:00
|
|
|
import utils
|
2016-08-08 08:20:43 +12:00
|
|
|
|
2017-06-12 17:46:07 +12:00
|
|
|
BASE_URL = 'https://api.picarto.tv/v1'
|
2016-08-08 08:20:43 +12:00
|
|
|
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2019-01-28 15:58:39 +13:00
|
|
|
def produce_embed(*channels):
|
|
|
|
description = ""
|
|
|
|
# Loop through each channel and produce the information that will go in the description
|
|
|
|
for channel in channels:
|
|
|
|
url = f"https://picarto.tv/{channel.get('name')}"
|
|
|
|
description = f"""{description}\n\n**Title:** [{channel.get("title")}]({url})
|
|
|
|
**Channel:** [{channel.get("name")}]({url})
|
|
|
|
**Adult:** {"Yes" if channel.get("adult") else "No"}
|
|
|
|
**Gaming:** {"Yes" if channel.get("gaming") else "No"}
|
|
|
|
**Commissions:** {"Yes" if channel.get("commissions") else "No"}"""
|
|
|
|
|
|
|
|
return discord.Embed(title="Channels that have gone online!", description=description.strip())
|
|
|
|
|
|
|
|
|
2016-08-08 08:20:43 +12:00
|
|
|
class Picarto:
|
2018-10-09 09:40:21 +13:00
|
|
|
"""Pretty self-explanatory"""
|
|
|
|
|
2016-08-08 08:20:43 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2018-09-23 04:17:11 +12:00
|
|
|
self.task = self.bot.loop.create_task(self.picarto_task())
|
2019-01-28 15:58:39 +13:00
|
|
|
self.channel_info = {}
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2017-06-12 17:46:07 +12:00
|
|
|
# noinspection PyAttributeOutsideInit
|
2017-03-13 15:51:00 +13:00
|
|
|
async def get_online_users(self):
|
|
|
|
# This method is in place to just return all online users so we can compare against it
|
2017-06-12 17:46:07 +12:00
|
|
|
url = BASE_URL + '/online'
|
|
|
|
payload = {
|
2017-06-12 18:41:10 +12:00
|
|
|
'adult': 'true',
|
|
|
|
'gaming': 'true'
|
2017-06-12 17:46:07 +12:00
|
|
|
}
|
2019-01-28 15:58:39 +13:00
|
|
|
channel_info = {}
|
|
|
|
channels = await utils.request(url, payload=payload)
|
|
|
|
if channels:
|
|
|
|
for channel in channels:
|
|
|
|
name = channel["name"]
|
|
|
|
previous = self.channel_info.get("name")
|
|
|
|
# There are three statuses, on, remained, and off
|
|
|
|
# On means they were off previously, but are now online
|
|
|
|
# Remained means they were on previous, and are still on
|
|
|
|
# Off means they were on preivous, but are now offline
|
|
|
|
# If they weren't included in the online channels...well they're off
|
|
|
|
if previous is None:
|
|
|
|
channel_info[name] = channel
|
|
|
|
channel_info[name]["status"] = "on"
|
|
|
|
elif previous["status"] in ["on", "remaining"]:
|
|
|
|
channel_info[name] = channel
|
|
|
|
channel_info[name]["status"] = "remaining"
|
|
|
|
# After loop has finished successfully, we want to override the statuses of the channels
|
|
|
|
self.channel_info = channel_info
|
|
|
|
|
|
|
|
def produce_embed(self, *channels):
|
|
|
|
description = ""
|
|
|
|
# Loop through each channel and produce the information that will go in the description
|
|
|
|
for channel in channels:
|
|
|
|
url = f"https://picarto.tv/{channel.get('name')}"
|
|
|
|
description = f"""{description}\n\n**Title:** [{channel.get("title")}]({url})
|
|
|
|
**Channel:** [{channel.get("name")}]({url})
|
|
|
|
**Adult:** {"Yes" if channel.get("adult") else "No"}
|
|
|
|
**Gaming:** {"Yes" if channel.get("gaming") else "No"}
|
|
|
|
**Commissions:** {"Yes" if channel.get("commissions") else "No"}"""
|
|
|
|
|
|
|
|
return discord.Embed(title="Channels that have gone online!", description=description.strip())
|
2017-03-13 15:51:00 +13:00
|
|
|
|
2018-09-23 04:17:11 +12:00
|
|
|
async def picarto_task(self):
|
2016-11-13 13:10:36 +13:00
|
|
|
try:
|
2018-09-23 04:17:11 +12:00
|
|
|
await self.bot.wait_until_ready()
|
2017-03-13 15:51:00 +13:00
|
|
|
while not self.bot.is_closed():
|
2018-09-23 04:17:11 +12:00
|
|
|
await self.check_channels()
|
|
|
|
except Exception as error:
|
|
|
|
with open("error_log", 'a') as f:
|
|
|
|
traceback.print_tb(error.__traceback__, file=f)
|
|
|
|
print('{0.__class__.__name__}: {0}'.format(error), file=f)
|
|
|
|
finally:
|
|
|
|
await asyncio.sleep(30)
|
|
|
|
|
|
|
|
async def check_channels(self):
|
2019-01-28 15:58:39 +13:00
|
|
|
query = """
|
|
|
|
SELECT
|
|
|
|
id, followed_picarto_channels, COALESCE(picarto_alerts, default_alerts) AS channel
|
|
|
|
FROM
|
|
|
|
guilds
|
|
|
|
WHERE
|
|
|
|
COALESCE(picarto_alerts, default_alerts) IS NOT NULL
|
|
|
|
"""
|
|
|
|
# Recheck who is currently online
|
2018-09-23 04:17:11 +12:00
|
|
|
await self.get_online_users()
|
2019-01-28 15:58:39 +13:00
|
|
|
# Now get all guilds and their picarto channels they follow and loop through them
|
|
|
|
results = await self.bot.db.fetch(query) or []
|
|
|
|
for result in results:
|
|
|
|
# Get all the channels that have gone online
|
|
|
|
gone_online = [
|
|
|
|
self.channel_info.get(name)
|
|
|
|
for name in result["followed_picarto_channels"]
|
|
|
|
if self.channel_info.get(name) == "on"
|
|
|
|
]
|
|
|
|
# If they've gone online, produce the embed for them and send it
|
|
|
|
if gone_online:
|
|
|
|
embed = produce_embed(*gone_online)
|
|
|
|
channel = self.bot.get_channel(result["channel"])
|
|
|
|
if channel is not None:
|
|
|
|
try:
|
|
|
|
await channel.send(embed=embed)
|
|
|
|
except (discord.Forbidden, discord.HTTPException, AttributeError):
|
|
|
|
pass
|
2016-08-09 11:32:33 +12:00
|
|
|
|
|
|
|
|
2016-08-08 08:20:43 +12:00
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Picarto(bot))
|