Skip to content

Commit

Permalink
Add event Discord notifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsCalebJones committed Jul 15, 2019
1 parent 7bed17f commit 701a453
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 8 deletions.
57 changes: 57 additions & 0 deletions bot/cogs/launches.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,62 @@ def launch_to_small_embed(launch, notification="", pre_launch=False):
return embed


def event_to_embed(event, notification="", pre_launch=False):
title = "%s" % event.name
color = Colour.teal()
status = "**Type:** %s\n" % event.type.name
location = "**Location:** %s\n" % event.location
follow_along = "\nFollow along on [Android](https://play.google.com/store/apps/details?id=me.calebjones." \
"spacelaunchnow&pcampaignid=MKT-Other-global-all-co-prtnr-py-PartBadge-Mar2515-1)," \
" [iOS](https://itunes.apple.com/us/app/space-launch-now/id1399715731)" \
" or [on the web](https://spacelaunchnow.me)"
event_description = ""
if event.description is not None:
event_description = "\n%s\n" % (event.mission.description[:75] + '...') if len(event.mission.description) > 75 else event.mission.description

formatted_countdown = ''

if pre_launch:
countdown = event.date - datetime.datetime.now(pytz.utc)
seconds = countdown.total_seconds()
days, remainder = divmod(seconds, 86400)
hours, remainder = divmod(remainder, 3600)
minutes, seconds = divmod(remainder, 60)
if days != 0:
formatted_countdown += str(int(days)) + ' Days '
if hours != 0:
if hours == 23:
hours = 24
formatted_countdown += str(int(hours)) + ' Hours '
if minutes != 0:
if minutes == 59:
minutes = 00
formatted_countdown += str(int(minutes)) + ' Minutes '

formatted_countdown = '\n**Event In ' + formatted_countdown + '**'

webcasts = ""
if event.video_url is not None :
webcasts = "\n**Watch Here:**\n"
webcasts = webcasts + "[%s](%s)\n" % (event.video_url, event.video_url)

description_text = notification + status + location + event_description + formatted_countdown + webcasts + follow_along

embed = discord.Embed(type="rich", title=title,
description=description_text,
color=color,
url=event.get_full_absolute_url())

if event.feature_image.name is not '':
try:
embed.set_thumbnail(url=event.feature_image.url)
except ValueError:
embed.set_thumbnail(url="https://daszojo4xmsc6.cloudfront.net/static/home/img/launcher.png")
else:
embed.set_thumbnail(url="https://daszojo4xmsc6.cloudfront.net/static/home/img/launcher.png")
embed.set_footer(text=event.net.strftime("Date: %A %B %e, %Y %H:%M %Z"))
return embed


def setup(bot):
bot.add_cog(Launches(bot))
72 changes: 64 additions & 8 deletions bot/cogs/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from django.db.models import Q
from django.template import defaultfilters

from api.models import Launch
from bot.cogs.launches import launch_to_small_embed
from api.models import Launch, Events
from bot.cogs.launches import launch_to_small_embed, event_to_embed
from bot.models import DiscordChannel, Notification

