Skip to content

Commit 4dbd562

Browse files
committed
DiscordSink: Add bot owner pings for bad exceptions
1 parent a320ccf commit 4dbd562

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

Commands/Debug.cs

+23
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,29 @@ public async Task SearchMembersCmd(CommandContext ctx, string regex)
615615
await ctx.Channel.SendMessageAsync(await StringHelpers.CodeOrHasteBinAsync(JsonConvert.SerializeObject(memberIdsTonames, Formatting.Indented), "json"));
616616
}
617617

618+
[Command("testnre")]
619+
[Description("throw a System.NullReferenceException error. dont spam this please.")]
620+
[IsBotOwner]
621+
public async Task ThrowNRE(CommandContext ctx, bool catchAsWarning = false)
622+
{
623+
if (catchAsWarning)
624+
{
625+
try
626+
{
627+
throw new NullReferenceException();
628+
}
629+
catch (NullReferenceException e)
630+
{
631+
ctx.Client.Logger.LogWarning(e, "logging test NRE as warning");
632+
await ctx.RespondAsync("thrown NRE and logged as warning, check logs");
633+
}
634+
}
635+
else
636+
{
637+
throw new NullReferenceException();
638+
}
639+
}
640+
618641
private static async Task<(bool success, ulong failedOverwrite)> ImportOverridesFromChannelAsync(DiscordChannel channel)
619642
{
620643
// Imports overrides from the specified channel to the database. See 'debug overrides import' and 'debug overrides importall'

Helpers/DiscordSink.cs

+26-5
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,40 @@ public async void Emit(LogEvent logEvent)
3939
return;
4040
}
4141

42-
if (dummyWriter.ToString().Length > 1984 && dummyWriter.ToString().Length < 4096)
42+
string extraText = "";
43+
44+
if (
45+
Program.cfgjson.PingBotOwnersOnBadErrors &&
46+
logEvent.Level >= LogEventLevel.Error &&
47+
logEvent.Exception is not null &&
48+
(
49+
logEvent.Exception.GetType() == typeof(NullReferenceException) ||
50+
logEvent.Exception.GetType() == typeof(RedisTimeoutException)
51+
)
52+
)
53+
{
54+
var pingList = Program.cfgjson.BotOwners.Select(x => $"<@{x}>").ToList();
55+
extraText = string.Join(", ", pingList);
56+
}
57+
58+
if (dummyWriter.ToString().Length > (1984 - extraText.Length) && dummyWriter.ToString().Length < (4096 - extraText.Length))
4359
{
44-
LogChannelHelper.LogMessageAsync("errors", new DiscordEmbedBuilder().WithDescription($"```cs\n{dummyWriter}\n```"));
60+
LogChannelHelper.LogMessageAsync("errors", new DiscordMessageBuilder().AddEmbed(new DiscordEmbedBuilder().WithDescription($"{extraText}\n```cs\n{dummyWriter}\n```")).WithAllowedMentions(Mentions.All));
4561

4662
}
47-
else if (dummyWriter.ToString().Length < 1984)
63+
else if (dummyWriter.ToString().Length < (1984 - extraText.Length))
4864
{
49-
LogChannelHelper.LogMessageAsync("errors", $"```cs\n{dummyWriter}\n```");
65+
LogChannelHelper.LogMessageAsync("errors", new DiscordMessageBuilder().WithContent($"{extraText}\n```cs\n{dummyWriter}\n```").WithAllowedMentions(Mentions.All));
5066
}
5167
else
5268
{
5369
var stream = new MemoryStream(Encoding.UTF8.GetBytes(dummyWriter.ToString()));
54-
LogChannelHelper.LogMessageAsync("errors", new DiscordMessageBuilder().AddFile("error.txt", stream));
70+
var resp = new DiscordMessageBuilder().AddFile("error.txt", stream).WithAllowedMentions(Mentions.All);
71+
if (extraText.Length > 0)
72+
{
73+
resp.WithContent(extraText);
74+
}
75+
LogChannelHelper.LogMessageAsync("errors", resp);
5576
}
5677
}
5778
catch

Structs.cs

+3
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ public ulong InsidersChannel
321321

322322
[JsonProperty("mentionTrackExcludedChannels")]
323323
public List<ulong> MentionTrackExcludedChannels { get; private set; } = new();
324+
325+
[JsonProperty("pingBotOwnersOnBadErrors")]
326+
public bool PingBotOwnersOnBadErrors { get; private set; } = false;
324327
}
325328

326329
public enum Level { Information, Warning, Error, Debug, Verbose }

config.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@
296296
},
297297
"botOwners": [
298298
228574821590499329,
299-
455432936339144705,
300-
159016432498114560
299+
455432936339144705
301300
],
302301
"ignoredVoiceChannels": [
303302
922260379470548992
@@ -342,5 +341,6 @@
342341
883400305679663156,
343342
593381041629298688,
344343
450181490345508884
345-
]
344+
],
345+
"pingBotOwnersOnBadErrors": true
346346
}

0 commit comments

Comments
 (0)