mirror of
				https://github.com/TheLovinator1/discord-reminder-bot.git
				synced 2025-11-04 01:59:48 +01:00 
			
		
		
		
	Refactor /remind cron
				
					
				
			This commit is contained in:
		@@ -16,6 +16,7 @@ from discord_reminder_bot.ui import JobManagementView, create_job_embed
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
if TYPE_CHECKING:
 | 
					if TYPE_CHECKING:
 | 
				
			||||||
    import datetime
 | 
					    import datetime
 | 
				
			||||||
 | 
					    from collections.abc import Callable
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    from apscheduler.job import Job
 | 
					    from apscheduler.job import Job
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -152,16 +153,16 @@ class RemindGroup(discord.app_commands.Group):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        jobs: list[Job] = settings.scheduler.get_jobs()
 | 
					        jobs: list[Job] = settings.scheduler.get_jobs()
 | 
				
			||||||
        if not jobs:
 | 
					        if not jobs:
 | 
				
			||||||
            await interaction.followup.send("No jobs available.")
 | 
					            await interaction.followup.send(content="No jobs available.")
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        first_job: Job | None = jobs[0] if jobs else None
 | 
					        first_job: Job | None = jobs[0] if jobs else None
 | 
				
			||||||
        if not first_job:
 | 
					        if not first_job:
 | 
				
			||||||
            await interaction.followup.send("No jobs available.")
 | 
					            await interaction.followup.send(content="No jobs available.")
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        embed: discord.Embed = create_job_embed(first_job)
 | 
					        embed: discord.Embed = create_job_embed(job=first_job)
 | 
				
			||||||
        view = JobManagementView(first_job, settings.scheduler)
 | 
					        view = JobManagementView(job=first_job, scheduler=settings.scheduler)
 | 
				
			||||||
        await interaction.followup.send(embed=embed, view=view)
 | 
					        await interaction.followup.send(embed=embed, view=view)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # /remind cron
 | 
					    # /remind cron
 | 
				
			||||||
