From c27d7f4f0ad3b25f7222ed736a07aa85257f1221 Mon Sep 17 00:00:00 2001 From: 13xforever Date: Wed, 2 Sep 2020 20:16:46 +0500 Subject: [PATCH] try to workaround new limits rolled out by discord also make a bunch of settings configurable --- CompatBot/Config.cs | 18 ++++++++++-------- CompatBot/EventHandlers/BotReactionsHandler.cs | 2 +- CompatBot/EventHandlers/DiscordInviteFilter.cs | 2 +- CompatBot/EventHandlers/LogParsingHandler.cs | 2 +- CompatBot/EventHandlers/Starbucks.cs | 6 +++--- .../EventHandlers/UsernameValidationMonitor.cs | 2 +- .../LogParserResultFormatter.cs | 2 +- CompatBot/Watchdog.cs | 4 ++-- 8 files changed, 20 insertions(+), 18 deletions(-) diff --git a/CompatBot/Config.cs b/CompatBot/Config.cs index 83b3abcf..9550ec6a 100644 --- a/CompatBot/Config.cs +++ b/CompatBot/Config.cs @@ -41,12 +41,6 @@ namespace CompatBot internal static readonly RecyclableMemoryStreamManager MemoryStreamManager = new RecyclableMemoryStreamManager(); public static readonly CancellationTokenSource Cts = new CancellationTokenSource(); - public static readonly TimeSpan ModerationTimeThreshold = TimeSpan.FromHours(12); - public static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30); - public static readonly TimeSpan LogParsingTimeout = TimeSpan.FromSeconds(30); - public static readonly TimeSpan BuildTimeDifferenceForOutdatedBuilds = TimeSpan.FromDays(3); - public static readonly TimeSpan ShutupTimeLimit = TimeSpan.FromMinutes(5); - public static readonly TimeSpan ForcedNicknamesRecheckTime = TimeSpan.FromHours(3); public static readonly Stopwatch Uptime = Stopwatch.StartNew(); // these settings could be configured either through `$ dotnet user-secrets`, or through environment variables (e.g. launchSettings.json, etc) @@ -61,6 +55,16 @@ namespace CompatBot public static ulong ThumbnailSpamId => config.GetValue(nameof(ThumbnailSpamId), 475678410098606100ul); // #bot-data; used for whatever bot needs to keep (cover embeds, etc) public static ulong BotAdminId => config.GetValue(nameof(BotAdminId), 267367850706993152ul); // discord user id for a bot admin public static ulong DeletedMessagesLogChannelId => config.GetValue(nameof(DeletedMessagesLogChannelId), 0ul); + + public static TimeSpan ModerationBacklogThresholdInHours => TimeSpan.FromHours(config.GetValue(nameof(ModerationBacklogThresholdInHours), 1)); + public static TimeSpan DefaultTimeoutInSec => TimeSpan.FromSeconds(config.GetValue(nameof(DefaultTimeoutInSec), 30)); + public static TimeSpan LogParsingTimeoutInSec => TimeSpan.FromSeconds(config.GetValue(nameof(LogParsingTimeoutInSec), 30)); + public static TimeSpan BuildTimeDifferenceForOutdatedBuildsInDays => TimeSpan.FromDays(config.GetValue(nameof(BuildTimeDifferenceForOutdatedBuildsInDays), 3)); + public static TimeSpan ShutupTimeLimitInMin => TimeSpan.FromMinutes(config.GetValue(nameof(ShutupTimeLimitInMin), 5)); + public static TimeSpan ForcedNicknamesRecheckTimeInHours => TimeSpan.FromHours(config.GetValue(nameof(ForcedNicknamesRecheckTimeInHours), 3)); + public static TimeSpan IncomingMessageCheckIntervalInMin => TimeSpan.FromMinutes(config.GetValue(nameof(IncomingMessageCheckIntervalInMin), 10)); + public static TimeSpan MetricsIntervalInSec => TimeSpan.FromSeconds(config.GetValue(nameof(MetricsIntervalInSec), 10)); + public static int ProductCodeLookupHistoryThrottle => config.GetValue(nameof(ProductCodeLookupHistoryThrottle), 7); public static int TopLimit => config.GetValue(nameof(TopLimit), 15); public static int AttachmentSizeLimit => config.GetValue(nameof(AttachmentSizeLimit), 8 * 1024 * 1024); @@ -70,8 +74,6 @@ namespace CompatBot public static int BuildNumberDifferenceForOutdatedBuilds => config.GetValue(nameof(BuildNumberDifferenceForOutdatedBuilds), 10); public static int MinimumPiracyTriggerLength => config.GetValue(nameof(MinimumPiracyTriggerLength), 4); public static int MaxSyscallResultLines => config.GetValue(nameof(MaxSyscallResultLines), 13); - public static TimeSpan IncomingMessageCheckIntervalInMinutes => TimeSpan.FromMinutes(config.GetValue(nameof(IncomingMessageCheckIntervalInMinutes), 10)); - public static TimeSpan MetricsIntervalInSeconds => TimeSpan.FromSeconds(config.GetValue(nameof(MetricsIntervalInSeconds), 10)); public static string Token => config.GetValue(nameof(Token), ""); public static string AzureDevOpsToken => config.GetValue(nameof(AzureDevOpsToken), ""); public static string AzureComputerVisionKey => config.GetValue(nameof(AzureComputerVisionKey), ""); diff --git a/CompatBot/EventHandlers/BotReactionsHandler.cs b/CompatBot/EventHandlers/BotReactionsHandler.cs index 119e44c4..53c3e8b6 100644 --- a/CompatBot/EventHandlers/BotReactionsHandler.cs +++ b/CompatBot/EventHandlers/BotReactionsHandler.cs @@ -186,7 +186,7 @@ namespace CompatBot.EventHandlers var botMember = args.Guild?.CurrentMember ?? args.Client.GetMember(args.Client.CurrentUser); if (args.Channel.PermissionsFor(botMember).HasPermission(Permissions.ReadMessageHistory)) { - var lastBotMessages = await args.Channel.GetMessagesBeforeAsync(args.Message.Id, 20, DateTime.UtcNow.Add(-Config.ShutupTimeLimit)).ConfigureAwait(false); + var lastBotMessages = await args.Channel.GetMessagesBeforeAsync(args.Message.Id, 20, DateTime.UtcNow.Add(-Config.ShutupTimeLimitInMin)).ConfigureAwait(false); if (lastBotMessages.OrderByDescending(m => m.CreationTimestamp).FirstOrDefault(m => m.Author.IsCurrent) is DiscordMessage msg) await msg.DeleteAsync("asked to shut up").ConfigureAwait(false); } diff --git a/CompatBot/EventHandlers/DiscordInviteFilter.cs b/CompatBot/EventHandlers/DiscordInviteFilter.cs index 4df1fe79..0275c2b9 100644 --- a/CompatBot/EventHandlers/DiscordInviteFilter.cs +++ b/CompatBot/EventHandlers/DiscordInviteFilter.cs @@ -54,7 +54,7 @@ namespace CompatBot.EventHandlers } } - var after = DateTime.UtcNow - Config.ModerationTimeThreshold; + var after = DateTime.UtcNow - Config.ModerationBacklogThresholdInHours; foreach (var channel in guild.Channels.Values.Where(ch => !ch.IsCategory && ch.Type != ChannelType.Voice)) { var permissions = channel.PermissionsFor(botMember); diff --git a/CompatBot/EventHandlers/LogParsingHandler.cs b/CompatBot/EventHandlers/LogParsingHandler.cs index 7b58407b..8f3d3469 100644 --- a/CompatBot/EventHandlers/LogParsingHandler.cs +++ b/CompatBot/EventHandlers/LogParsingHandler.cs @@ -101,7 +101,7 @@ namespace CompatBot.EventHandlers parsedLog = true; LogParseState result = null, tmpResult = null; - using (var timeout = new CancellationTokenSource(Config.LogParsingTimeout)) + using (var timeout = new CancellationTokenSource(Config.LogParsingTimeoutInSec)) { using var combinedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, Config.Cts.Token); var tries = 0; diff --git a/CompatBot/EventHandlers/Starbucks.cs b/CompatBot/EventHandlers/Starbucks.cs index 5876f39c..c65bffdf 100644 --- a/CompatBot/EventHandlers/Starbucks.cs +++ b/CompatBot/EventHandlers/Starbucks.cs @@ -93,7 +93,7 @@ namespace CompatBot.EventHandlers { try { - var after = DateTime.UtcNow - Config.ModerationTimeThreshold; + var after = DateTime.UtcNow - Config.ModerationBacklogThresholdInHours; var checkTasks = new List(); foreach (var channel in guild.Channels.Values.Where(ch => Config.Moderation.Channels.Contains(ch.Id))) { @@ -146,7 +146,7 @@ namespace CompatBot.EventHandlers return; // message.Timestamp throws if it's not in the cache AND is in local time zone - if (DateTime.UtcNow - message.CreationTimestamp > Config.ModerationTimeThreshold) + if (DateTime.UtcNow - message.CreationTimestamp > Config.ModerationBacklogThresholdInHours) return; if (message.Reactions.Any(r => r.Emoji == emoji && (r.IsMe || r.Count < Config.Moderation.StarbucksThreshold))) @@ -182,7 +182,7 @@ namespace CompatBot.EventHandlers if (!message.Author.IsCurrent) return Task.CompletedTask; - if (message.CreationTimestamp.Add(Config.ShutupTimeLimit) < DateTime.UtcNow) + if (message.CreationTimestamp.Add(Config.ShutupTimeLimitInMin) < DateTime.UtcNow) return Task.CompletedTask; if (!user.IsWhitelisted(client, message.Channel.Guild)) diff --git a/CompatBot/EventHandlers/UsernameValidationMonitor.cs b/CompatBot/EventHandlers/UsernameValidationMonitor.cs index d1bc53c5..3b6210c4 100644 --- a/CompatBot/EventHandlers/UsernameValidationMonitor.cs +++ b/CompatBot/EventHandlers/UsernameValidationMonitor.cs @@ -49,7 +49,7 @@ namespace CompatBot.EventHandlers do { if (!once) - await Task.Delay(Config.ForcedNicknamesRecheckTime, Config.Cts.Token).ConfigureAwait(false); + await Task.Delay(Config.ForcedNicknamesRecheckTimeInHours, Config.Cts.Token).ConfigureAwait(false); if (await Moderation.Audit.CheckLock.WaitAsync(0).ConfigureAwait(false)) try { diff --git a/CompatBot/Utils/ResultFormatters/LogParserResultFormatter.cs b/CompatBot/Utils/ResultFormatters/LogParserResultFormatter.cs index cc7746f5..3baae212 100644 --- a/CompatBot/Utils/ResultFormatters/LogParserResultFormatter.cs +++ b/CompatBot/Utils/ResultFormatters/LogParserResultFormatter.cs @@ -485,7 +485,7 @@ namespace CompatBot.Utils.ResultFormatters private static bool VersionIsTooOld(NameValueCollection items, Match update, UpdateInfo updateInfo) { - if ((updateInfo.GetUpdateDelta() is TimeSpan updateTimeDelta) && (updateTimeDelta < Config.BuildTimeDifferenceForOutdatedBuilds)) + if ((updateInfo.GetUpdateDelta() is TimeSpan updateTimeDelta) && (updateTimeDelta < Config.BuildTimeDifferenceForOutdatedBuildsInDays)) return false; if (Version.TryParse(items["build_version"], out var logVersion) && Version.TryParse(update.Groups["version"].Value, out var updateVersion)) diff --git a/CompatBot/Watchdog.cs b/CompatBot/Watchdog.cs index f884c11c..b112af7c 100644 --- a/CompatBot/Watchdog.cs +++ b/CompatBot/Watchdog.cs @@ -17,7 +17,7 @@ namespace CompatBot private static readonly TimeSpan CheckInterval = TimeSpan.FromSeconds(10); public static readonly ConcurrentQueue DisconnectTimestamps = new ConcurrentQueue(); public static readonly Stopwatch TimeSinceLastIncomingMessage = Stopwatch.StartNew(); - private static bool IsOk => DisconnectTimestamps.IsEmpty && TimeSinceLastIncomingMessage.Elapsed < Config.IncomingMessageCheckIntervalInMinutes; + private static bool IsOk => DisconnectTimestamps.IsEmpty && TimeSinceLastIncomingMessage.Elapsed < Config.IncomingMessageCheckIntervalInMin; private static DiscordClient discordClient = null; public static async Task Watch(DiscordClient client) @@ -92,7 +92,7 @@ namespace CompatBot { do { - await Task.Delay(Config.MetricsIntervalInSeconds).ConfigureAwait(false); + await Task.Delay(Config.MetricsIntervalInSec).ConfigureAwait(false); if (Config.TelemetryClient is TelemetryClient tc) { tc.TrackMetric("gw-latency", client.Ping);