logger = logging.getLogger('bot.discord.notifications')
Expand Down Expand Up @@ -110,7 +110,8 @@ async def check_success(self, bot_channels, time_threshold_past_two_days, time_t
try:
await self.bot.send_message(channel,
embed=launch_to_small_embed(launch,
"**Launch was a %s!**\n\n" % launch.status.name, pre_launch=False))
"**Launch was a %s!**\n\n" % launch.status.name,
pre_launch=False))
except Exception as e:
logger.error(channel.id)
logger.error(channel.name)
Expand All @@ -133,7 +134,8 @@ async def check_in_flight(self, bot_channels):
logger.info("Sending notification to %s" % channel.name)
try:
await self.bot.send_message(channel,
embed=launch_to_small_embed(launch, "**Launch is in flight!**\n\n", pre_launch=False))
embed=launch_to_small_embed(launch, "**Launch is in flight!**\n\n",
pre_launch=False))
except Exception as e:
logger.error(channel.id)
logger.error(channel.name)
Expand All @@ -157,7 +159,8 @@ async def check_one_minute(self, bot_channels, time_threshold_1_minute):
logger.info("Sending notification to %s" % channel.name)
try:
await self.bot.send_message(channel,
embed=launch_to_small_embed(launch, "**Launching in one minute!**\n\n"))
embed=launch_to_small_embed(launch,
"**Launching in one minute!**\n\n"))
except Exception as e:
logger.error(channel.id)
logger.error(channel.name)
Expand Down Expand Up @@ -230,7 +233,8 @@ async def check_one_hour(self, bot_channels, time_threshold_10_minute, time_thre
logger.info("Sending notification to %s" % channel.name)
try:
await self.bot.send_message(channel,
embed=launch_to_small_embed(launch, "**Launching in one hour!**\n\n"))
embed=launch_to_small_embed(launch,
"**Launching in one hour!**\n\n"))
except Exception as e:
logger.error(channel.id)
logger.error(channel.name)
Expand All @@ -241,7 +245,8 @@ async def check_one_hour(self, bot_channels, time_threshold_10_minute, time_thre

async def check_webcast_live(self, bot_channels, time_threshold_1_hour, time_threshold_1_minute):
logger.debug("Checking webcast live launches...")
one_hour_launches = Launch.objects.filter(net__gte=time_threshold_1_minute, net__lte=time_threshold_1_hour, webcast_live=True)
one_hour_launches = Launch.objects.filter(net__gte=time_threshold_1_minute, net__lte=time_threshold_1_hour,
webcast_live=True)
for launch in one_hour_launches:
logger.debug("Found %s launches with a live webcast." % len(one_hour_launches))
notification, created = Notification.objects.get_or_create(launch=launch)
Expand All @@ -252,7 +257,54 @@ async def check_webcast_live(self, bot_channels, time_threshold_1_hour, time_thr
for channel in bot_channels:
logger.info("Sending notification to %s" % channel.name)
try:
await self.bot.send_message(channel, embed=launch_to_small_embed(launch, "**Webcast is live!**\n\n"))
await self.bot.send_message(channel,
embed=event_to_embed(launch, "**Webcast is live!**\n\n"))
except Exception as e:
logger.error(channel.id)
logger.error(channel.name)
logger.error(e)
if 'Missing Permissions' in e.args or 'Received NoneType' in e.args:
channel.delete()
return

async def check_webcast_live_event(self, bot_channels, time_threshold_1_hour, time_threshold_1_minute):
logger.debug("Checking webcast live launches...")
events = Events.objects.filter(date__gte=time_threshold_1_minute, date__lte=time_threshold_1_hour,
webcast_live=True)
for event in events:
logger.debug("Found %s events with a live webcast." % len(events))
if not event.was_discorded_webcast_live:
event.was_discorded_webcast_live = True
event.save()
logger.info("Webcast Live - Event Notification for %s" % event.name)
for channel in bot_channels:
logger.info("Sending notification to %s" % channel.name)
try:
await self.bot.send_message(channel, embed=event_to_embed(event, "**Webcast is live!**\n\n"))
except Exception as e:
logger.error(channel.id)
logger.error(channel.name)
logger.error(e)
if 'Missing Permissions' in e.args or 'Received NoneType' in e.args:
channel.delete()
return

async def check_ten_minute_event(self, bot_channels, time_threshold_10_minute, time_threshold_1_minute):
logger.debug("Checking ten-minute events...")
events = Events.objects.filter(date__lte=time_threshold_10_minute,
date__gte=time_threshold_1_minute)
for event in events:
logger.debug("Found %s events in the next ten minutes." % len(events))
if not event.was_discorded_ten_minutes:
event.was_discorded_ten_minutes = True
event.save()
logger.info("Ten Minutes - Event Notification for %s" % event.name)
for channel in bot_channels:
logger.info("Sending notification to %s" % channel.name)
try:
await self.bot.send_message(channel,
embed=event_to_embed(event,
"**Launching in ten minutes!**\n\n"))
except Exception as e:
logger.error(channel.id)
logger.error(channel.name)
Expand Down Expand Up @@ -294,6 +346,10 @@ async def discord_launch_events(self):

await self.check_success(bot_channels, time_threshold_past_two_days, time_threshold_24_hour)

await self.check_ten_minute_event(bot_channels, time_threshold_10_minute, time_threshold_1_minute)

await self.check_webcast_live_event(bot_channels, time_threshold_1_hour, time_threshold_1_minute)

await self.set_bot_description()

logger.debug("Completed.")
Expand Down

0 comments on commit 701a453

Please sign in to comment.