From ee64a9eee68e858a3588527b9f0b048630ea1114 Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Sun, 4 May 2025 21:44:28 -0500 Subject: [PATCH 01/12] Add ReloadTipDatabase to TipService.cs --- DiscordBot/Services/Tips/TipService.cs | 29 ++++++++++++++++---------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/DiscordBot/Services/Tips/TipService.cs b/DiscordBot/Services/Tips/TipService.cs index b94490c0..76abf1c7 100644 --- a/DiscordBot/Services/Tips/TipService.cs +++ b/DiscordBot/Services/Tips/TipService.cs @@ -76,16 +76,8 @@ private void Initialize() _loggingService.LogAction($"[{ServiceName}] Tip directory contains {new DirectoryInfo(_imageDirectory).EnumerateFiles("*.*", SearchOption.AllDirectories).Count()} files.", ExtendedLogSeverity.Info); } - - if (File.Exists(jsonPath)) - { - var json = File.ReadAllText(jsonPath); - _tips = JsonConvert.DeserializeObject>>(json); - _loggingService.LogAction( - $"[{ServiceName}] Tip index has {_tips.Count} keywords.", - ExtendedLogSeverity.Info); - //NOTE: elements of type Tip are not de-duplicated after loading - } + + var blocking = ReloadTipDatabase().Result; } _isRunning = true; @@ -271,11 +263,26 @@ public async Task ReplaceTip(IUserMessage message, Tip tip, string content) // REVIEW: causes two CommitTipDatabase calls } + public async Task ReloadTipDatabase() + { + if (File.Exists(jsonPath)) + { + var json = File.ReadAllText(jsonPath); + _tips = JsonConvert.DeserializeObject>>(json); + _loggingService.LogAction( + $"[{ServiceName}] Tip index has {_tips.Count} keywords.", + ExtendedLogSeverity.Info); + //NOTE: elements of type Tip are not de-duplicated after loading + } + } + private async Task CommitTipDatabase() { // In same folder, we save json files var jsonPath = GetTipPath(DatabaseName); - await File.WriteAllTextAsync(jsonPath, JsonConvert.SerializeObject(_tips)); + await File.WriteAllTextAsync(jsonPath, + JsonConvert.SerializeObject(_tips, + Formatting.Indented)); } public string DumpTipDatabase() From 7be1d837f2c430cffb31caf13d7343551d54ff35 Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Sun, 4 May 2025 21:49:00 -0500 Subject: [PATCH 02/12] Update TipService.cs --- DiscordBot/Services/Tips/TipService.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DiscordBot/Services/Tips/TipService.cs b/DiscordBot/Services/Tips/TipService.cs index 76abf1c7..26903d67 100644 --- a/DiscordBot/Services/Tips/TipService.cs +++ b/DiscordBot/Services/Tips/TipService.cs @@ -77,7 +77,7 @@ private void Initialize() ExtendedLogSeverity.Info); } - var blocking = ReloadTipDatabase().Result; + var blocking = ReloadTipDatabase(); } _isRunning = true; @@ -265,6 +265,7 @@ public async Task ReplaceTip(IUserMessage message, Tip tip, string content) public async Task ReloadTipDatabase() { + var jsonPath = GetTipPath(DatabaseName);; if (File.Exists(jsonPath)) { var json = File.ReadAllText(jsonPath); From 277cd04d672f1f121d8697a3e25180c8c502d428 Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Sun, 4 May 2025 21:51:16 -0500 Subject: [PATCH 03/12] Add !reloadtips to TipModule.cs --- DiscordBot/Modules/TipModule.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/DiscordBot/Modules/TipModule.cs b/DiscordBot/Modules/TipModule.cs index 0423737e..8cd4b96d 100644 --- a/DiscordBot/Modules/TipModule.cs +++ b/DiscordBot/Modules/TipModule.cs @@ -109,7 +109,8 @@ public async Task ReplaceTip(ulong tipId, string content = "") await TipService.ReplaceTip(Context.Message, tip, content); } - + +#if false [Command("DumpTips")] [Summary("For debugging, view the tip index.")] [RequireModerator] @@ -138,6 +139,17 @@ await Context.Channel.SendMessageAsync( await Task.Delay(chunkTime); } } +#endif + + [Command("ReloadTips")] + [Summary("Reload the database of tips.")] + [RequireModerator] + public async Task ReloadTipDatabase() + { + // rare usage, but in case someone with a shell decides + // to edit the json for debugging/expansion reasons... + await TipService.ReloadTipDatabase(); + } [Command("ListTips")] [Summary("List available tips by their keywords.")] From fdf118aeddba7a00998e160eb89857a8fe4dda85 Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Sun, 4 May 2025 22:31:47 -0500 Subject: [PATCH 04/12] Update TipModule.cs --- DiscordBot/Modules/TipModule.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/DiscordBot/Modules/TipModule.cs b/DiscordBot/Modules/TipModule.cs index 8cd4b96d..91037ee2 100644 --- a/DiscordBot/Modules/TipModule.cs +++ b/DiscordBot/Modules/TipModule.cs @@ -149,6 +149,7 @@ public async Task ReloadTipDatabase() // rare usage, but in case someone with a shell decides // to edit the json for debugging/expansion reasons... await TipService.ReloadTipDatabase(); + await ReplyAsync("Tip index reloaded."); } [Command("ListTips")] From 0e00d28c1fda380bfedaa45b03dc19aa34d138bf Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Mon, 5 May 2025 17:52:11 -0500 Subject: [PATCH 05/12] Add de-duplication and reference handling to TipService.cs --- DiscordBot/Services/Tips/TipService.cs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/DiscordBot/Services/Tips/TipService.cs b/DiscordBot/Services/Tips/TipService.cs index 26903d67..1b3acb8b 100644 --- a/DiscordBot/Services/Tips/TipService.cs +++ b/DiscordBot/Services/Tips/TipService.cs @@ -273,7 +273,21 @@ public async Task ReloadTipDatabase() _loggingService.LogAction( $"[{ServiceName}] Tip index has {_tips.Count} keywords.", ExtendedLogSeverity.Info); - //NOTE: elements of type Tip are not de-duplicated after loading + } + //NOTE: elements of type Tip are not de-duplicated after loading in earlier versions + // probably some Linq clever way of de-duplicating + var tips = new Dictionary(); + foreach (string keyword in _tips.Keys) + { + var list = _tips[keyword]; + for (int i = 0; i < list.Count; i++) + { + ulong id = list[i].Id; + if (tips.ContainsKey(id)) + list[i] = tips[id]; + else + tips[id] = list[i]; + } } } @@ -281,9 +295,14 @@ private async Task CommitTipDatabase() { // In same folder, we save json files var jsonPath = GetTipPath(DatabaseName); + var settings = new JsonSerializerSettings + { + PreserveReferencesHandling = PreserveReferencesHandling.Objects + }; await File.WriteAllTextAsync(jsonPath, JsonConvert.SerializeObject(_tips, - Formatting.Indented)); + Formatting.Indented, + settings)); } public string DumpTipDatabase() From 5eec8cfab01f4943d616867b002b9696c0526cca Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Mon, 5 May 2025 17:55:38 -0500 Subject: [PATCH 06/12] Update TipService.cs --- DiscordBot/Services/Tips/TipService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DiscordBot/Services/Tips/TipService.cs b/DiscordBot/Services/Tips/TipService.cs index 1b3acb8b..eb6960b0 100644 --- a/DiscordBot/Services/Tips/TipService.cs +++ b/DiscordBot/Services/Tips/TipService.cs @@ -283,8 +283,8 @@ public async Task ReloadTipDatabase() for (int i = 0; i < list.Count; i++) { ulong id = list[i].Id; - if (tips.ContainsKey(id)) - list[i] = tips[id]; + if (tips.TryGetValue(id, out var tip)) + list[i] = tip; else tips[id] = list[i]; } From 2390b1569c21f38a88fbb78c080c7bc16a2a1471 Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Mon, 5 May 2025 18:07:08 -0500 Subject: [PATCH 07/12] Update TipService.cs --- DiscordBot/Services/Tips/TipService.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/DiscordBot/Services/Tips/TipService.cs b/DiscordBot/Services/Tips/TipService.cs index eb6960b0..77a0b128 100644 --- a/DiscordBot/Services/Tips/TipService.cs +++ b/DiscordBot/Services/Tips/TipService.cs @@ -274,8 +274,10 @@ public async Task ReloadTipDatabase() $"[{ServiceName}] Tip index has {_tips.Count} keywords.", ExtendedLogSeverity.Info); } + //NOTE: elements of type Tip are not de-duplicated after loading in earlier versions // probably some Linq clever way of de-duplicating + bool touched = false; var tips = new Dictionary(); foreach (string keyword in _tips.Keys) { @@ -284,11 +286,19 @@ public async Task ReloadTipDatabase() { ulong id = list[i].Id; if (tips.TryGetValue(id, out var tip)) - list[i] = tip; + { list[i] = tip; touched = true; } else tips[id] = list[i]; } } + + if (touched) + { + _loggingService.LogAction( + $"[{ServiceName}] Tip index was de-duplicated.", + ExtendedLogSeverity.Info); + await CommitTipDatabase(); + } } private async Task CommitTipDatabase() From 6b3da34eac1d18af234280e94de606f063668a95 Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Mon, 5 May 2025 18:17:01 -0500 Subject: [PATCH 08/12] Update TipService.cs --- DiscordBot/Services/Tips/TipService.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/DiscordBot/Services/Tips/TipService.cs b/DiscordBot/Services/Tips/TipService.cs index 77a0b128..5f568ff4 100644 --- a/DiscordBot/Services/Tips/TipService.cs +++ b/DiscordBot/Services/Tips/TipService.cs @@ -286,9 +286,17 @@ public async Task ReloadTipDatabase() { ulong id = list[i].Id; if (tips.TryGetValue(id, out var tip)) - { list[i] = tip; touched = true; } + { + if (!Object.ReferenceEquals(list[i], tip)) + { + list[i] = tip; + touched = true; + } + } else + { tips[id] = list[i]; + } } } From 9b24d7bb157031b27528060a82d6bf2b57dcc002 Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Mon, 5 May 2025 18:27:30 -0500 Subject: [PATCH 09/12] Add Requests count to Tip.cs --- DiscordBot/Services/Tips/Components/Tip.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/DiscordBot/Services/Tips/Components/Tip.cs b/DiscordBot/Services/Tips/Components/Tip.cs index f7962ad4..bfaab3f4 100644 --- a/DiscordBot/Services/Tips/Components/Tip.cs +++ b/DiscordBot/Services/Tips/Components/Tip.cs @@ -8,4 +8,5 @@ public class Tip: IEntity public string Content { get; set; } public List Keywords { get; set; } public List ImagePaths { get; set; } + public int Requests { get; set; } } From d8c5ef2250641c8104065f45abb37d7463e1b370 Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Mon, 5 May 2025 18:27:48 -0500 Subject: [PATCH 10/12] Make CommitTipDatabase public in TipService.cs --- DiscordBot/Services/Tips/TipService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DiscordBot/Services/Tips/TipService.cs b/DiscordBot/Services/Tips/TipService.cs index 5f568ff4..24cd0404 100644 --- a/DiscordBot/Services/Tips/TipService.cs +++ b/DiscordBot/Services/Tips/TipService.cs @@ -309,7 +309,7 @@ public async Task ReloadTipDatabase() } } - private async Task CommitTipDatabase() + public async Task CommitTipDatabase() { // In same folder, we save json files var jsonPath = GetTipPath(DatabaseName); From b1635705923356ebb189e954cc1570886ce017e5 Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Mon, 5 May 2025 18:28:33 -0500 Subject: [PATCH 11/12] Increment request count in TipModule.cs --- DiscordBot/Modules/TipModule.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/DiscordBot/Modules/TipModule.cs b/DiscordBot/Modules/TipModule.cs index 91037ee2..90eb2cc4 100644 --- a/DiscordBot/Modules/TipModule.cs +++ b/DiscordBot/Modules/TipModule.cs @@ -31,6 +31,9 @@ public async Task Tip(string keywords) return; } + foreach (var tip in tips) + tip.Requests++; + var isAnyTextTips = tips.Any(tip => !string.IsNullOrEmpty(tip.Content)); var builder = new EmbedBuilder(); if (isAnyTextTips) @@ -46,7 +49,7 @@ public async Task Tip(string keywords) } var attachments = tips - .Where(tip => tip.ImagePaths != null && tip.ImagePaths.Any()) + .Where(tip => tip.ImagePaths?.Any()) .SelectMany(tip => tip.ImagePaths) .Select(imagePath => new FileAttachment(TipService.GetTipPath(imagePath))) .ToList(); @@ -70,6 +73,7 @@ public async Task Tip(string keywords) var ids = string.Join(" ", tips.Select(t => t.Id.ToString()).ToArray()); await ReplyAsync($"-# Tip ID {ids}"); await Context.Message.DeleteAsync(); + await TipService.CommitTipDatabase(); } [Command("AddTip")] From 48ea798f7af3486eacb0b1641e7d3bc3248c2080 Mon Sep 17 00:00:00 2001 From: Ed Halley <1223980+hariedo@users.noreply.github.com> Date: Mon, 5 May 2025 18:30:46 -0500 Subject: [PATCH 12/12] Update TipModule.cs --- DiscordBot/Modules/TipModule.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DiscordBot/Modules/TipModule.cs b/DiscordBot/Modules/TipModule.cs index 90eb2cc4..85d94075 100644 --- a/DiscordBot/Modules/TipModule.cs +++ b/DiscordBot/Modules/TipModule.cs @@ -49,7 +49,7 @@ public async Task Tip(string keywords) } var attachments = tips - .Where(tip => tip.ImagePaths?.Any()) + .Where(tip => tip.ImagePaths != null && tip.ImagePaths.Any()) .SelectMany(tip => tip.ImagePaths) .Select(imagePath => new FileAttachment(TipService.GetTipPath(imagePath))) .ToList();