Skip to content

Commit

Permalink
feat: sync topics to DB daily (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria authored Mar 14, 2021
1 parent fa59060 commit f975335
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
20 changes: 20 additions & 0 deletions bot/exts/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import random

import discord
from aiohttp import web
from discord.ext.commands import Bot
from discord.ext.commands import Cog
Expand All @@ -11,18 +12,25 @@

from bot import settings
from bot.database import store
from bot.utils.datetimes import EASTERN
from bot.utils.datetimes import utcnow
from bot.utils.gsheets import get_gsheet_client

logger = logging.getLogger(__name__)

# -----------------------------------------------------------------------------

DAILY_SYNC_TIME = dt.time(7, 0) # Eastern time


class Topics(Cog):
def __init__(self, bot: Bot):
self.bot = bot

@Cog.listener()
async def on_ready(self):
self.bot.loop.create_task(self.daily_sync())

@command(
name="synctopics",
aliases=("st",),
Expand All @@ -34,6 +42,18 @@ async def sync_topics_command(self, ctx: Context):
topics = await sync_topics()
await ctx.reply(f"✅ Synced {len(topics)} topics.")

async def daily_sync(self):
while True:
now_eastern = dt.datetime.now(EASTERN)
date = now_eastern.date()
if now_eastern.time() > DAILY_SYNC_TIME:
date = now_eastern.date() + dt.timedelta(days=1)
then = EASTERN.localize(dt.datetime.combine(date, DAILY_SYNC_TIME))
logger.info(f"topics will be synced at at {then.isoformat()}")
await discord.utils.sleep_until(then.astimezone(dt.timezone.utc))
topics = await sync_topics()
logger.info(f"synced {len(topics)} topics")


# -----------------------------------------------------------------------------

Expand Down
11 changes: 2 additions & 9 deletions tests/bot/exts/__snapshots__/test_practice.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,9 @@
author=EmbedProxy(),
color=<Colour value=15105570>,
colour=<Colour value=15105570>,
description='
Today - Thursday, November 26 🦃

*There are no scheduled practices today!*

To schedule a practice, edit the schedule below or use the `!practice` command.
Example: `!practice today 2pm pst`

',
description='Today - Thursday, November 26 🦃',
fields=<class 'list'> [
EmbedProxy(inline=False, name='11 AM PST / 12 PM MST / 1 PM CST / 2 PM EST', value='[Add to Google Calendar](http://www.google.com/calendar/event?action=TEMPLATE&text=ASL+Practice%3A+Turkey+day+PRACTICE&dates=20201126T190000Z%2F20201126T200000Z&details=See+the+full+schedule+here%3A+https%3A%2F%2Fdocs.google.com%2Fspreadsheets%2Fd%2Fabc%2Fedit)\n> Notes: Turkey day PRACTICE'),
EmbedProxy(inline=True, name='🗓 View or edit the schedule using the link below.', value='[Full schedule](https://docs.google.com/spreadsheets/d/abc/edit)'),
],
footer=EmbedProxy(),
Expand Down
1 change: 1 addition & 0 deletions tests/bot/exts/test_practice.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async def mock_worksheet(monkeypatch, db):
["Wed 6pm edt", "", "another recurring", ""],
["9/26 2pm PDT 2020", "Steve", "one time", ""],
["Sunday, September 27 02:00 PM EDT 2020", "", "another 1️⃣", ""],
["Nov 26 02:00 PM EDT 2020", "", "Turkey day PRACTICE", ""],
]
mock_get_worksheet.return_value = mock_get_worksheet2.return_value = WorksheetMock
yield WorksheetMock
Expand Down

0 comments on commit f975335

Please sign in to comment.