From 4f6c98db4897ea0dcb17cdb6f9dd3e51de763183 Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 20 Sep 2020 22:36:04 +0200 Subject: [PATCH] (Add the actual service file, shh) --- PluralKit.Bot/Services/ErrorMessageService.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 PluralKit.Bot/Services/ErrorMessageService.cs diff --git a/PluralKit.Bot/Services/ErrorMessageService.cs b/PluralKit.Bot/Services/ErrorMessageService.cs new file mode 100644 index 00000000..48a4cd5c --- /dev/null +++ b/PluralKit.Bot/Services/ErrorMessageService.cs @@ -0,0 +1,35 @@ +using System.Collections.Concurrent; +using System.Threading.Tasks; + +using DSharpPlus.Entities; + +using NodaTime; + +namespace PluralKit.Bot +{ + public class ErrorMessageService + { + private static readonly Duration MinErrorInterval = Duration.FromSeconds(10); + private readonly ConcurrentDictionary _lastErrorInChannel = new ConcurrentDictionary(); + + public async Task SendErrorMessage(DiscordChannel channel, string errorId) + { + var now = SystemClock.Instance.GetCurrentInstant(); + if (_lastErrorInChannel.TryGetValue(channel.Id, out var lastErrorTime)) + { + var interval = now - lastErrorTime; + if (interval < MinErrorInterval) + return; + } + _lastErrorInChannel[channel.Id] = now; + + 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()); + await channel.SendMessageAsync($"> **Error code:** `{errorId}`", embed: embed.Build()); + } + } +} \ No newline at end of file