Add embed builder, some more ported classes

This commit is contained in:
Ske
2020-12-23 02:19:02 +01:00
parent 05334f0d25
commit f6fb8204bb
13 changed files with 305 additions and 189 deletions

View File

@@ -4,7 +4,8 @@ using System.Threading.Tasks;
using App.Metrics;
using DSharpPlus.Entities;
using Myriad.Builders;
using Myriad.Rest;
using NodaTime;
@@ -19,54 +20,61 @@ namespace PluralKit.Bot
private readonly IMetrics _metrics;
private readonly ILogger _logger;
private readonly DiscordApiClient _rest;
public ErrorMessageService(IMetrics metrics, ILogger logger)
public ErrorMessageService(IMetrics metrics, ILogger logger, DiscordApiClient rest)
{
_metrics = metrics;
_logger = logger;
_rest = rest;
}
public async Task SendErrorMessage(DiscordChannel channel, string errorId)
public async Task SendErrorMessage(ulong channelId, string errorId)
{
var now = SystemClock.Instance.GetCurrentInstant();
if (!ShouldSendErrorMessage(channel, now))
if (!ShouldSendErrorMessage(channelId, now))
{
_logger.Warning("Rate limited sending error message to {ChannelId} with error code {ErrorId}", channel.Id, errorId);
_logger.Warning("Rate limited sending error message to {ChannelId} with error code {ErrorId}", channelId, errorId);
_metrics.Measure.Meter.Mark(BotMetrics.ErrorMessagesSent, "throttled");
return;
}
var embed = new DiscordEmbedBuilder()
.WithColor(new DiscordColor(0xE74C3C))
.WithTitle("Internal error occurred")
.WithDescription("For support, please send the error code above in **#bug-reports-and-errors** on **[the support server *(click to join)*](https://discord.gg/PczBt78)** with a description of what you were doing at the time.")
.WithFooter(errorId)
.WithTimestamp(now.ToDateTimeOffset());
var embed = new EmbedBuilder()
.Color(0xE74C3C)
.Title("Internal error occurred")
.Description("For support, please send the error code above in **#bug-reports-and-errors** on **[the support server *(click to join)*](https://discord.gg/PczBt78)** with a description of what you were doing at the time.")
.Footer(new(errorId))
.Timestamp(now.ToDateTimeOffset().ToString("O"));
try
{
await channel.SendMessageAsync($"> **Error code:** `{errorId}`", embed: embed.Build());
_logger.Information("Sent error message to {ChannelId} with error code {ErrorId}", channel.Id, errorId);
await _rest.CreateMessage(channelId, new()
{
Content = $"> **Error code:** `{errorId}`",
Embed = embed.Build()
});
_logger.Information("Sent error message to {ChannelId} with error code {ErrorId}", channelId, errorId);
_metrics.Measure.Meter.Mark(BotMetrics.ErrorMessagesSent, "sent");
}
catch (Exception e)
{
_logger.Error(e, "Error sending error message to {ChannelId}", channel.Id);
_logger.Error(e, "Error sending error message to {ChannelId}", channelId);
_metrics.Measure.Meter.Mark(BotMetrics.ErrorMessagesSent, "failed");
throw;
}
}
private bool ShouldSendErrorMessage(DiscordChannel channel, Instant now)
private bool ShouldSendErrorMessage(ulong channelId, Instant now)
{
if (_lastErrorInChannel.TryGetValue(channel.Id, out var lastErrorTime))
if (_lastErrorInChannel.TryGetValue(channelId, out var lastErrorTime))
{
var interval = now - lastErrorTime;
if (interval < MinErrorInterval)
return false;
}
_lastErrorInChannel[channel.Id] = now;
_lastErrorInChannel[channelId] = now;
return true;
}
}