From ea8632bd978df8c0f3fbac6159644e63b8e7354f Mon Sep 17 00:00:00 2001 From: EpicOfficer Date: Wed, 10 Apr 2024 15:21:54 +0100 Subject: [PATCH] Refactor WordleModule and group word definitions The WordleModule has been refactored with minor adjustments to improve code readability and slight enhancements. Definitions in "define" command now group by parts of speech, potentially providing clearer explanations for the user. The unnecessary System.Globalization namespace usage has also been removed. --- Blink3.Bot/Modules/WordleModule.cs | 38 ++++++++++++++++++------------ 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/Blink3.Bot/Modules/WordleModule.cs b/Blink3.Bot/Modules/WordleModule.cs index 5356e9b..0ba77c5 100644 --- a/Blink3.Bot/Modules/WordleModule.cs +++ b/Blink3.Bot/Modules/WordleModule.cs @@ -1,4 +1,3 @@ -using System.Globalization; using Blink3.Core.Entities; using Blink3.Core.Extensions; using Blink3.Core.Interfaces; @@ -70,13 +69,14 @@ await RespondErrorAsync("No game in progress", await wordleRepository.DeleteAsync(wordle); } - using MemoryStream image = new MemoryStream(); + using MemoryStream image = new(); await wordleGameService.GenerateImageAsync(guess, image); FileAttachment attachment = new(image, $"{guess.Word}.png"); ComponentBuilder? component = new ComponentBuilder().WithButton("Define", $"blink-define-word_{guess.Word}"); - - await FollowupWithFileAsync(text: text, attachment: attachment, ephemeral: false, components: component.Build()); + + await FollowupWithFileAsync(text: text, attachment: attachment, ephemeral: false, + components: component.Build()); } [SlashCommand("define", "Get the definition of a word")] @@ -91,21 +91,29 @@ public async Task Define(string word) catch { await RespondErrorAsync(word.ToTitleCase(), "An error occured fetching word definition", - ephemeral: true); + true); return; } - EmbedFieldBuilder[]? fields = details?.Definitions.Select(wordDetails => new EmbedFieldBuilder - { - Name = wordDetails.PartOfSpeech.ToTitleCase(), - Value = wordDetails.Definition.ToSentenceCase(), - IsInline = false - }).ToArray(); - + EmbedFieldBuilder[]? groupedDefinitions = details?.Definitions + .GroupBy(wd => wd.PartOfSpeech) + .Select(g => + { + EmbedFieldBuilder? builder = new() + { + Name = g.Key.ToTitleCase(), + Value = string.Join("\n", + g.Select(v => $"- {v.Definition.ToSentenceCase()}")), + IsInline = false + }; + return builder; + }) + .ToArray(); + await RespondPlainAsync( - name: word.ToTitleCase(), - message: fields?.Length < 1 ? "Could not find a definition" : string.Empty, - embedFields: fields, + $"Definition of {word.ToTitleCase()}", + groupedDefinitions?.Length < 1 ? "Could not find a definition" : string.Empty, + embedFields: groupedDefinitions, ephemeral: false); } } \ No newline at end of file