App-RoboBot

 view release on metacpan or  search on metacpan

lib/App/RoboBot/Plugin/Fun/Factoid.pm  view on Meta::CPAN

package App::RoboBot::Plugin::Fun::Factoid;
$App::RoboBot::Plugin::Fun::Factoid::VERSION = '4.004';
use v5.20;

use namespace::autoclean;

use Moose;
use MooseX::SetOnce;

extends 'App::RoboBot::Plugin';

=head1 fun.factoid

Exports functions for managing small snippets of keyword-based knowledge.

In addition to the exported functions, this module inserts a pre-hook which
inspects all messages for keywords which match the stored factoids.  Messages
in the general format of a question which contain matching keywords trigger an
automatic response from the bot with the stored factoid.

=cut

has '+name' => (
    default => 'Fun::Factoid',
);

has '+description' => (
    default => 'Allows for saving and retrieving small snippets of keyword-based knowledge.',
);

has '+before_hook' => (
    default => 'check_factoids',
);

=head2 add-factoid

=head3 Description

Creates a new factoid of ``name`` on the current network with the given
description. Descriptions are limited only by the restrictions of the current
network.

=head3 Usage

<factoid name> "<description>"

=head3 Examples

    (add-factoid perl "A language which looks the same before and after encryption.")

=head2 update-factoid

=head3 Description

Updates the description of the named factoid.

=head3 Usage

<factoid name> "<new description>"

=head3 Examples

    (update-factoid perl "A fine and upstanding member of the interpreted languages ecosystem.")

=head2 remove-factoid

=head3 Description

Removes the named factoid.

=head3 Usage

<factoid name>

=head3 Examples

    (remove-factoid perl)

=cut

has '+commands' => (
    default => sub {{
        'add-factoid' => { method      => 'add_factoid',
                           description => 'Adds snippets of information (replacing any that already exist for the given name).',
                           usage       => '"<name>" <... facts ...>' },

        'update-factoid' => { method      => 'add_factoid',
                              description => 'Updates an existing factoid, or creates a new one if there is no existing factoid by the same name.',
                              usage       => '"<name>" <... facts ...>' },

        'remove-factoid' => { method      => 'remove_factoid',
                              description => 'Removes the factoids stored under the given name.',
                              usage       => '"<name>"' },
    }},
);

sub check_factoids {
    my ($self, $message) = @_;

    return if $message->has_expression;

    my $bot_name = $message->network->nick->name;
    return unless defined $bot_name && $bot_name =~ m{\w+};

    return unless $message->raw =~ m{^\s*${bot_name}[:,]?\s+(?:what|who|where|when|why|how|is|are)\s+(.+)\?\s*$}i;

    my $query = lc($1);

    my $res = $self->bot->config->db->do(q{



( run in 0.977 second using v1.01-cache-2.11-cpan-39bf76dae61 )