Skip to content

Commit

Permalink
Updated typing to fit apschedler v4
Browse files Browse the repository at this point in the history
  • Loading branch information
49Indium committed Jul 5, 2024
1 parent 696641b commit eea0fd5
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 49 deletions.
27 changes: 15 additions & 12 deletions uqcsbot/advent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import requests
from requests.exceptions import RequestException
from sqlalchemy.sql.expression import and_
from apscheduler.triggers.cron import CronTrigger

import discord
from discord import app_commands
Expand Down Expand Up @@ -69,7 +70,7 @@
member.times[day].get(2, MAXIMUM_TIME_FOR_STAR),
member.times[day].get(1, MAXIMUM_TIME_FOR_STAR),
),
"Total Time": lambda member, dat: (
"Total Time": lambda member, day: (
member.get_total_time(default=MAXIMUM_TIME_FOR_STAR),
-member.star_total,
),
Expand Down Expand Up @@ -171,20 +172,22 @@ def __init__(self, bot: UQCSBot):
self.bot = bot
self.bot.schedule_task(
self.reminder_released,
trigger="cron",
timezone="Australia/Brisbane",
hour=15,
day="1-25",
month=12,
trigger=CronTrigger(
timezone=bot.BOT_TIMEZONE,
hour=15,
day="1-25",
month=12,
),
)
self.bot.schedule_task(
self.reminder_fifteen_minutes,
trigger="cron",
timezone="Australia/Brisbane",
hour=14,
minute=45,
day="1-25",
month=12,
trigger=CronTrigger(
timezone=bot.BOT_TIMEZONE,
hour=14,
minute=45,
day="1-25",
month=12,
),
)

# A dictionary from a year to the list of members
Expand Down
21 changes: 13 additions & 8 deletions uqcsbot/bot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import logging
import os
from typing import List, Optional, Tuple, Any, Callable, Coroutine
from typing import List, Optional, Tuple, Any, Callable

import discord
from discord.ext import commands
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler import AsyncScheduler
from apscheduler.abc import Trigger
from asyncio import run
from sqlalchemy.engine import Engine
from sqlalchemy.orm import sessionmaker
from datetime import datetime
Expand All @@ -23,7 +25,7 @@ class UQCSBot(commands.Bot):

def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
self._scheduler = AsyncIOScheduler()
self._scheduler = AsyncScheduler()
self.start_time = datetime.now()

# Important channel names & constants go here
Expand All @@ -36,10 +38,13 @@ def __init__(self, *args: Any, **kwargs: Any):
self.uqcs_server: discord.Guild

def schedule_task(
self, func: Callable[..., Coroutine[Any, Any, None]], *args: Any, **kwargs: Any
self, func: Callable[..., Any], trigger: Trigger, *args: Any, **kwargs: Any
):
"""Schedule a function to be run at a later time. A wrapper for apscheduler add_job."""
self._scheduler.add_job(func, *args, **kwargs)
"""Schedule a function to be run at a later time. A wrapper for apscheduler add_schedule."""
# The apschedule library has an `Unknown` type within their definition of the type of `add_schedule`
run(
self._scheduler.add_schedule(func, trigger, *args, **kwargs)
) # pyright: ignore[reportUnknownMemberType]

def set_db_engine(self, db_engine: Engine):
"""Creates a sessionmaker from the provided database engine which can be called from commands."""
Expand Down Expand Up @@ -81,7 +86,7 @@ async def admin_alert(
# Web server binds to port 8080. This is a basic template to ensure
# that Azure has something for a health check.
async def web_server(self):
def handle(request):
async def handle(_: web.Request):
return web.Response(text="UQCSbot is running")

app = web.Application()
Expand All @@ -93,7 +98,7 @@ def handle(request):

async def on_ready(self):
"""Once the bot is loaded and has connected, run these commands first."""
self._scheduler.start()
await self._scheduler.start_in_background()

if (user := self.user) is None:
raise RuntimeError("Ready... but not logged in!")
Expand Down
10 changes: 6 additions & 4 deletions uqcsbot/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from calendar import day_abbr, month_abbr, month_name
from datetime import date, datetime, timedelta
from typing import List, Optional, Tuple, Dict
from apscheduler.triggers.cron import CronTrigger

import discord
from discord import app_commands
Expand Down Expand Up @@ -195,10 +196,11 @@ def __init__(self, bot: UQCSBot):
self.bot = bot
self.bot.schedule_task(
self.scheduled_message,
trigger="cron",
hour=9,
day_of_week="mon",
timezone="Australia/Brisbane",
trigger=CronTrigger(
hour=9,
day_of_week="mon",
timezone="Australia/Brisbane",
),
)

async def scheduled_message(self):
Expand Down
9 changes: 5 additions & 4 deletions uqcsbot/holidays.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from apscheduler.triggers.cron import CronTrigger
from bs4 import BeautifulSoup
import csv
from datetime import datetime
Expand Down Expand Up @@ -114,10 +115,10 @@ def __init__(self, bot: UQCSBot):
self.bot = bot
self.bot.schedule_task(
self.holiday,
trigger="cron",
hour=9,
minute=0,
timezone="Australia/Brisbane",
trigger=CronTrigger(
hour=9,
timezone=bot.BOT_TIMEZONE,
),
)

async def holiday(self):
Expand Down
40 changes: 21 additions & 19 deletions uqcsbot/remindme.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import datetime as dt
from apscheduler.triggers.cron import CronTrigger
import discord
from discord import app_commands
from discord.ext import commands
from functools import partial
import logging
from typing import List, NamedTuple, Optional, Union
from zoneinfo import ZoneInfo
from asyncio import run

from uqcsbot.bot import UQCSBot
from uqcsbot.models import Reminders
Expand Down Expand Up @@ -257,24 +259,23 @@ def _schedule_reminder(self, reminder: Reminder):
and end_datetime != None
and end_datetime < dt.datetime.now()
):
self.bot.schedule_task(
partial(self._process_reminder, reminder), misfire_grace_time=None
)
run(self._process_reminder(reminder))

# otherwise, reminder datetime is in the future so we can schedule it
if reminder.week_frequency == None or start_datetime > dt.datetime.now():
# one-time reminder OR first occurrence of recurring reminder, so schedule for start_date
return self.bot.schedule_task(
partial(self._process_reminder, reminder),
trigger="cron",
timezone="Australia/Brisbane",
trigger=CronTrigger(
timezone=self.bot.BOT_TIMEZONE,
year=start_date.year,
month=start_date.month,
day=start_date.day,
hour=time.hour,
minute=time.minute,
second=time.second,
),
misfire_grace_time=None,
year=start_date.year,
month=start_date.month,
day=start_date.day,
hour=time.hour,
minute=time.minute,
second=time.second,
)

# non-first occurrence of recurring reminder, schedule next occurrence based on week_frequency
Expand Down Expand Up @@ -305,15 +306,16 @@ def _schedule_reminder(self, reminder: Reminder):

self.bot.schedule_task(
partial(self._process_reminder, reminder),
trigger="cron",
timezone="Australia/Brisbane",
trigger=CronTrigger(
timezone=self.bot.BOT_TIMEZONE,
year=year,
month=month,
day=day,
hour=time.hour,
minute=time.minute,
second=time.second,
),
misfire_grace_time=None,
year=year,
month=month,
day=day,
hour=time.hour,
minute=time.minute,
second=time.second,
)

async def _process_reminder(self, reminder: Reminder):
Expand Down
3 changes: 2 additions & 1 deletion uqcsbot/working_on.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from random import choice
from apscheduler.triggers.cron import CronTrigger

import discord
from discord.ext import commands
Expand All @@ -13,7 +14,7 @@ class WorkingOn(commands.Cog):
def __init__(self, bot: UQCSBot):
self.bot = bot
self.bot.schedule_task(
self.workingon, trigger="cron", hour=17, timezone="Australia/Brisbane"
self.workingon, trigger=CronTrigger(hour=17, timezone=bot.BOT_TIMEZONE)
)

async def workingon(self):
Expand Down
3 changes: 2 additions & 1 deletion uqcsbot/yelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from datetime import timedelta
from functools import wraps
from apscheduler.triggers.cron import CronTrigger


def yelling_exemptor(input_args: List[str] = ["text"]) -> Callable[..., Any]:
Expand Down Expand Up @@ -64,7 +65,7 @@ class Yelling(commands.Cog):
def __init__(self, bot: UQCSBot):
self.bot = bot
self.bot.schedule_task(
self.clear_bans, trigger="cron", hour=17, timezone="Australia/Brisbane"
self.clear_bans, trigger=CronTrigger(hour=17, timezone=bot.BOT_TIMEZONE)
)

@commands.Cog.listener()
Expand Down

0 comments on commit eea0fd5

Please sign in to comment.