Skip to content

Commit

Permalink
Merge pull request #162 from Naamloos/feature/timer-refactor
Browse files Browse the repository at this point in the history
Feature/timer refactor
  • Loading branch information
Naamloos committed May 24, 2023
2 parents 1fe374b + 57e1ca4 commit ca1d29c
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 359 deletions.
2 changes: 1 addition & 1 deletion ModCore/Commands/Moderation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public class Moderation : ApplicationCommandModule
await db.SaveChangesAsync();
}

await Timers.RescheduleTimers(ctx.Client, this.Database, this.Shared);
await Timers.ScheduleNextAsync();

var banEnd = DateTimeOffset.UtcNow.Add(timespan);

Expand Down
16 changes: 11 additions & 5 deletions ModCore/Commands/Reminders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public class Reminders : ApplicationCommandModule
return;
}

if (duration < TimeSpan.FromSeconds(15)) // 1 year is the maximum
{
await ctx.CreateResponseAsync("⚠️ Minimum allowed time span to set a reminder is 15 seconds.", true);
return;
}

var now = DateTimeOffset.UtcNow;
var dispatchAt = now + duration;

Expand All @@ -80,7 +86,7 @@ public class Reminders : ApplicationCommandModule
}

// reschedule timers
await Timers.RescheduleTimers(ctx.Client, this.Database, this.Shared);
await Timers.ScheduleNextAsync();
await ctx.CreateResponseAsync(
$"⏰ Ok, <t:{DateTimeOffset.Now.Add(duration).ToUnixTimeSeconds()}:R> I will remind you about the following:\n\n{about}", true);
}
Expand Down Expand Up @@ -160,7 +166,7 @@ public async Task ListAsync(InteractionContext ctx)
}

// unschedule and reset timers
await Timers.UnscheduleTimerAsync(reminder, ctx.Client, this.Database, this.Shared);
await Timers.UnscheduleTimersAsync(reminder);

var duration = reminder.DispatchAt - DateTimeOffset.Now;
var data = reminder.GetData<TimerReminderData>();
Expand All @@ -186,10 +192,10 @@ public async Task ClearAsync(InteractionContext ctx)
{
using (var db = this.Database.CreateContext())
{
List<DatabaseTimer> timers = db.Timers.Where(xt => xt.ActionType == TimerActionType.Reminder && xt.UserId == (long)ctx.User.Id).ToList();
DatabaseTimer[] timers = db.Timers.Where(xt => xt.ActionType == TimerActionType.Reminder && xt.UserId == (long)ctx.User.Id).ToArray();

var count = timers.Count;
await Timers.UnscheduleTimersAsync(timers, ctx.Client, this.Database, this.Shared);
var count = timers.Length;
await Timers.UnscheduleTimersAsync(timers);

await ctx.EditFollowupAsync(confirmed.FollowupMessage.Id, new DiscordWebhookBuilder()
.WithContent("✅ Alright, cleared " + count + $" timer{(count > 1? "s" : "")}."));
Expand Down

0 comments on commit ca1d29c

Please sign in to comment.