From 3ca832090e4a8e0c7d7fb943c71aaccc99b7312c Mon Sep 17 00:00:00 2001 From: Toastie Date: Sun, 18 Aug 2024 16:22:43 +1200 Subject: [PATCH] some cleanup of remind command. Moved some logic to the service --- .../Modules/Utility/Remind/RemindCommands.cs | 71 ++++++++++--------- .../Modules/Utility/Remind/RemindService.cs | 27 +++++-- 2 files changed, 57 insertions(+), 41 deletions(-) diff --git a/src/EllieBot/Modules/Utility/Remind/RemindCommands.cs b/src/EllieBot/Modules/Utility/Remind/RemindCommands.cs index 9667384..b568ada 100644 --- a/src/EllieBot/Modules/Utility/Remind/RemindCommands.cs +++ b/src/EllieBot/Modules/Utility/Remind/RemindCommands.cs @@ -44,11 +44,14 @@ public partial class Utility ulong target; target = meorhere == MeOrHere.Me ? ctx.User.Id : ctx.Channel.Id; - if (!await RemindInternal(target, - meorhere == MeOrHere.Me || ctx.Guild is null, - remindData.Time, - remindData.What, - ReminderType.User)) + + var success = await RemindInternal(target, + meorhere == MeOrHere.Me || ctx.Guild is null, + remindData.Time, + remindData.What, + ReminderType.User); + + if (!success) await Response().Error(strs.remind_too_long).SendAsync(); } @@ -72,7 +75,8 @@ public partial class Utility } - if (!await RemindInternal(channel.Id, false, remindData.Time, remindData.What, ReminderType.User)) + var success = await RemindInternal(channel.Id, false, remindData.Time, remindData.What, ReminderType.User); + if (!success) await Response().Error(strs.remind_too_long).SendAsync(); } @@ -81,32 +85,32 @@ public partial class Utility [UserPerm(GuildPerm.Administrator)] [Priority(0)] public Task RemindList(Server _, int page = 1) - => RemindListInternal(page, true); + => RemindListInternal(page, ctx.Guild.Id); [Cmd] [Priority(1)] public Task RemindList(int page = 1) - => RemindListInternal(page, false); + => RemindListInternal(page, null); - private async Task RemindListInternal(int page, bool isServer) + private async Task RemindListInternal(int page, ulong? guildId) { if (--page < 0) return; var embed = _sender.CreateEmbed() .WithOkColor() - .WithTitle(GetText(isServer ? strs.reminder_server_list : strs.reminder_list)); + .WithTitle(GetText(guildId is not null + ? strs.reminder_server_list + : strs.reminder_list)); List rems; - await using (var uow = _db.GetDbContext()) - { - if (isServer) - rems = uow.Set().RemindersForServer(ctx.Guild.Id, page).ToList(); - else - rems = uow.Set().RemindersFor(ctx.User.Id, page).ToList(); - } + if (guildId is { } gid) + rems = await _service.GetServerReminders(page, gid); + else + rems = await _service.GetUserReminders(page, ctx.User.Id); - if (rems.Any()) + + if (rems.Count > 0) { var i = 0; foreach (var rem in rems) @@ -114,18 +118,23 @@ public partial class Utility var when = rem.When; embed.AddField( $"#{++i + (page * 10)}", - $@"`When:` {TimestampTag.FromDateTime(when, TimestampTagStyles.ShortDateTime)} -`Target:` {(rem.IsPrivate ? "DM" : "Channel")} [`{rem.ChannelId}`] -`Message:` {rem.Message?.TrimTo(50)}"); + $""" + `When:` {TimestampTag.FromDateTime(when, TimestampTagStyles.ShortDateTime)} + `Target:` {(rem.IsPrivate ? "DM" : "Channel")} [`{rem.ChannelId}`] + `Message:` {rem.Message?.TrimTo(50)} + """); } } else + { embed.WithDescription(GetText(strs.reminders_none)); + } embed.AddPaginatedFooter(page + 1, null); await Response().Embed(embed).SendAsync(); } + [Cmd] [RequireContext(ContextType.Guild)] [UserPerm(GuildPerm.Administrator)] @@ -201,19 +210,13 @@ public partial class Utility } // var gTime = ctx.Guild is null ? time : TimeZoneInfo.ConvertTime(time, _tz.GetTimeZoneOrUtc(ctx.Guild.Id)); - try - { - await Response() - .Confirm($"\u23f0 {GetText(strs.remind2( - Format.Bold(!isPrivate ? $"<#{targetId}>" : ctx.User.Username), - Format.Bold(message), - TimestampTag.FromDateTime(DateTime.UtcNow.Add(ts), TimestampTagStyles.Relative), - TimestampTag.FormatFromDateTime(time, TimestampTagStyles.ShortDateTime)))}") - .SendAsync(); - } - catch - { - } + await Response() + .Confirm($"\u23f0 {GetText(strs.remind2( + Format.Bold(!isPrivate ? $"<#{targetId}>" : ctx.User.Username), + Format.Bold(message), + TimestampTag.FromDateTime(DateTime.UtcNow.Add(ts), TimestampTagStyles.Relative), + TimestampTag.FormatFromDateTime(time, TimestampTagStyles.ShortDateTime)))}") + .SendAsync(); return true; } diff --git a/src/EllieBot/Modules/Utility/Remind/RemindService.cs b/src/EllieBot/Modules/Utility/Remind/RemindService.cs index dad22d2..78981d7 100644 --- a/src/EllieBot/Modules/Utility/Remind/RemindService.cs +++ b/src/EllieBot/Modules/Utility/Remind/RemindService.cs @@ -11,7 +11,8 @@ namespace EllieBot.Modules.Utility.Services; public class RemindService : IEService, IReadyExecutor, IRemindService { private readonly Regex _regex = - new(@"^(?:(?:at|on(?:\sthe)?)?\s*(?(?:\d{2}:\d{2}\s)?\d{1,2}\.\d{1,2}(?:\.\d{2,4})?)|(?:in\s?)?\s*(?:(?\d+)(?:\s?(?:months?|mos?),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:weeks?|w),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:days?|d),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:hours?|h),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:minutes?|mins?|m),?))?)\s+(?:to:?\s+)?(?(?:\r\n|[\r\n]|.)+)", + new( + @"^(?:(?:at|on(?:\sthe)?)?\s*(?(?:\d{2}:\d{2}\s)?\d{1,2}\.\d{1,2}(?:\.\d{2,4})?)|(?:in\s?)?\s*(?:(?\d+)(?:\s?(?:months?|mos?),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:weeks?|w),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:days?|d),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:hours?|h),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:minutes?|mins?|m),?))?)\s+(?:to:?\s+)?(?(?:\r\n|[\r\n]|.)+)", RegexOptions.Compiled | RegexOptions.Multiline); private readonly DiscordSocketClient _client; @@ -208,12 +209,12 @@ public class RemindService : IEService, IReadyExecutor, IRemindService { await _sender.Response(ch) .Embed(_sender.CreateEmbed() - .WithOkColor() - .WithTitle("Reminder") - .AddField("Created At", - r.DateAdded.HasValue ? r.DateAdded.Value.ToLongDateString() : "?") - .AddField("By", - (await ch.GetUserAsync(r.UserId))?.ToString() ?? r.UserId.ToString())) + .WithOkColor() + .WithTitle("Reminder") + .AddField("Created At", + r.DateAdded.HasValue ? r.DateAdded.Value.ToLongDateString() : "?") + .AddField("By", + (await ch.GetUserAsync(r.UserId))?.ToString() ?? r.UserId.ToString())) .Text(r.Message) .SendAsync(); } @@ -255,4 +256,16 @@ public class RemindService : IEService, IReadyExecutor, IRemindService .AddAsync(rem); await ctx.SaveChangesAsync(); } + + public async Task> GetServerReminders(int page, ulong guildId) + { + await using var uow = _db.GetDbContext(); + return uow.Set().RemindersForServer(guildId, page).ToList(); + } + + public async Task> GetUserReminders(int page, ulong userId) + { + await using var uow = _db.GetDbContext(); + return uow.Set().RemindersFor(userId, page).ToList(); + } } \ No newline at end of file