From ff298a386290fdc32b1ee9e8c3e789d853727433 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Wed, 25 Aug 2021 21:25:18 -0700 Subject: [PATCH] Factoids: add `factoid` command The `factoid` commands interprets its arguments as an anonymous factoid. Useful for testing factoid actions without creating and deleting them. E.g.: !factoid /say $args[1:2] --args 'testing hello world args' hello world --- lib/PBot/Core/Commands/Factoids.pm | 50 +++++++++++++++++++++++++++--- lib/PBot/VERSION.pm | 2 +- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/lib/PBot/Core/Commands/Factoids.pm b/lib/PBot/Core/Commands/Factoids.pm index 08e4c009..1766e113 100644 --- a/lib/PBot/Core/Commands/Factoids.pm +++ b/lib/PBot/Core/Commands/Factoids.pm @@ -61,6 +61,7 @@ sub initialize { $self->{pbot}->{commands}->register(sub { $self->cmd_factalias(@_) }, "factalias", 0); $self->{pbot}->{commands}->register(sub { $self->cmd_factmove(@_) }, "factmove", 0); $self->{pbot}->{commands}->register(sub { $self->cmd_call_factoid(@_) }, "fact", 0); + $self->{pbot}->{commands}->register(sub { $self->cmd_as_factoid(@_) }, "factoid", 0); $self->{pbot}->{commands}->register(sub { $self->cmd_factfind(@_) }, "factfind", 0); $self->{pbot}->{commands}->register(sub { $self->cmd_top20(@_) }, "top20", 0); $self->{pbot}->{commands}->register(sub { $self->cmd_histogram(@_) }, "histogram", 0); @@ -87,6 +88,47 @@ sub cmd_call_factoid { return $self->{pbot}->{factoids}->{interpreter}->interpreter($context); } +sub cmd_as_factoid { + my ($self, $context) = @_; + + my $arguments = $context->{arguments}; + + my $usage = "Usage: factoid [--args 'arguments passed to factoid']"; + + return $usage if not length $arguments; + + my ($args); + + my %opts = ( + args => \$args, + ); + + my ($opt_args, $opt_error) = $self->{pbot}->{interpreter}->getopt( + $arguments, + \%opts, + ['bundling'], + 'args=s', + ); + + return "/say $opt_error -- $usage" if defined $opt_error; + return $usage if not @$opt_args; + + my $action = "@$opt_args"; + + my $trigger = "__anon-$context->{nick}-" . $self->{pbot}->random_nick(4); + + $self->{pbot}->{factoids}->{data}->add('text', $context->{from}, $context->{hostmask}, $trigger, $action); + + $context->{keyword} = $trigger; + $context->{arguments} = $args // ''; + + my $result = $self->{pbot}->{factoids}->{interpreter}->interpreter($context); + + $self->{pbot}->{factoids}->{data}->remove($context->{from}, $trigger); + + return $result; +} + sub cmd_factundo { my ($self, $context) = @_; my $usage = "Usage: factundo [-l [N]] [-r N] [channel] (-l list undo history, optionally starting from N; -r jump to revision N)"; @@ -112,7 +154,7 @@ sub cmd_factundo { return $usage if @$opt_args > 2; return $usage if not @$opt_args; - $arguments = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @$opt_args); + $arguments = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_ } @$opt_args); my $arglist = $self->{pbot}->{interpreter}->make_args($arguments); my ($channel, $trigger) = $self->find_factoid_with_optional_channel( @@ -218,7 +260,7 @@ sub cmd_factredo { return $usage if @$opt_args > 2; return $usage if not @$opt_args; - $arguments = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @$opt_args); + $arguments = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_ } @$opt_args); my ($channel, $trigger) = $self->find_factoid_with_optional_channel( $context->{from}, $context->{arguments}, 'factredo', explicit => 1, exact_channel => 1 @@ -831,7 +873,7 @@ sub cmd_factshow { my ($chan, $trig) = @$opt_args; $chan = $context->{from} if not defined $trig; - my $args = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @$opt_args); + my $args = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_ } @$opt_args); my ($channel, $trigger) = $self->find_factoid_with_optional_channel($context->{from}, $args, 'factshow', usage => $usage); return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message @@ -880,7 +922,7 @@ sub cmd_factlog { return "Too many arguments -- $usage" if @$opt_args > 2; return "Missing argument -- $usage" if not @$opt_args; - my $args = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @$opt_args); + my $args = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_ } @$opt_args); my ($channel, $trigger) = $self->find_factoid_with_optional_channel($context->{from}, $args, 'factlog', usage => $usage, exact_channel => 1); diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index d61494f1..cab515b7 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,7 +25,7 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4367, + BUILD_REVISION => 4368, BUILD_DATE => "2021-08-25", };