From d4880854dba1ab260454704d24f7d257deea09e0 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Thu, 22 Feb 2024 18:56:52 -0800 Subject: [PATCH] Plugin/WordMorph: add search subcommand to find words by regex --- lib/PBot/Plugin/WordMorph.pm | 34 +++++++++++++++++++++++++++++++++- lib/PBot/VERSION.pm | 4 ++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/PBot/Plugin/WordMorph.pm b/lib/PBot/Plugin/WordMorph.pm index ca2a6c1e..1ff9f4fa 100644 --- a/lib/PBot/Plugin/WordMorph.pm +++ b/lib/PBot/Plugin/WordMorph.pm @@ -32,7 +32,7 @@ sub unload($self) { } use constant { - USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom ( | ) | solve | hint [from direction] | check | neighbors | show | giveup', + USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom ( | ) | solve | hint [from direction] | check | neighbors | search | show | giveup', NO_MORPH_AVAILABLE => "There is no word morph available. Use `wordmorph start [steps to solve [word length]]` to create one.", DB_UNAVAILABLE => "Word morph database not available.", LEFT => 0, @@ -270,6 +270,38 @@ sub wordmorph($self, $context) { return "New word morph: " . $self->show_morph_with_blanks($channel) . " (Fill in the blanks)"; } + when ('search') { + if (not @args) { + return "Usage: wordmorph search "; + } + + return DB_UNAVAILABLE if not $self->{db}; + + my @words; + + eval { + foreach my $length (keys $self->{db}->%*) { + foreach my $word (keys $self->{db}->{$length}->%*) { + + if ($word =~ m/$args[0]/) { + push @words, $word; + } + } + } + }; + + if (my $except = $@) { + $except =~ s/ at \/home.*$//; + return "Error: $except"; + } + + if (not @words) { + return "No matching words found."; + } + + return scalar @words . (@words == 1 ? ' word' : ' words') . ': ' . join(' ', @words); + } + when ('solve') { if (not @args) { return "Usage: wordmorph solve "; diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index e102d7b4..c576d65e 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,8 +25,8 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4702, - BUILD_DATE => "2024-01-26", + BUILD_REVISION => 4703, + BUILD_DATE => "2024-02-22", }; sub initialize {}