L3TDE-IRC/lib/L3TDE/Help.pm

60 lines
1.1 KiB
Perl

package L3TDE::Help;
use v5.34.1;
use strict;
use warnings;
use List::AllUtils qw/one/;
use Moo;
has _help => (
is => 'rw',
);
# We have to be sure every file which installs help gets loaded.
require L3TDE::Player;
require L3TDE::Bot;
my $single_instance;
sub singleton {
if (!$single_instance) {
$single_instance = __PACKAGE__->new;
}
return $single_instance;
}
sub BUILD {
my $self = shift;
$self->_help({});
}
sub search_help {
my $self = shift;
my $search_string = shift;
my $help_text = $self->_help->{$search_string};
if (defined $help_text) {
return $help_text;
}
my @matches = map { index($_, $search_string) != -1 ? ($_) : () } keys %{$self->_help};
if (!@matches) {
die "No match.";
}
if (@matches > 1) {
die "More than a match " . (join ', ', @matches);
}
return $self->_help->{$matches[0]};
}
sub install_help {
my $self = shift;
my ($key, $help_text) = @_;
if (defined $self->_help->{$key}) {
warn "Overwriting key $key. ¡This is an error!";
}
$self->_help->{$key} = $help_text;
}
1;