some cleanup of remind command. Moved some logic to the service
This commit is contained in:
parent
2605351f5c
commit
3ca832090e
2 changed files with 57 additions and 41 deletions
|
@ -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,
|
|
||||||
meorhere == MeOrHere.Me || ctx.Guild is null,
|
var success = await RemindInternal(target,
|
||||||
remindData.Time,
|
meorhere == MeOrHere.Me || ctx.Guild is null,
|
||||||
remindData.What,
|
remindData.Time,
|
||||||
ReminderType.User))
|
remindData.What,
|
||||||
|
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)
|
else
|
||||||
rems = uow.Set<Reminder>().RemindersForServer(ctx.Guild.Id, page).ToList();
|
rems = await _service.GetUserReminders(page, ctx.User.Id);
|
||||||
else
|
|
||||||
rems = uow.Set<Reminder>().RemindersFor(ctx.User.Id, page).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
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,19 +210,13 @@ 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()
|
||||||
{
|
.Confirm($"\u23f0 {GetText(strs.remind2(
|
||||||
await Response()
|
Format.Bold(!isPrivate ? $"<#{targetId}>" : ctx.User.Username),
|
||||||
.Confirm($"\u23f0 {GetText(strs.remind2(
|
Format.Bold(message),
|
||||||
Format.Bold(!isPrivate ? $"<#{targetId}>" : ctx.User.Username),
|
TimestampTag.FromDateTime(DateTime.UtcNow.Add(ts), TimestampTagStyles.Relative),
|
||||||
Format.Bold(message),
|
TimestampTag.FormatFromDateTime(time, TimestampTagStyles.ShortDateTime)))}")
|
||||||
TimestampTag.FromDateTime(DateTime.UtcNow.Add(ts), TimestampTagStyles.Relative),
|
.SendAsync();
|
||||||
TimestampTag.FormatFromDateTime(time, TimestampTagStyles.ShortDateTime)))}")
|
|
||||||
.SendAsync();
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -208,12 +209,12 @@ public class RemindService : IEService, IReadyExecutor, IRemindService
|
||||||
{
|
{
|
||||||
await _sender.Response(ch)
|
await _sender.Response(ch)
|
||||||
.Embed(_sender.CreateEmbed()
|
.Embed(_sender.CreateEmbed()
|
||||||
.WithOkColor()
|
.WithOkColor()
|
||||||
.WithTitle("Reminder")
|
.WithTitle("Reminder")
|
||||||
.AddField("Created At",
|
.AddField("Created At",
|
||||||
r.DateAdded.HasValue ? r.DateAdded.Value.ToLongDateString() : "?")
|
r.DateAdded.HasValue ? r.DateAdded.Value.ToLongDateString() : "?")
|
||||||
.AddField("By",
|
.AddField("By",
|
||||||
(await ch.GetUserAsync(r.UserId))?.ToString() ?? r.UserId.ToString()))
|
(await ch.GetUserAsync(r.UserId))?.ToString() ?? r.UserId.ToString()))
|
||||||
.Text(r.Message)
|
.Text(r.Message)
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue