some cleanup of remind command. Moved some logic to the service

This commit is contained in:
Toastie (DCS Team) 2024-08-18 16:22:43 +12:00
parent 2605351f5c
commit 3ca832090e
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
2 changed files with 57 additions and 41 deletions

View file

@ -44,11 +44,14 @@ public partial class Utility
ulong target; ulong target;
target = meorhere == MeOrHere.Me ? ctx.User.Id : ctx.Channel.Id; target = meorhere == MeOrHere.Me ? ctx.User.Id : ctx.Channel.Id;
if (!await RemindInternal(target,
var success = await RemindInternal(target,
meorhere == MeOrHere.Me || ctx.Guild is null, meorhere == MeOrHere.Me || ctx.Guild is null,
remindData.Time, remindData.Time,
remindData.What, remindData.What,
ReminderType.User)) ReminderType.User);
if (!success)
await Response().Error(strs.remind_too_long).SendAsync(); 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(); await Response().Error(strs.remind_too_long).SendAsync();
} }
@ -81,32 +85,32 @@ public partial class Utility
[UserPerm(GuildPerm.Administrator)] [UserPerm(GuildPerm.Administrator)]
[Priority(0)] [Priority(0)]
public Task RemindList(Server _, int page = 1) public Task RemindList(Server _, int page = 1)
=> RemindListInternal(page, true); => RemindListInternal(page, ctx.Guild.Id);
[Cmd] [Cmd]
[Priority(1)] [Priority(1)]
public Task RemindList(int page = 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) if (--page < 0)
return; return;
var embed = _sender.CreateEmbed() var embed = _sender.CreateEmbed()
.WithOkColor() .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<Reminder> rems; List<Reminder> rems;
await using (var uow = _db.GetDbContext()) if (guildId is { } gid)
{ rems = await _service.GetServerReminders(page, gid);
if (isServer)
rems = uow.Set<Reminder>().RemindersForServer(ctx.Guild.Id, page).ToList();
else else
rems = uow.Set<Reminder>().RemindersFor(ctx.User.Id, page).ToList(); rems = await _service.GetUserReminders(page, ctx.User.Id);
}
if (rems.Any())
if (rems.Count > 0)
{ {
var i = 0; var i = 0;
foreach (var rem in rems) foreach (var rem in rems)
@ -114,18 +118,23 @@ public partial class Utility
var when = rem.When; var when = rem.When;
embed.AddField( embed.AddField(
$"#{++i + (page * 10)}", $"#{++i + (page * 10)}",
$@"`When:` {TimestampTag.FromDateTime(when, TimestampTagStyles.ShortDateTime)} $"""
`Target:` {(rem.IsPrivate ? "DM" : "Channel")} [`{rem.ChannelId}`] `When:` {TimestampTag.FromDateTime(when, TimestampTagStyles.ShortDateTime)}
`Message:` {rem.Message?.TrimTo(50)}"); `Target:` {(rem.IsPrivate ? "DM" : "Channel")} [`{rem.ChannelId}`]
`Message:` {rem.Message?.TrimTo(50)}
""");
} }
} }
else else
{
embed.WithDescription(GetText(strs.reminders_none)); embed.WithDescription(GetText(strs.reminders_none));
}
embed.AddPaginatedFooter(page + 1, null); embed.AddPaginatedFooter(page + 1, null);
await Response().Embed(embed).SendAsync(); await Response().Embed(embed).SendAsync();
} }
[Cmd] [Cmd]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.Administrator)] [UserPerm(GuildPerm.Administrator)]
@ -201,8 +210,6 @@ public partial class Utility
} }
// var gTime = ctx.Guild is null ? time : TimeZoneInfo.ConvertTime(time, _tz.GetTimeZoneOrUtc(ctx.Guild.Id)); // var gTime = ctx.Guild is null ? time : TimeZoneInfo.ConvertTime(time, _tz.GetTimeZoneOrUtc(ctx.Guild.Id));
try
{
await Response() await Response()
.Confirm($"\u23f0 {GetText(strs.remind2( .Confirm($"\u23f0 {GetText(strs.remind2(
Format.Bold(!isPrivate ? $"<#{targetId}>" : ctx.User.Username), Format.Bold(!isPrivate ? $"<#{targetId}>" : ctx.User.Username),
@ -210,10 +217,6 @@ public partial class Utility
TimestampTag.FromDateTime(DateTime.UtcNow.Add(ts), TimestampTagStyles.Relative), TimestampTag.FromDateTime(DateTime.UtcNow.Add(ts), TimestampTagStyles.Relative),
TimestampTag.FormatFromDateTime(time, TimestampTagStyles.ShortDateTime)))}") TimestampTag.FormatFromDateTime(time, TimestampTagStyles.ShortDateTime)))}")
.SendAsync(); .SendAsync();
}
catch
{
}
return true; return true;
} }

View file

@ -11,7 +11,8 @@ namespace EllieBot.Modules.Utility.Services;
public class RemindService : IEService, IReadyExecutor, IRemindService public class RemindService : IEService, IReadyExecutor, IRemindService
{ {
private readonly Regex _regex = private readonly Regex _regex =
new(@"^(?:(?:at|on(?:\sthe)?)?\s*(?<date>(?:\d{2}:\d{2}\s)?\d{1,2}\.\d{1,2}(?:\.\d{2,4})?)|(?:in\s?)?\s*(?:(?<mo>\d+)(?:\s?(?:months?|mos?),?))?(?:(?:\sand\s|\s*)?(?<w>\d+)(?:\s?(?:weeks?|w),?))?(?:(?:\sand\s|\s*)?(?<d>\d+)(?:\s?(?:days?|d),?))?(?:(?:\sand\s|\s*)?(?<h>\d+)(?:\s?(?:hours?|h),?))?(?:(?:\sand\s|\s*)?(?<m>\d+)(?:\s?(?:minutes?|mins?|m),?))?)\s+(?:to:?\s+)?(?<what>(?:\r\n|[\r\n]|.)+)", new(
@"^(?:(?:at|on(?:\sthe)?)?\s*(?<date>(?:\d{2}:\d{2}\s)?\d{1,2}\.\d{1,2}(?:\.\d{2,4})?)|(?:in\s?)?\s*(?:(?<mo>\d+)(?:\s?(?:months?|mos?),?))?(?:(?:\sand\s|\s*)?(?<w>\d+)(?:\s?(?:weeks?|w),?))?(?:(?:\sand\s|\s*)?(?<d>\d+)(?:\s?(?:days?|d),?))?(?:(?:\sand\s|\s*)?(?<h>\d+)(?:\s?(?:hours?|h),?))?(?:(?:\sand\s|\s*)?(?<m>\d+)(?:\s?(?:minutes?|mins?|m),?))?)\s+(?:to:?\s+)?(?<what>(?:\r\n|[\r\n]|.)+)",
RegexOptions.Compiled | RegexOptions.Multiline); RegexOptions.Compiled | RegexOptions.Multiline);
private readonly DiscordSocketClient _client; private readonly DiscordSocketClient _client;
@ -255,4 +256,16 @@ public class RemindService : IEService, IReadyExecutor, IRemindService
.AddAsync(rem); .AddAsync(rem);
await ctx.SaveChangesAsync(); await ctx.SaveChangesAsync();
} }
public async Task<List<Reminder>> GetServerReminders(int page, ulong guildId)
{
await using var uow = _db.GetDbContext();
return uow.Set<Reminder>().RemindersForServer(guildId, page).ToList();
}
public async Task<List<Reminder>> GetUserReminders(int page, ulong userId)
{
await using var uow = _db.GetDbContext();
return uow.Set<Reminder>().RemindersFor(userId, page).ToList();
}
} }