1

Implement !friday and !пятница commands

This commit is contained in:
eleksir
2020-11-21 14:45:57 +03:00
parent 88da6ab653
commit 451999ea29
7 changed files with 191 additions and 1 deletions

85
lib/friday.pm Normal file
View File

@@ -0,0 +1,85 @@
package friday;
# for friday data stolen from https://raw.githubusercontent.com/isida/vi/master/data/friday.txt
use 5.018;
use strict;
use warnings;
use utf8;
use open qw(:std :utf8);
use English qw( -no_match_vars );
use Carp qw(cluck croak);
use vars qw/$VERSION/;
use Exporter qw(import);
our @EXPORT_OK = qw(seed friday);
$VERSION = '1.0';
use SQLite_File;
use MIME::Base64;
use File::Path qw(mkpath);
use conf qw(loadConf);
use botlib qw(logger);
my $c = loadConf();
my $dir = $c->{friday}->{dir};
my $srcfile = $c->{friday}->{src};
my @dow = qw (monday tuesday wednesday thursday friday saturday sunday);
sub seed () {
unless (-d $dir) {
mkpath ($dir) || croak "Unable to create $dir: $OS_ERROR";
}
my $friday;
for (0..$#dow) {
my $backingfile = sprintf '%s/%s.sqlite', $dir, $dow[$_];
if (-f $backingfile) {
unlink $backingfile || croak "Unable to remove $backingfile: $OS_ERROR\n";
}
tie @{$friday->[$_]}, 'SQLite_File', $backingfile || croak "Unable to tie to $backingfile: $OS_ERROR\n";
}
open (my $fh, '<', $srcfile) || croak "Unable to open $srcfile, $OS_ERROR\n";
while (readline $fh) {
next unless m/ \|\| /xmsg;
chomp();
my ($phrase, $days) = split(/ \|\| /);
my @daylist = split(//, $days);
foreach my $day (@daylist) {
next unless ($day =~ /^\d$/);
push @{$friday->[$day - 1]}, $phrase;
}
}
close $fh; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
for (0..$#dow) { untie $friday->[$_]; }
return;
}
# just return answer
sub friday () {
my $today = (localtime(time))[6] - 1;
my $backingfile = sprintf '%s/%s.sqlite', $dir, $dow[$today];
tie my @array, 'SQLite_File', $backingfile || do {
cluck "Unable to tie to $backingfile: $OS_ERROR\n";
return '';
};
my $phrase = $array[int (rand ($#array - 1))];
# decode?
utf8::decode $phrase;
untie @array;
return $phrase;
}
1;
# vim: ft=perl noet ai ts=4 sw=4 sts=4:

View File

@@ -16,6 +16,7 @@ use conf qw(loadConf);
use botlib qw(weather logger trim randomCommonPhrase);
use lat qw(latAnswer);
use karma qw(karmaSet karmaGet);
use friday qw(friday);
use Exporter qw(import);
use vars qw/$VERSION/;
@@ -112,11 +113,12 @@ sub __new_bot_message {
} elsif (substr($text, 1) eq 'help' || substr($text, 1) eq 'помощь') {
$bot->SendGroupMessage($hash{'reply_to'}, <<"EOL" );
!help | !помощь - это сообщение
!w город | !п город - погода в городе
!friday | !пятница - а не пятница ли сегодня?
!lat | !лат - сгенерировать фразу из крылатого латинского выражения
!ping | !пинг - попинговать бота
!some_brew - выдать соответсвующий напиток, бармен может налить rum, vodka, beer, tequila, whisky, absinthe
!ver | !version | !версия - написать что-то про версию ПО
!w город | !п город - погода в городе
!karma фраза | !карма фраза - посмотреть карму фразы
фраза++ | фраза-- - повысить или понизить карму фразы
EOL
@@ -132,6 +134,8 @@ EOL
}
$bot->SendGroupMessage($hash{'reply_to'}, karmaGet($hash{'reply_to'}, $mytext));
} elsif (substr($text, 1) eq 'friday' || substr($text, 1) eq 'пятница') {
$bot->SendGroupMessage($hash{'reply_to'}, friday());
} else {
$hailo->learn($text);
return;