Skip to content

Instantly share code, notes, and snippets.

@jsocol
Created July 21, 2010 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsocol/483950 to your computer and use it in GitHub Desktop.
Save jsocol/483950 to your computer and use it in GitHub Desktop.
WTF.bm - A WTF module for MozBot
###########################
# WTF Module #
###########################
# WTF.bm - the WTF module for MozBot 2.6
# The WTF Module counts all the "wtf"s it hears in any channel the bot
# is in, and when you ask it "wtfs" will tell you how many it's heard.
# @author James Socol <me@jamessocol.com>
# @license MIT/X11. Go nuts.
package BotModules::WTF;
use vars qw(@ISA);
@ISA = qw(BotModules);
use DBI;
use strict;
1;
our $db = DBI->connect('dbi:SQLite:wtfs.db', '', '',
{RaiseError => 1, AutoCommit => 1});
sub Help {
my $self = shift;
my ($event) = @_;
return {
'' => 'A module to count WTFs.',
'wtfs' => 'How many times I\'ve heard "WTF" in this channel.',
'wtfs by %name' => 'How many times I\'ve heard %name say "WTF".',
'wtfs today' => 'How many times I\'ve heard "WTF" today.',
'wtfs here today' => 'How many times I\'ve heard "WTF" today in this channel.',
'wtfs per hour' => 'How many times I\'ve heard "WTF" per hour (on average) in this channel today.',
};
}
sub Initialise {
$db->do('CREATE TABLE IF NOT EXISTS wtfs (
user VARCHAR(60) NOT NULL,
created DATETIME NOT NULL,
message TEXT NOT NULL,
channel VARCHAR(60) NOT NULL)');
}
sub Heard {
my $self = shift;
my ($event, $message) = @_;
if ($message =~ /wtf/si) {
$self->HeardWTF($event);
}
return 0;
}
sub Told {
my $self = shift;
my ($event, $message) = @_;
if ($message =~ /wtfs\s+(here\s+)?today/si) {
my $count;
my $here;
if ("$1") {
$count = $self->GetWTFsTodayByChannel($event->{'channel'});
$here = ' here';
} else {
$count = $self->GetWTFsToday();
$here = '';
}
if ($count > 1) {
$self->say($event, "I've heard $count WTFs$here today.");
} elsif (1 == $count) {
$self->say($event, "I've heard $count WTF$here today.");
} else {
$self->say($event, "I haven't heard WTF$here today.");
}
} elsif ($message =~ /wtfs per hour/si) {
my $perhour = $self->GetWTFsPerHourToday($event->{'channel'});
$self->say($event, "Today, I've heard $perhour WTFs per hour in this channel.");
} elsif ($message =~ /wtfs\s+by\s+(\S+)/si) {
my $count = $self->GetWTFsByUser($1);
if ($count > 1) {
$self->say($event, "Looks like $1 has said WTF $count times.");
} elsif (1 == $count) {
$self->say($event, "Looks like $1 has said WTF once.");
} else {
$self->say($event, "I've never heard $1 say WTF.");
}
} elsif ($message =~ /^\s*wtfs\s*$/si) {
my $count = $self->GetWTFsByChannel($event->{'channel'});
if (1==$count) {
$self->say($event, "I've heard one WTF in this channel.");
} elsif ($count > 1) {
$self->say($event, "I've heard $count WTFs in this channel.");
} else {
$self->say($event, 'I\'ve never heard WTF in this channel.');
}
}
return 0;
}
sub HeardWTF {
my $self = shift;
my ($event) = @_;
my $who = substr $event->{'user'}, 0, index($event->{'user'}, '@');
my $what = $event->{'data'};
my $where = $event->{'channel'};
my $sth = $db->prepare("INSERT INTO wtfs (user, created, message, channel) VALUES (?, date('now'), ?, ?)");
$sth->execute($who, $what, $where);
if ($sth->errstr) {
$self->say($event, $sth->errstr);
} else {
$self->say('Looks like it worked!');
}
}
sub GetWTFsByChannel {
my $self = shift;
my ($channel) = @_;
my $sth = $db->prepare("SELECT COUNT(*) AS num FROM wtfs WHERE channel = ?");
my @data;
$sth->execute($channel);
@data = $sth->fetchrow_array();
return @data[0];
}
sub GetWTFsByUser {
my $self = shift;
my ($user) = @_;
my $sth = $db->prepare("SELECT COUNT(*) AS num FROM wtfs WHERE user = ?");
my @data;
$sth->execute($user);
@data = $sth->fetchrow_array();
return @data[0];
}
sub GetWTFsToday {
my $self = shift;
my $sth = $db->prepare("SELECT COUNT(*) AS num FROM wtfs WHERE created >= date('now', 'start of day')");
my @data;
$sth->execute();
@data = $sth->fetchrow_array();
return @data[0];
}
sub GetWTFsTodayByChannel {
my $self = shift;
my ($channel) = @_;
my $sth = $db->prepare("SELECT COUNT(*) AS num FROM wtfs WHERE created >= date('now', 'start of day') AND channel = ?");
my @data;
$sth->execute($channel);
@data = $sth->fetchrow_array();
return @data[0];
}
sub GetWTFsPerHourToday {
my $self = shift;
my ($channel) = @_;
my $count = $self->GetWTFsTodayByChannel($channel);
my $perhour = $count/(time % 86400.0) * 3600;
return sprintf('%.3f', $perhour);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment