Skip to content

Commit

Permalink
Handle NotFoundExceptions when modifying & deleting reminders
Browse files Browse the repository at this point in the history
Would have been thrown when modifying or deleting a reminder whose initial message was deleted & interrupted modification/deletion

Resolves #28
  • Loading branch information
FloatingMilkshake committed Dec 14, 2024
1 parent 3343321 commit 85fc76e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
35 changes: 21 additions & 14 deletions Commands/ReminderCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,22 +342,29 @@ await ctx.FollowupAsync(

if (!reminder.IsPrivate && reminder.ReminderTime is not null)
{
var reminderChannel = await Program.Discord.GetChannelAsync(reminder.ChannelId);

if (reminder.MessageId != default)
try
{
var reminderMessage = await reminderChannel.GetMessageAsync(reminder.MessageId);
var reminderChannel = await Program.Discord.GetChannelAsync(reminder.ChannelId);

if (reminder.MessageId != default)
{
var reminderMessage = await reminderChannel.GetMessageAsync(reminder.MessageId);

var unixTime = ((DateTimeOffset)reminder.ReminderTime).ToUnixTimeSeconds();

if (reminderMessage.Content.Contains("pushed back"))
await reminderMessage.ModifyAsync(
$"[Reminder](https://discord.com/channels/{reminder.GuildId}/{reminder.ChannelId}/{reminder.MessageId})" +
$" pushed back to <t:{unixTime}:F> (<t:{unixTime}:R>)!" +
$"\nReminder ID: `{reminder.ReminderId}`");
else
await reminderMessage.ModifyAsync($"Reminder set for <t:{unixTime}:F> (<t:{unixTime}:R>)!" +
$"\nReminder ID: `{reminder.ReminderId}`");
var unixTime = ((DateTimeOffset)reminder.ReminderTime).ToUnixTimeSeconds();

if (reminderMessage.Content.Contains("pushed back"))
await reminderMessage.ModifyAsync(
$"[Reminder](https://discord.com/channels/{reminder.GuildId}/{reminder.ChannelId}/{reminder.MessageId})" +
$" pushed back to <t:{unixTime}:F> (<t:{unixTime}:R>)!" +
$"\nReminder ID: `{reminder.ReminderId}`");
else
await reminderMessage.ModifyAsync($"Reminder set for <t:{unixTime}:F> (<t:{unixTime}:R>)!" +
$"\nReminder ID: `{reminder.ReminderId}`");
}
}
catch
{
// Not important
}
}

Expand Down
20 changes: 14 additions & 6 deletions Events/ComponentInteractionEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,22 @@ await e.Interaction.CreateResponseAsync(DiscordInteractionResponseType.ChannelMe

if (!reminder!.IsPrivate)
{
var reminderChannel = await Program.Discord.GetChannelAsync(reminder.ChannelId);

if (reminder.MessageId != default)
try
{
var reminderMessage = await reminderChannel.GetMessageAsync(reminder.MessageId);

await reminderMessage.ModifyAsync("This reminder was deleted.");
var reminderChannel = await Program.Discord.GetChannelAsync(reminder.ChannelId);

if (reminder.MessageId != default)
{
var reminderMessage = await reminderChannel.GetMessageAsync(reminder.MessageId);

await reminderMessage.ModifyAsync("This reminder was deleted.");
}
}
catch
{
// Not important
}

}

await Program.Db.HashDeleteAsync("reminders", e.Values[0]);
Expand Down

0 comments on commit 85fc76e

Please sign in to comment.