"Fix" remind list when you have more than 25 reminders

This commit is contained in:
2022-03-19 03:55:46 +01:00
parent e6fedbb7e7
commit fae53f15ff

View File

@ -130,20 +130,13 @@ async def command_modify(ctx: SlashContext, time_or_message: str):
# TODO: Reduce complexity. # TODO: Reduce complexity.
# Only make a list with normal reminders. # Only make a list with normal reminders.
list_embed, jobs_dict = make_list(ctx, skip_cron_or_interval=True) jobs_dict = await send_list(ctx, skip_cron_or_interval=True)
if time_or_message == "date": if time_or_message == "date":
date_or_message = "the date" date_or_message = "the date"
else: else:
date_or_message = "the message" date_or_message = "the message"
# The empty embed has 76 characters
# TODO: This is a hack. Fix it.
# TODO: Move this to a function.
if len(list_embed) <= 76:
await ctx.send(f"{ctx.guild.name} has no reminders.")
else:
await ctx.send(embed=list_embed)
await ctx.channel.send( await ctx.channel.send(
"Type the corresponding number to the reminder were you wish to" "Type the corresponding number to the reminder were you wish to"
f" change {date_or_message}. Does not work with cron or interval." f" change {date_or_message}. Does not work with cron or interval."
@ -247,13 +240,8 @@ async def remind_remove(ctx: SlashContext):
"""Select reminder from list that you want to remove.""" """Select reminder from list that you want to remove."""
# TODO: Reduce complexity # TODO: Reduce complexity
list_embed, jobs_dict = make_list(ctx) jobs_dict = await send_list(ctx)
# The empty embed has 76 characters
if len(list_embed) <= 76:
await ctx.send(f"{ctx.guild.name} has no reminders.")
else:
await ctx.send(embed=list_embed)
await ctx.channel.send( await ctx.channel.send(
"Type the corresponding number to the reminder you wish to remove." "Type the corresponding number to the reminder you wish to remove."
" Type Exit to exit." " Type Exit to exit."
@ -273,9 +261,7 @@ async def remind_remove(ctx: SlashContext):
if int(response_message.clean_content) == num: if int(response_message.clean_content) == num:
job = scheduler.get_job(job_from_dict) job = scheduler.get_job(job_from_dict)
if job is None: if job is None:
await ctx.channel.send( await ctx.channel.send(f"No reminder with that ID ({job_from_dict}).")
f"No reminder with that ID ({job_from_dict})."
)
return return
channel_id = job.kwargs.get("channel_id") channel_id = job.kwargs.get("channel_id")
@ -305,7 +291,7 @@ async def remind_remove(ctx: SlashContext):
await ctx.channel.send(msg) await ctx.channel.send(msg)
def make_list(ctx, skip_datetriggers=False, skip_cron_or_interval=False): async def send_list(ctx, skip_datetriggers=False, skip_cron_or_interval=False):
"""Create a list of reminders. """Create a list of reminders.
Args: Args:
@ -313,11 +299,11 @@ def make_list(ctx, skip_datetriggers=False, skip_cron_or_interval=False):
skip_cron_or_interval (bool, optional): Only show normal reminders. skip_cron_or_interval (bool, optional): Only show normal reminders.
Returns: Returns:
embed: Embed is the list of reminders that we send to Discord.
jobs_dict: Dictionary that contains placement in list and job id. jobs_dict: Dictionary that contains placement in list and job id.
""" """
jobs_dict = {} jobs_dict = {}
job_number = 0 job_number = 0
embed = discord.Embed( embed = discord.Embed(
colour=discord.Colour.random(), colour=discord.Colour.random(),
title="discord-reminder-bot by TheLovinator#9276", title="discord-reminder-bot by TheLovinator#9276",
@ -325,19 +311,17 @@ def make_list(ctx, skip_datetriggers=False, skip_cron_or_interval=False):
url="https://github.com/TheLovinator1/discord-reminder-bot", url="https://github.com/TheLovinator1/discord-reminder-bot",
) )
jobs = scheduler.get_jobs() jobs = scheduler.get_jobs()
for job in jobs: for job in jobs:
channel_id = job.kwargs.get("channel_id") channel_id = job.kwargs.get("channel_id")
channel_name = bot.get_channel(int(channel_id)) channel_name = bot.get_channel(int(channel_id))
# Only add reminders from channels in server we run "/reminder # Only add reminders from channels in server we run
# list" in # "/reminder list" in
# Check if channel is in server # Check if channel is in server
for channel in ctx.guild.channels: for channel in ctx.guild.channels:
if channel.id == channel_id: if channel.id == channel_id:
job_number += 1
jobs_dict[job_number] = job.id
message = job.kwargs.get("message")
if type(job.trigger) is DateTrigger: if type(job.trigger) is DateTrigger:
# Get trigger time for normal reminders # Get trigger time for normal reminders
trigger_time = job.trigger.run_date trigger_time = job.trigger.run_date
@ -359,6 +343,10 @@ def make_list(ctx, skip_datetriggers=False, skip_cron_or_interval=False):
else: else:
trigger_value = f'{trigger_time.strftime("%Y-%m-%d %H:%M")} (in {calc_countdown(job)})' trigger_value = f'{trigger_time.strftime("%Y-%m-%d %H:%M")} (in {calc_countdown(job)})'
job_number += 1
jobs_dict[job_number] = job.id
message = job.kwargs.get("message")
# Truncate message if it's too long # Truncate message if it's too long
field_name = f"{job_number}) {message} in #{channel_name}" field_name = f"{job_number}) {message} in #{channel_name}"
field_name = field_name[:253] + (field_name[253:] and "...") field_name = field_name[:253] + (field_name[253:] and "...")
@ -368,7 +356,20 @@ def make_list(ctx, skip_datetriggers=False, skip_cron_or_interval=False):
value=trigger_value, value=trigger_value,
inline=False, inline=False,
) )
return embed, jobs_dict
if job_number == 24:
await ctx.send(
"I haven't added support for showing more than 25 reminders yet 🙃"
)
break
# The empty embed has 76 characters
if len(embed) <= 76:
await ctx.send(f"{ctx.guild.name} has no reminders.")
else:
await ctx.send(embed=embed)
return jobs_dict
@slash.subcommand( @slash.subcommand(
@ -378,15 +379,7 @@ def make_list(ctx, skip_datetriggers=False, skip_cron_or_interval=False):
) )
async def remind_list(ctx: SlashContext): async def remind_list(ctx: SlashContext):
"""Send a list of reminders to Discord.""" """Send a list of reminders to Discord."""
list_embed, _ = make_list(ctx) await send_list(ctx)
# The empty embed has 76 characters
# TODO: This is a hack. Fix it.
# TODO: Move this to a function.
if len(list_embed) <= 76:
await ctx.send(f"{ctx.guild.name} has no reminders.")
else:
await ctx.send(embed=list_embed)
@slash.subcommand( @slash.subcommand(
@ -396,22 +389,14 @@ async def remind_list(ctx: SlashContext):
) )
async def remind_pause(ctx: SlashContext): async def remind_pause(ctx: SlashContext):
"""Get a list of reminders that you can pause.""" """Get a list of reminders that you can pause."""
list_embed, jobs_dict = make_list(ctx, skip_datetriggers=True) jobs_dict = await send_list(ctx, skip_datetriggers=True)
# The empty embed has 76 characters
# TODO: This is a hack. Fix it.
# TODO: Move this to a function.
if len(list_embed) <= 76:
await ctx.send(f"{ctx.guild.name} has no reminders.")
else:
await ctx.send(embed=list_embed)
await ctx.channel.send( await ctx.channel.send(
"Type the corresponding number to the reminder you wish to pause." "Type the corresponding number to the reminder you wish to pause."
" Type Exit to exit." " Type Exit to exit."
) )
# Only check for response from the original user and in the # Only check for response from the original user and in the correct channel
# correct channel
def check(m): def check(m):
return m.author == ctx.author and m.channel == ctx.channel return m.author == ctx.author and m.channel == ctx.channel
@ -442,7 +427,9 @@ async def remind_pause(ctx: SlashContext):
f"{message} in #{channel_name} is already paused." f"{message} in #{channel_name} is already paused."
) )
trigger_value = f'{trigger_time.strftime("%Y-%m-%d %H:%M")} (in {calc_countdown(job)})' trigger_value = (
f'{trigger_time.strftime("%Y-%m-%d %H:%M")} (in {calc_countdown(job)})'
)
msg = ( msg = (
f"**Paused** {message} in #{channel_name}.\n" f"**Paused** {message} in #{channel_name}.\n"
@ -462,22 +449,14 @@ async def remind_pause(ctx: SlashContext):
async def remind_resume(ctx: SlashContext): async def remind_resume(ctx: SlashContext):
"""Send a list of reminders to pause to Discord.""" """Send a list of reminders to pause to Discord."""
# TODO: Reduce the complexity of this function # TODO: Reduce the complexity of this function
list_embed, jobs_dict = make_list(ctx, skip_datetriggers=True) jobs_dict = await send_list(ctx, skip_datetriggers=True)
# The empty embed has 76 characters
# TODO: This is a hack. Fix it.
# TODO: Move this to a function.
if len(list_embed) <= 76:
await ctx.send(f"{ctx.guild.name} has no reminders.")
else:
await ctx.send(embed=list_embed)
await ctx.channel.send( await ctx.channel.send(
"Type the corresponding number to the reminder you wish to pause." "Type the corresponding number to the reminder you wish to pause."
" Type Exit to exit." " Type Exit to exit."
) )
# Only check for response from the original user and in the # Only check for response from the original user and in the correct channel
# correct channel
def check(m): def check(m):
return m.author == ctx.author and m.channel == ctx.channel return m.author == ctx.author and m.channel == ctx.channel
@ -567,7 +546,6 @@ async def remind_add(
f"{message_date}", f"{message_date}",
settings={ settings={
"PREFER_DATES_FROM": "future", "PREFER_DATES_FROM": "future",
# TODO: Is timezones even working? Timezones confuse me.
"TO_TIMEZONE": f"{config_timezone}", "TO_TIMEZONE": f"{config_timezone}",
}, },
) )