@@ -213,19 +214,23 @@ class RemindGroup(discord.app_commands.Group):
 | 
				
			|||||||
        logger.info("New cron job from %s (%s) in %s", interaction.user, interaction.user.id, interaction.channel)
 | 
					        logger.info("New cron job from %s (%s) in %s", interaction.user, interaction.user.id, interaction.channel)
 | 
				
			||||||
        logger.info("Cron job arguments: %s", {k: v for k, v in locals().items() if k != "self" and v is not None})
 | 
					        logger.info("Cron job arguments: %s", {k: v for k, v in locals().items() if k != "self" and v is not None})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        channel_id: int | None = self.get_channel_id(interaction, channel)
 | 
					        # Get the channel ID
 | 
				
			||||||
        guild: discord.Guild | None = interaction.guild or None
 | 
					        channel_id: int | None = self.get_channel_id(interaction=interaction, channel=channel)
 | 
				
			||||||
        if not guild:
 | 
					        if not channel_id:
 | 
				
			||||||
            await interaction.followup.send("Failed to get guild.")
 | 
					            await interaction.followup.send(content="Failed to get channel.")
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        dm_message: str = ""
 | 
					        # Ensure the guild is valid
 | 
				
			||||||
        where_and_when = ""
 | 
					        guild: discord.Guild | None = interaction.guild or None
 | 
				
			||||||
        should_send_channel_reminder = True
 | 
					        if not guild:
 | 
				
			||||||
        if user:
 | 
					            await interaction.followup.send(content="Failed to get guild.")
 | 
				
			||||||
            user_reminder: Job = settings.scheduler.add_job(
 | 
					            return
 | 
				
			||||||
                send_to_user,
 | 
					
 | 
				
			||||||
                "cron",
 | 
					        # Helper to add a job
 | 
				
			||||||
 | 
					        def add_job(func: Callable, job_kwargs: dict[str, int | str]) -> Job:
 | 
				
			||||||
 | 
					            return settings.scheduler.add_job(
 | 
				
			||||||
 | 
					                func=func,
 | 
				
			||||||
 | 
					                trigger="cron",
 | 
				
			||||||
                year=year,
 | 
					                year=year,
 | 
				
			||||||
                month=month,
 | 
					                month=month,
 | 
				
			||||||
                day=day,
 | 
					                day=day,
 | 
				
			||||||
@@ -238,49 +243,45 @@ class RemindGroup(discord.app_commands.Group):
 | 
				
			|||||||
                end_date=end_date,
 | 
					                end_date=end_date,
 | 
				
			||||||
                timezone=timezone,
 | 
					                timezone=timezone,
 | 
				
			||||||
                jitter=jitter,
 | 
					                jitter=jitter,
 | 
				
			||||||
                kwargs={
 | 
					                kwargs=job_kwargs,
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Create user DM reminder job if user is specified
 | 
				
			||||||
 | 
					        dm_message: str = ""
 | 
				
			||||||
 | 
					        if user:
 | 
				
			||||||
 | 
					            dm_job: Job = add_job(
 | 
				
			||||||
 | 
					                func=send_to_user,
 | 
				
			||||||
 | 
					                job_kwargs={
 | 
				
			||||||
                    "user_id": user.id,
 | 
					                    "user_id": user.id,
 | 
				
			||||||
                    "guild_id": guild.id,
 | 
					                    "guild_id": guild.id,
 | 
				
			||||||
                    "message": message,
 | 
					                    "message": message,
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
 | 
					 | 
				
			||||||
            dm_message = f" and a DM to {user.display_name}"
 | 
					            dm_message = f" and a DM to {user.display_name}"
 | 
				
			||||||
            if not dm_and_current_channel:
 | 
					            if not dm_and_current_channel:
 | 
				
			||||||
                should_send_channel_reminder = False
 | 
					                # If only DM is required, notify about the DM job and exit
 | 
				
			||||||
                where_and_when: str = (
 | 
					                await interaction.followup.send(
 | 
				
			||||||
 | 
					                    content=f"Hello {interaction.user.display_name},\n"
 | 
				
			||||||
                    f"I will send a DM to {user.display_name} at:\n"
 | 
					                    f"I will send a DM to {user.display_name} at:\n"
 | 
				
			||||||
                    f"First run in {calculate(user_reminder)} with the message:\n"
 | 
					                    f"First run in {calculate(dm_job)} with the message:\n**{message}**.",
 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
        if should_send_channel_reminder:
 | 
					 | 
				
			||||||
            reminder: Job = settings.scheduler.add_job(
 | 
					 | 
				
			||||||
                send_to_discord,
 | 
					 | 
				
			||||||
                "cron",
 | 
					 | 
				
			||||||
                year=year,
 | 
					 | 
				
			||||||
                month=month,
 | 
					 | 
				
			||||||
                day=day,
 | 
					 | 
				
			||||||
                week=week,
 | 
					 | 
				
			||||||
                day_of_week=day_of_week,
 | 
					 | 
				
			||||||
                hour=hour,
 | 
					 | 
				
			||||||
                minute=minute,
 | 
					 | 
				
			||||||
                second=second,
 | 
					 | 
				
			||||||
                start_date=start_date,
 | 
					 | 
				
			||||||
                end_date=end_date,
 | 
					 | 
				
			||||||
                timezone=timezone,
 | 
					 | 
				
			||||||
                jitter=jitter,
 | 
					 | 
				
			||||||
                kwargs={
 | 
					 | 
				
			||||||
                    "channel_id": channel_id,
 | 
					 | 
				
			||||||
                    "message": message,
 | 
					 | 
				
			||||||
                    "author_id": interaction.user.id,
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
            )
 | 
					 | 
				
			||||||
            where_and_when = (
 | 
					 | 
				
			||||||
                f"I will notify you in <#{channel_id}>{dm_message}.\n"
 | 
					 | 
				
			||||||
                f"First run in {calculate(reminder)} with the message:\n"
 | 
					 | 
				
			||||||
            )
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        final_message: str = f"Hello {interaction.user.display_name}, {where_and_when}**{message}**."
 | 
					        # Create channel reminder job
 | 
				
			||||||
        await interaction.followup.send(final_message)
 | 
					        channel_job: Job = add_job(
 | 
				
			||||||
 | 
					            func=send_to_discord,
 | 
				
			||||||
 | 
					            job_kwargs={
 | 
				
			||||||
 | 
					                "channel_id": channel_id,
 | 
				
			||||||
 | 
					                "message": message,
 | 
				
			||||||
 | 
					                "author_id": interaction.user.id,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Compose the final message
 | 
				
			||||||
 | 
					        await interaction.followup.send(
 | 
				
			||||||
 | 
					            content=f"Hello {interaction.user.display_name},\n"
 | 
				
			||||||
 | 
					            f"I will notify you in <#{channel_id}>{dm_message}.\n"
 | 
				
			||||||
 | 
					            f"First run in {calculate(channel_job)} with the message:\n**{message}**.",
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @staticmethod
 | 
					    @staticmethod
 | 
				
			||||||
    def get_channel_id(interaction: discord.Interaction, channel: discord.TextChannel | None) -> int | None:
 | 
					    def get_channel_id(interaction: discord.Interaction, channel: discord.TextChannel | None) -> int | None:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user