WIP adding timezone to Cron triggers
This commit is contained in:
parent
d619c9a502
commit
d377461602
@ -1,8 +1,8 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, tzinfo
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from apscheduler.triggers.cron import CronTrigger
|
||||
from dateutil import parser
|
||||
from dateutil import parser, tz
|
||||
from discord.ext.commands import BadArgument, Converter
|
||||
|
||||
from fifo.timezones import assemble_timezones
|
||||
@ -11,6 +11,17 @@ if TYPE_CHECKING:
|
||||
DatetimeConverter = datetime
|
||||
CronConverter = str
|
||||
else:
|
||||
class TimezoneConverter(Converter):
|
||||
async def convert(self, ctx, argument) -> tzinfo:
|
||||
tzinfos = assemble_timezones()
|
||||
if argument.upper() in tzinfos:
|
||||
return tzinfos[argument.upper()]
|
||||
|
||||
timez = tz.gettz(argument)
|
||||
|
||||
if timez is not None:
|
||||
return timez
|
||||
raise BadArgument()
|
||||
|
||||
class DatetimeConverter(Converter):
|
||||
async def convert(self, ctx, argument) -> datetime:
|
||||
|
11
fifo/fifo.py
11
fifo/fifo.py
@ -11,7 +11,7 @@ from redbot.core import Config, checks, commands
|
||||
from redbot.core.bot import Red
|
||||
from redbot.core.commands import TimedeltaConverter
|
||||
|
||||
from .datetime_cron_converters import CronConverter, DatetimeConverter
|
||||
from .datetime_cron_converters import CronConverter, DatetimeConverter, TimezoneConverter
|
||||
from .task import Task
|
||||
|
||||
schedule_log = logging.getLogger("red.fox_v3.fifo.scheduler")
|
||||
@ -444,7 +444,12 @@ class FIFO(commands.Cog):
|
||||
|
||||
@fifo_trigger.command(name="cron")
|
||||
async def fifo_trigger_cron(
|
||||
self, ctx: commands.Context, task_name: str, *, cron_str: CronConverter
|
||||
self,
|
||||
ctx: commands.Context,
|
||||
task_name: str,
|
||||
optional_tz: Optional[TimezoneConverter] = None,
|
||||
*,
|
||||
cron_str: CronConverter,
|
||||
):
|
||||
"""
|
||||
Add a cron "time of day" trigger to the specified task
|
||||
@ -460,7 +465,7 @@ class FIFO(commands.Cog):
|
||||
)
|
||||
return
|
||||
|
||||
result = await task.add_trigger("cron", cron_str)
|
||||
result = await task.add_trigger("cron", cron_str, optional_tz)
|
||||
if not result:
|
||||
await ctx.maybe_send_embed(
|
||||
"Failed to add a cron trigger to this task, see console for logs"
|
||||
|
@ -300,7 +300,10 @@ class Task:
|
||||
self.data["command_str"] = command_str
|
||||
return True
|
||||
|
||||
async def add_trigger(self, param, parsed_time: Union[timedelta, datetime, str]):
|
||||
async def add_trigger(
|
||||
self, param, parsed_time: Union[timedelta, datetime, str], timezone=None
|
||||
):
|
||||
# TODO: Save timezone separately for cron and date triggers
|
||||
trigger_data = {"type": param, "time_data": parsed_time}
|
||||
if not get_trigger(trigger_data):
|
||||
return False
|
||||
|
Loading…
x
Reference in New Issue
Block a user