Add support for timezones
This commit is contained in:
37
main.py
37
main.py
@ -9,6 +9,7 @@ from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
|
|||||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
from pytz import timezone
|
||||||
|
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
intents.members = True
|
intents.members = True
|
||||||
@ -23,10 +24,7 @@ logging.basicConfig(level=logging.DEBUG)
|
|||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print("Logged in as")
|
print(f"Logged in as {bot.user.name} ({bot.user.id})")
|
||||||
print(bot.user.name)
|
|
||||||
print(bot.user.id)
|
|
||||||
print("------")
|
|
||||||
|
|
||||||
|
|
||||||
@bot.command(aliases=["reminder"])
|
@bot.command(aliases=["reminder"])
|
||||||
@ -34,30 +32,33 @@ async def remind(ctx, message_date: str, message_reason: str):
|
|||||||
print("remind - ---------------------")
|
print("remind - ---------------------")
|
||||||
print(f"remind - Message: {ctx.message}")
|
print(f"remind - Message: {ctx.message}")
|
||||||
|
|
||||||
parsed_date = dateparser.parse(f"{message_date}")
|
parsed_date = dateparser.parse(
|
||||||
formatted_date = parsed_date.strftime("%Y-%m-%d %H:%M:%S")
|
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 - Date from command: {message_date}")
|
||||||
print(f"remind - Reason from command: {message_reason}")
|
print(f"remind - Reason from command: {message_reason}")
|
||||||
print(f"remind - Parsed Date: {parsed_date}")
|
print(f"remind - Parsed Date: {parsed_date}")
|
||||||
print(f"remind - Formatted date: {formatted_date}")
|
print(f"remind - Converted date: {convert_date_to_our_timezone}")
|
||||||
|
print(f"remind - Date without timezone: {remove_timezone_from_date}")
|
||||||
print("remind - Creating webhook")
|
|
||||||
print(f"remind - Channel ID: {ctx.channel.id}")
|
print(f"remind - Channel ID: {ctx.channel.id}")
|
||||||
print(f"remind - Channel name: {ctx.channel.name}")
|
print(f"remind - Channel name: {ctx.channel.name}")
|
||||||
|
|
||||||
job = scheduler.add_job(
|
job = scheduler.add_job(
|
||||||
send_to_discord,
|
send_to_discord,
|
||||||
run_date=formatted_date,
|
run_date=remove_timezone_from_date,
|
||||||
kwargs={
|
kwargs={
|
||||||
"webhook_url": webhook_url,
|
"webhook_url": webhook_url,
|
||||||
"message": message_reason,
|
"message": message_reason,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
print(f"remind - Id: {job.id}, Name: {job.name}, kwargs: {job.kwargs}")
|
print(f"remind - Id: {job.id}, Name: {job.name}, kwargs: {job.kwargs}")
|
||||||
message = (
|
message = f"I will notify you at `{remove_timezone_from_date}` with the message `{message_reason}`."
|
||||||
f"I will notify you at `{formatted_date}` with the message `{message_reason}`."
|
|
||||||
)
|
|
||||||
print(f"remind - Message we sent back to user in Discord: {message}")
|
print(f"remind - Message we sent back to user in Discord: {message}")
|
||||||
await ctx.send(message)
|
await ctx.send(message)
|
||||||
|
|
||||||
@ -72,12 +73,10 @@ async def send_to_discord(webhook_url, message):
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Enviroment variables
|
# Enviroment variables
|
||||||
load_dotenv(verbose=True)
|
load_dotenv(verbose=True)
|
||||||
webhook_url = os.getenv(
|
webhook_url = os.getenv("WEBHOOK_URL") # TODO: Read this directly from the server
|
||||||
"WEBHOOK_URL", default="value does not exist"
|
|
||||||
) # TODO: Read this directly from the server
|
|
||||||
sqlite_location = os.getenv("SQLITE_LOCATION", default="/jobs.sqlite")
|
sqlite_location = os.getenv("SQLITE_LOCATION", default="/jobs.sqlite")
|
||||||
scheduler_timezone = os.getenv("TIMEZONE", default="Europe/Stockholm")
|
config_timezone = os.getenv("TIMEZONE", default="Europe/Stockholm")
|
||||||
bot_token = os.getenv("BOT_TOKEN", default="value does not exist")
|
bot_token = os.getenv("BOT_TOKEN")
|
||||||
log_level = os.getenv(key="LOG_LEVEL", default="INFO")
|
log_level = os.getenv(key="LOG_LEVEL", default="INFO")
|
||||||
|
|
||||||
# Advanced Python Scheduler
|
# Advanced Python Scheduler
|
||||||
@ -85,7 +84,7 @@ if __name__ == "__main__":
|
|||||||
job_defaults = {"coalesce": True}
|
job_defaults = {"coalesce": True}
|
||||||
scheduler = AsyncIOScheduler(
|
scheduler = AsyncIOScheduler(
|
||||||
jobstores=jobstores,
|
jobstores=jobstores,
|
||||||
timezone=pytz.timezone(scheduler_timezone),
|
timezone=pytz.timezone(config_timezone),
|
||||||
job_defaults=job_defaults,
|
job_defaults=job_defaults,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user