From 384bd3d241904ad35f3cb82d209c29904f8fc3e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Sat, 21 Nov 2020 16:15:56 +0100 Subject: [PATCH] Add support for timezones --- main.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index e52e1ed..d1ee323 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore from apscheduler.schedulers.asyncio import AsyncIOScheduler from discord.ext import commands from dotenv import load_dotenv +from pytz import timezone intents = discord.Intents.default() intents.members = True @@ -23,10 +24,7 @@ logging.basicConfig(level=logging.DEBUG) @bot.event async def on_ready(): - print("Logged in as") - print(bot.user.name) - print(bot.user.id) - print("------") + print(f"Logged in as {bot.user.name} ({bot.user.id})") @bot.command(aliases=["reminder"]) @@ -34,30 +32,33 @@ async def remind(ctx, message_date: str, message_reason: str): print("remind - ---------------------") print(f"remind - Message: {ctx.message}") - parsed_date = dateparser.parse(f"{message_date}") - formatted_date = parsed_date.strftime("%Y-%m-%d %H:%M:%S") + parsed_date = dateparser.parse( + f"{message_date}", + settings={"PREFER_DATES_FROM": "future"}, + ) + convert_date_to_our_timezone = parsed_date.astimezone(timezone(config_timezone)) + remove_timezone_from_date = convert_date_to_our_timezone.strftime( + "%Y-%m-%d %H:%M:%S" + ) print(f"remind - Date from command: {message_date}") print(f"remind - Reason from command: {message_reason}") print(f"remind - Parsed Date: {parsed_date}") - print(f"remind - Formatted date: {formatted_date}") - - print("remind - Creating webhook") + print(f"remind - Converted date: {convert_date_to_our_timezone}") + print(f"remind - Date without timezone: {remove_timezone_from_date}") print(f"remind - Channel ID: {ctx.channel.id}") print(f"remind - Channel name: {ctx.channel.name}") job = scheduler.add_job( send_to_discord, - run_date=formatted_date, + run_date=remove_timezone_from_date, kwargs={ "webhook_url": webhook_url, "message": message_reason, }, ) print(f"remind - Id: {job.id}, Name: {job.name}, kwargs: {job.kwargs}") - message = ( - f"I will notify you at `{formatted_date}` with the message `{message_reason}`." - ) + message = f"I will notify you at `{remove_timezone_from_date}` with the message `{message_reason}`." print(f"remind - Message we sent back to user in Discord: {message}") await ctx.send(message) @@ -72,12 +73,10 @@ async def send_to_discord(webhook_url, message): if __name__ == "__main__": # Enviroment variables load_dotenv(verbose=True) - webhook_url = os.getenv( - "WEBHOOK_URL", default="value does not exist" - ) # TODO: Read this directly from the server + webhook_url = os.getenv("WEBHOOK_URL") # TODO: Read this directly from the server sqlite_location = os.getenv("SQLITE_LOCATION", default="/jobs.sqlite") - scheduler_timezone = os.getenv("TIMEZONE", default="Europe/Stockholm") - bot_token = os.getenv("BOT_TOKEN", default="value does not exist") + config_timezone = os.getenv("TIMEZONE", default="Europe/Stockholm") + bot_token = os.getenv("BOT_TOKEN") log_level = os.getenv(key="LOG_LEVEL", default="INFO") # Advanced Python Scheduler @@ -85,7 +84,7 @@ if __name__ == "__main__": job_defaults = {"coalesce": True} scheduler = AsyncIOScheduler( jobstores=jobstores, - timezone=pytz.timezone(scheduler_timezone), + timezone=pytz.timezone(config_timezone), job_defaults=job_defaults, )