mirror of
				https://github.com/TheLovinator1/discord-reminder-bot.git
				synced 2025-11-03 17:59:47 +01:00 
			
		
		
		
	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 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,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user