This commit is contained in:
2025-04-28 03:51:34 +02:00
parent 0eb7a26439
commit 45a9d2655c
6 changed files with 166 additions and 134 deletions

View File

@ -10,19 +10,18 @@ from discord_reminder_bot.settings import scheduler
if TYPE_CHECKING:
import datetime
import discord
from apscheduler.job import Job
from discord.interactions import InteractionChannel
async def add_reminder_job(
interaction: discord.Interaction,
def add_reminder_job(
message: str,
time: str,
channel: discord.TextChannel | None = None,
user: discord.User | None = None,
channel_id: int,
author_id: int,
user_id: int | None = None,
guild_id: int | None = None,
dm_and_current_channel: bool | None = None,
) -> None:
) -> str:
"""Adds a reminder job to the scheduler based on user input.
Schedules a message to be sent at a specified time either to a specific channel,
@ -30,87 +29,63 @@ async def add_reminder_job(
time parsing, and job creation using the APScheduler instance.
Args:
interaction: The discord interaction object initiated by the user command.
message: The content of the reminder message to be sent.
time: A string representing the date and time for the reminder.
This string will be parsed to a datetime object.
channel: The target text channel for the reminder. If None, defaults
to the channel where the interaction occurred.
user: The target user to send a direct message reminder to. If None,
no direct message is scheduled.
channel_id: The ID of the channel where the reminder will be sent.
user_id: The Discord ID of the user to send a DM to. If None, no DM is sent.
guild_id: The ID of the guild (server) where the reminder is set.
author_id: The ID of the user who created the reminder.
dm_and_current_channel: If True and a user is specified, sends the
reminder to both the user's DM and the target channel. If False
and a user is specified, only sends the DM. Defaults to None,
behaving like False if only a user is specified, or sending only
to the channel if no user is specified.
Returns:
The response message indicating the status of the reminder job creation.
"""
logger.info(f"New reminder from {interaction.user} ({interaction.user.id}) in {interaction.channel}")
logger.info(f"Arguments: {locals()}")
# Check if we have access to the specified channel or the current channel
target_channel: InteractionChannel | discord.TextChannel | None = channel or interaction.channel
if target_channel and interaction.guild and not target_channel.permissions_for(interaction.guild.me).send_messages:
await interaction.followup.send(content=f"I don't have permission to send messages in <#{target_channel.id}>.", ephemeral=True)
# Get the channel ID
channel_id: int | None = channel.id if channel else (interaction.channel.id if interaction.channel else None)
if not channel_id:
await interaction.followup.send(content="Failed to get channel.", ephemeral=True)
return
# Ensure the guild is valid
guild: discord.Guild | None = interaction.guild or None
if not guild:
await interaction.followup.send(content="Failed to get guild.", ephemeral=True)
return
dm_message: str = ""
if user:
if user_id:
parsed_time: datetime.datetime | None = parse_time(date_to_parse=time)
if not parsed_time:
await interaction.followup.send(content=f"Failed to parse time: {time}.", ephemeral=True)
return
return f"Failed to parse time: {time}."
user_reminder: Job = scheduler.add_job(
func="discord_reminder_bot.main:send_to_user",
trigger="date",
run_date=parsed_time,
kwargs={
"user_id": user.id,
"guild_id": guild.id,
"user_id": user_id,
"guild_id": guild_id,
"message": message,
},
)
logger.info(f"User reminder job created: {user_reminder} for {user.id} at {parsed_time}")
logger.info(f"User reminder job created: {user_reminder} for {user_id} at {parsed_time}")
dm_message = f" and a DM to {user.display_name}"
dm_message = f" and a DM to <@{user_id}>"
if not dm_and_current_channel:
msg = (
f"Hello {interaction.user.display_name},\n"
f"I will send a DM to {user.display_name} at:\n"
return (
f"Hello <@{author_id}>,\n"
f"I will send a DM to <@{user_id}> at:\n"
f"First run in {calculate(user_reminder)} with the message:\n**{message}**."
)
await interaction.followup.send(content=msg)
return
# Create channel reminder job
channel_job: Job = scheduler.add_job(
func="discord_reminder_bot.main:send_to_channel",
func="discord_reminder_bot.main:send_to_discord",
trigger="date",
run_date=parse_time(date_to_parse=time),
kwargs={
"channel_id": channel_id,
"message": message,
"author_id": interaction.user.id,
"author_id": author_id,
},
)
logger.info(f"Channel reminder job created: {channel_job} for {channel_id}")
msg: str = (
f"Hello {interaction.user.display_name},\n"
return (
f"Hello <@{author_id}>,\n"
f"I will notify you in <#{channel_id}>{dm_message}.\n"
f"First run in {calculate(channel_job)} with the message:\n**{message}**."
)
await interaction.followup.send(content=msg)

View File

@ -111,7 +111,7 @@ async def cron_reminder_job(
# Create channel reminder job
channel_job: Job = scheduler.add_job(
func="discord_reminder_bot.main:send_to_channel",
func="discord_reminder_bot.main:send_to_discord",
trigger="cron",
year=year,
month=month,

View File

@ -108,9 +108,9 @@ async def interval_reminder_job(
)
# Create channel reminder job
# TODO(TheLovinator): Test that "discord_reminder_bot.main:send_to_channel" is always there # noqa: TD003
# TODO(TheLovinator): Test that "discord_reminder_bot.main:send_to_discord" is always there # noqa: TD003
channel_job: Job = scheduler.add_job(
func="discord_reminder_bot.main:send_to_channel",
func="discord_reminder_bot.main:send_to_discord",
trigger="interval",
weeks=weeks,
days=days,

View File

@ -44,7 +44,7 @@ async def list_reminder_job(interaction: discord.Interaction, scheduler: AsyncIO
message: discord.InteractionMessage = await interaction.original_response()
job_summary: list[str] = generate_reminder_summary(ctx=interaction)
job_summary: list[str] = generate_reminder_summary(ctx=interaction, bot=interaction.client, scheduler=scheduler)
for i, msg in enumerate(job_summary):
if i == 0: