shithead-ng/MarkovBot.pl

97 lines
2.2 KiB
Perl
Raw Normal View History

2016-04-30 23:53:26 +02:00
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use FindBin qw($Bin);
use lib $Bin;
package MarkovBot;
use base qw/Bot::BasicBot/;
use MarkovBot::Config;
use MarkovBot::Learning;
use MarkovBot::Ignore;
use MarkovBot::Commands;
2016-07-04 22:03:09 +02:00
use MarkovBot::MarkovChain;
use MarkovBot::Redis;
use Encode qw(encode decode);
2016-07-19 15:14:11 +02:00
use if (config("rng") eq "mt"), "Math::Random::MT::Perl" => qw(rand);
2016-04-30 23:53:26 +02:00
sub said {
my $self = shift;
my $msg = shift;
2016-07-04 20:40:33 +02:00
my $command_char = quotemeta config "command_character";
2016-07-04 22:03:09 +02:00
my $redis = redis();
my $redis_prefix = config("redis_prefix");
2016-04-30 23:53:26 +02:00
# Ignore PMs and ignored users
return if $msg->{channel} eq 'msg';
return if isIgnored($msg->{who});
# Intercept commands
2016-07-04 19:41:12 +02:00
if ($msg->{body} =~ m/^$command_char.+/) {
2016-04-30 23:53:26 +02:00
my $command = encode("UTF-8", $msg->{body});
2016-04-30 23:53:26 +02:00
my @command = split(" ", $command);
my $bare = $command[0];
2016-07-04 19:41:12 +02:00
$bare =~ s/^$command_char//;
2016-04-30 23:53:26 +02:00
my %subs = %{getCommandSubs()};
if (defined $subs{$bare}) {
my $ret = $subs{$bare}->(\@command);
$self->say( channel => config("irc_channel"), body => decode("UTF-8", $ret) ) unless $ret eq "___null___";
2016-04-30 23:53:26 +02:00
}
2016-07-04 22:03:09 +02:00
return;
}
2016-07-17 23:29:02 +02:00
if ($msg->{body} eq 'wew') {
2016-07-18 18:53:44 +02:00
$self->say(channel => config("irc_channel"), body => "lad");
2016-07-17 23:29:02 +02:00
}
2016-07-04 22:03:09 +02:00
my $chattiness = $redis->get("$redis_prefix:chattiness");
2016-07-19 17:06:59 +02:00
my $rand = rand 100;
if ($rand < $chattiness) {
2016-07-04 22:03:09 +02:00
# generate a shitpost
my @line = split " ", $msg->{body};
return unless scalar(@line) > 1;
my $start = int rand $#line;
my $resp = markov( [$line[$start], $line[$start+1]] );
2016-07-19 16:44:11 +02:00
if (rand() * 100 < config("insult_chance") && !$resp) {
$resp = config("insult");
}
2016-07-04 22:03:09 +02:00
$self->say(
channel => config("irc_channel"),
body => $resp,
2016-07-19 16:44:11 +02:00
) if $resp;
2016-04-30 23:53:26 +02:00
}
# Learn
learn $msg->{body};
}
2016-07-04 22:03:09 +02:00
# Set base chattiness value on first run
my $redis = redis();
my $p = config("redis_prefix");
if (!$redis->get("$p:chattiness")) {
$redis->set("$p:chattiness", 10);
}
2016-04-30 23:53:26 +02:00
MarkovBot->new(
server => config("irc_server"),
port => config("irc_port"),
channels => [config("irc_channel")],
nick => config("irc_nickname"),
alt_nicks => [config("irc_nickname2")],
username => config("irc_nickname"),
name => config("irc_nickname"),
2016-07-04 20:40:33 +02:00
ssl => config("irc_ssl") eq "true" ? 1 : 0,
2016-04-30 23:53:26 +02:00
)->run();