diff --git a/PluralKit.API/Program.cs b/PluralKit.API/Program.cs index 4f746b57..4958baed 100644 --- a/PluralKit.API/Program.cs +++ b/PluralKit.API/Program.cs @@ -2,6 +2,8 @@ using Autofac.Extensions.DependencyInjection; using PluralKit.Core; +using Sentry; + using Serilog; namespace PluralKit.API; @@ -21,9 +23,16 @@ public class Program opts.Dsn = config.SentryUrl ?? ""; opts.Release = BuildInfoService.FullVersion; opts.AutoSessionTracking = true; - // opts.DisableTaskUnobservedTaskExceptionCapture(); + opts.DisableUnobservedTaskExceptionCapture(); }); + TaskScheduler.UnobservedTaskException += (_, e) => + { + foreach (var inner in e.Exception.Flatten().InnerExceptions) + SentrySdk.CaptureException(inner); + e.SetObserved(); + }; + await host.Services.GetRequiredService().InitAsync(config); await host.RunAsync(); } diff --git a/PluralKit.Bot/Commands/Message.cs b/PluralKit.Bot/Commands/Message.cs index c61dd88b..e947c6e8 100644 --- a/PluralKit.Bot/Commands/Message.cs +++ b/PluralKit.Bot/Commands/Message.cs @@ -442,7 +442,7 @@ public class ProxiedMessage return; } - await ctx.Reply(components: await _embeds.CreateAuthorMessageComponents(user, message)); + await ctx.Reply(components: _embeds.CreateAuthorMessageComponents(user, message)); return; } diff --git a/PluralKit.Bot/Handlers/MessageEdited.cs b/PluralKit.Bot/Handlers/MessageEdited.cs index a732fa5b..81b75a02 100644 --- a/PluralKit.Bot/Handlers/MessageEdited.cs +++ b/PluralKit.Bot/Handlers/MessageEdited.cs @@ -58,13 +58,19 @@ public class MessageEdited: IEventHandler var channel = await _cache.TryGetChannel(guildId, evt.ChannelId); // todo: is this correct for message update? if (channel == null) - throw new Exception("could not find self channel in MessageEdited event"); + { + _logger.Warning("could not find self channel in MessageEdited event"); + return; + } if (!DiscordUtils.IsValidGuildChannel(channel)) return; var rootChannel = await _cache.GetRootChannel(guildId, channel.Id); var guild = await _cache.TryGetGuild(channel.GuildId!.Value); if (guild == null) - throw new Exception("could not find self guild in MessageEdited event"); + { + _logger.Warning("could not find self guild in MessageEdited event"); + return; + } var lastMessage = (await _lastMessageCache.GetLastMessage(evt.GuildId.HasValue ? evt.GuildId.Value ?? 0 : 0, evt.ChannelId))?.Current; // Only react to the last message in the channel diff --git a/PluralKit.Bot/Init.cs b/PluralKit.Bot/Init.cs index 25b98f1b..7ccd3696 100644 --- a/PluralKit.Bot/Init.cs +++ b/PluralKit.Bot/Init.cs @@ -45,9 +45,16 @@ public class Init opts.Dsn = services.Resolve().SentryUrl ?? ""; opts.Release = BuildInfoService.FullVersion; opts.AutoSessionTracking = true; - // opts.DisableTaskUnobservedTaskExceptionCapture(); + opts.DisableUnobservedTaskExceptionCapture(); }); + TaskScheduler.UnobservedTaskException += (_, e) => + { + foreach (var inner in e.Exception.Flatten().InnerExceptions) + SentrySdk.CaptureException(inner); + e.SetObserved(); + }; + var config = services.Resolve(); var coreConfig = services.Resolve(); diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index f33d56f8..a5a4a123 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -1004,7 +1004,7 @@ public class EmbedService return eb.Build(); } - public async Task CreateAuthorMessageComponents(User? user, FullMessage msg) + public MessageComponent[] CreateAuthorMessageComponents(User? user, FullMessage msg) { MessageComponent authorInfo; var author = user != null @@ -1049,16 +1049,15 @@ public class EmbedService authorInfo, ] }; - return ( - [ - new MessageComponent() - { - Type = ComponentType.Text, - Content = user != null ? $"{user.Mention()} ({user.Id})" : $"*(deleted user {msg.Message.Sender})*" - }, - container - ] - ); + return + [ + new MessageComponent() + { + Type = ComponentType.Text, + Content = user != null ? $"{user.Mention()} ({user.Id})" : $"*(deleted user {msg.Message.Sender})*" + }, + container + ]; } public async Task CreateCommandMessageInfoMessageComponents(Core.CommandMessage msg, bool showContent) diff --git a/PluralKit.Bot/Services/LastMessageCacheService.cs b/PluralKit.Bot/Services/LastMessageCacheService.cs index 46a51c64..03db8b8e 100644 --- a/PluralKit.Bot/Services/LastMessageCacheService.cs +++ b/PluralKit.Bot/Services/LastMessageCacheService.cs @@ -32,7 +32,7 @@ public class LastMessageCacheService public async Task GetLastMessage(ulong guild, ulong channel) { if (_maybeHttp is HttpDiscordCache) - return await (_maybeHttp as HttpDiscordCache).GetLastMessage(guild, channel); + return await (_maybeHttp as HttpDiscordCache)!.GetLastMessage(guild, channel); return _cache.TryGetValue(channel, out var message) ? message : null; }