part 3 of the response rework

This commit is contained in:
Kwoth
2024-04-29 21:03:40 +00:00
parent d28c7b500d
commit daa2177559
65 changed files with 508 additions and 625 deletions

View File

@@ -13,7 +13,7 @@ public sealed class GiveawayService : INService, IReadyExecutor
private readonly DbService _db;
private readonly IBotCredentials _creds;
private readonly DiscordSocketClient _client;
private readonly IEmbedBuilderService _eb;
private readonly IMessageSenderService _sender;
private readonly IBotStrings _strings;
private readonly ILocalization _localization;
private readonly IMemoryCache _cache;
@@ -22,12 +22,12 @@ public sealed class GiveawayService : INService, IReadyExecutor
private readonly ConcurrentDictionary<int, GiveawayRerollData> _rerolls = new();
public GiveawayService(DbService db, IBotCredentials creds, DiscordSocketClient client,
IEmbedBuilderService eb, IBotStrings strings, ILocalization localization, IMemoryCache cache)
IMessageSenderService sender, IBotStrings strings, ILocalization localization, IMemoryCache cache)
{
_db = db;
_creds = creds;
_client = client;
_eb = eb;
_sender = sender;
_strings = strings;
_localization = localization;
_cache = cache;
@@ -317,8 +317,7 @@ public sealed class GiveawayService : INService, IReadyExecutor
{Format.Code(winner.UserId.ToString())}
""";
var eb = _eb
.Create()
var eb = new EmbedBuilder()
.WithOkColor()
.WithTitle(GetText(strs.giveaway_ended))
.WithDescription(ga.Message)
@@ -334,7 +333,7 @@ public sealed class GiveawayService : INService, IReadyExecutor
catch
{
_ = msg.DeleteAsync();
await ch.EmbedAsync(eb);
await _sender.Response(ch).Embed(eb).SendAsync();
}
}

View File

@@ -133,23 +133,23 @@ public partial class Utility
private async Task ShowQuoteData(Quote data)
{
var eb = new EmbedBuilder()
.WithOkColor()
.WithTitle($"{GetText(strs.quote_id($"#{data.Id}"))} | {GetText(strs.response)}:")
.WithDescription(Format.Sanitize(data.Text).Replace("](", "]\\(").TrimTo(4096))
.AddField(GetText(strs.trigger), data.Keyword)
.WithFooter(
GetText(strs.created_by($"{data.AuthorName} ({data.AuthorId})")))
.Build();
.WithOkColor()
.WithTitle($"{GetText(strs.quote_id($"#{data.Id}"))} | {GetText(strs.response)}:")
.WithDescription(Format.Sanitize(data.Text).Replace("](", "]\\(").TrimTo(4096))
.AddField(GetText(strs.trigger), data.Keyword)
.WithFooter(
GetText(strs.created_by($"{data.AuthorName} ({data.AuthorId})")));
if (!(data.Text.Length > 4096))
{
await ctx.Channel.SendMessageAsync(embed: eb);
await Response().Embed(eb).SendAsync();
return;
}
// todo all send files should go through response system too
await ctx.Channel.SendFileAsync(
attachment: new FileAttachment(await data.Text.ToStream(), "quote.txt"),
embed: eb);
embed: eb.Build());
}
private async Task QuoteSearchinternalAsync(string? keyword, string textOrAuthor)
@@ -168,10 +168,12 @@ public partial class Utility
if (quote is null)
return;
await ctx.Channel.SendMessageAsync($"`#{quote.Id}` 💬 "
+ quote.Keyword.ToLowerInvariant()
+ ": "
+ quote.Text.SanitizeAllMentions());
await Response()
.Confirm($"`#{quote.Id}` 💬 ",
quote.Keyword.ToLowerInvariant()
+ ": "
+ quote.Text.SanitizeAllMentions())
.SendAsync();
}
[Cmd]
@@ -204,7 +206,7 @@ public partial class Utility
if (quote is null || quote.GuildId != ctx.Guild.Id)
{
await Response().Error(GetText(strs.quotes_notfound)).SendAsync();
await Response().Error(strs.quotes_notfound).SendAsync();
return;
}

View File

@@ -11,25 +11,26 @@ namespace NadekoBot.Modules.Utility.Services;
public class RemindService : INService, IReadyExecutor, IRemindService
{
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);
private readonly DiscordSocketClient _client;
private readonly DbService _db;
private readonly IBotCredentials _creds;
private readonly IEmbedBuilderService _eb;
private readonly IMessageSenderService _sender;
private readonly CultureInfo _culture;
public RemindService(
DiscordSocketClient client,
DbService db,
IBotCredentials creds,
IEmbedBuilderService eb)
IMessageSenderService sender)
{
_client = client;
_db = db;
_creds = creds;
_eb = eb;
_sender = sender;
try
{
@@ -120,7 +121,7 @@ public class RemindService : INService, IReadyExecutor, IRemindService
if (!string.IsNullOrWhiteSpace(dateString))
{
var now = DateTime.UtcNow;
if (!DateTime.TryParse(dateString, _culture, DateTimeStyles.None, out var dt))
{
Log.Warning("Invalid remind datetime format");
@@ -162,6 +163,7 @@ public class RemindService : INService, IReadyExecutor, IRemindService
values[groupName] = value;
}
ts = new TimeSpan((30 * values["mo"]) + (7 * values["w"]) + values["d"], values["h"], values["m"], 0);
}
@@ -197,22 +199,24 @@ public class RemindService : INService, IReadyExecutor, IRemindService
if (st is SmartEmbedText set)
{
await ch.SendMessageAsync(null, embed: set.GetEmbed().Build());
await _sender.Response(ch).Embed(set.GetEmbed()).SendAsync();
}
else if (st is SmartEmbedTextArray seta)
{
await ch.SendMessageAsync(null, embeds: seta.GetEmbedBuilders().Map(x => x.Build()));
await _sender.Response(ch).Embeds(seta.GetEmbedBuilders()).SendAsync();
}
else
{
await ch.EmbedAsync(new EmbedBuilder()
.WithOkColor()
.WithTitle("Reminder")
.AddField("Created At",
r.DateAdded.HasValue ? r.DateAdded.Value.ToLongDateString() : "?")
.AddField("By",
(await ch.GetUserAsync(r.UserId))?.ToString() ?? r.UserId.ToString()),
r.Message);
await _sender.Response(ch)
.Embed(new EmbedBuilder()
.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();
}
}
catch (Exception ex)
@@ -227,7 +231,8 @@ public class RemindService : INService, IReadyExecutor, IRemindService
public TimeSpan Time { get; set; }
}
public async Task AddReminderAsync(ulong userId,
public async Task AddReminderAsync(
ulong userId,
ulong targetId,
ulong? guildId,
bool isPrivate,
@@ -242,7 +247,7 @@ public class RemindService : INService, IReadyExecutor, IRemindService
ServerId = guildId ?? 0,
IsPrivate = isPrivate,
When = time,
Message = message,
Message = message,
Type = reminderType
};

View File

@@ -1,4 +1,5 @@
using NadekoBot.Db.Models;
using System.Text;
namespace NadekoBot.Modules.Utility;
@@ -86,8 +87,8 @@ public partial class Utility
(curPage) =>
{
var eb = new EmbedBuilder()
.WithOkColor()
.WithTitle(GetText(strs.todo_list));
.WithOkColor()
.WithTitle(GetText(strs.todo_list));
ShowTodoItem(todos, curPage, eb);
@@ -99,15 +100,15 @@ public partial class Utility
private static void ShowTodoItem(IReadOnlyCollection<TodoModel> todos, int curPage, EmbedBuilder eb)
{
var sb = new StringBuilder();
foreach (var todo in todos.Skip(curPage * 9).Take(9))
{
// green circle and yellow circle emojis
eb.AddField($"-",
$"{(todo.IsDone
? ""
: "🟡")} {Format.Code(new kwum(todo.Id).ToString())} {todo.Todo}",
false);
sb.AppendLine($"{(todo.IsDone ? "" : "")} {Format.Code(new kwum(todo.Id).ToString())} {todo.Todo}");
sb.AppendLine("---");
}
eb.WithDescription(sb.ToString());
}
[Group("archive")]
@@ -150,8 +151,8 @@ public partial class Utility
(curPage) =>
{
var eb = new EmbedBuilder()
.WithTitle(GetText(strs.todo_archive_list))
.WithOkColor();
.WithTitle(GetText(strs.todo_archive_list))
.WithOkColor();
foreach (var archivedList in archivedTodoLists.Skip(curPage * 9).Take(9))
{
@@ -179,8 +180,8 @@ public partial class Utility
(curPage) =>
{
var eb = new EmbedBuilder()
.WithOkColor()
.WithTitle(GetText(strs.todo_list));
.WithOkColor()
.WithTitle(GetText(strs.todo_list));
ShowTodoItem(list.Items, curPage, eb);

View File

@@ -90,10 +90,10 @@ public partial class Utility
res = Math.Round(res, 4);
await Response()
.Confirm(GetText(strs.convert(value,
.Confirm(strs.convert(value,
originUnit.Triggers.Last(),
res,
targetUnit.Triggers.Last())))
targetUnit.Triggers.Last()))
.SendAsync();
}
}

View File

@@ -337,7 +337,7 @@ public partial class Utility : NadekoModule
if (string.IsNullOrWhiteSpace(result))
await Response().Error(strs.showemojis_none).SendAsync();
else
await ctx.Channel.SendMessageAsync(result.TrimTo(2000));
await Response().Text(result.TrimTo(2000)).SendAsync();
}
[Cmd]
@@ -613,7 +613,7 @@ public partial class Utility : NadekoModule
try
{
var sw = Stopwatch.StartNew();
var msg = await ctx.Channel.SendMessageAsync("🏓");
var msg = await Response().Text("🏓").SendAsync();
sw.Stop();
msg.DeleteAfter(0);

View File

@@ -9,16 +9,19 @@ public class VerboseErrorsService : INService
private readonly DbService _db;
private readonly CommandHandler _ch;
private readonly ICommandsUtilityService _hs;
private readonly IMessageSenderService _sender;
public VerboseErrorsService(
IBot bot,
DbService db,
CommandHandler ch,
IMessageSenderService sender,
ICommandsUtilityService hs)
{
_db = db;
_ch = ch;
_hs = hs;
_sender = sender;
_ch.CommandErrored += LogVerboseError;
@@ -38,7 +41,7 @@ public class VerboseErrorsService : INService
.WithFooter("Admin may disable verbose errors via `.ve` command")
.WithErrorColor();
await channel.EmbedAsync(embed);
await _sender.Response(channel).Embed(embed).SendAsync();
}
catch
{