Skip to content

Commit

Permalink
Merge pull request #18 from EpicOfficer/FEATURE/Wordle
Browse files Browse the repository at this point in the history
Refactor WordleModule and group word definitions
  • Loading branch information
EpicOfficer authored Apr 10, 2024
2 parents 3f4bdd0 + ea8632b commit 5747bc6
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions Blink3.Bot/Modules/WordleModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Globalization;
using Blink3.Core.Entities;
using Blink3.Core.Extensions;
using Blink3.Core.Interfaces;
Expand Down Expand Up @@ -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")]
Expand All @@ -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);
}
}

0 comments on commit 5747bc6

Please sign in to comment.