First run with admin module: fix typos in ChatMember.pm, Brain.pm, admin.pm, botlib.pm

This commit is contained in:
eleksir
2021-02-23 14:10:54 +03:00
parent e38726a1f2
commit 04cb3d2357
5 changed files with 50 additions and 32 deletions

View File

@@ -276,20 +276,15 @@ sub deleteMessage {
my $token = $self->token || croak 'No token supplied to deleteMessage()?'; my $token = $self->token || croak 'No token supplied to deleteMessage()?';
if (Teapot::Bot::Object::ChatMember::canDelete($self, $chatid)) { if (Teapot::Bot::Object::ChatMember::canDeleteMessage($self, $args->{chat_id})) {
my $url = "https://api.telegram.org/bot${token}/deleteMessage"; my $url = "https://api.telegram.org/bot${token}/deleteMessage";
my $api_response = $self->_post_request($url, $send_args); $self->_post_request($url, $send_args);
}
else {
return {'error' => 1};
}
if ($api_response) { return;
return Teapot::Bot::Object::Message->create_from_hash($api_response, $self);
}
else {
return {'error' => 1};
}
}
else {
return {'error' => 1};
}
} }

View File

@@ -48,7 +48,7 @@ sub canDeleteMessage {
if ($chatid < 0) { if ($chatid < 0) {
# group chat # group chat
my $chatobj = $self->_brain->getChat ({ 'chat_id' => $chatid }); my $chatobj = $self->getChat ({ 'chat_id' => $chatid });
# on api error, keep silence # on api error, keep silence
unless ($chatobj) { unless ($chatobj) {
@@ -56,7 +56,7 @@ sub canDeleteMessage {
return 0; return 0;
} }
my $myObj = $self->_brain->getMe (); my $myObj = $self->getMe ();
# on api error, keep silence # on api error, keep silence
unless ($chatobj) { unless ($chatobj) {
@@ -65,7 +65,7 @@ sub canDeleteMessage {
} }
my $myid = $myObj->id; my $myid = $myObj->id;
my $me = $self->_brain->getChatMember ({ 'chat_id' => $chatid, 'user_id' => $myid }); my $me = $self->getChatMember ({ 'chat_id' => $chatid, 'user_id' => $myid });
# on api error, keep silence # on api error, keep silence
unless ($me) { unless ($me) {
@@ -78,8 +78,8 @@ sub canDeleteMessage {
if ($me->{'status'} eq 'administrator') { if ($me->{'status'} eq 'administrator') {
$can_delete = 1; $can_delete = 1;
} }
# in supergroup bot must be granted with # in supergroup bot must be granted with can_delete_messages
} elsif ($chatobj->{'type'} eq 'group') || ($chatobj->{'type'} eq 'channel') } elsif (($chatobj->{'type'} eq 'supergroup') || ($chatobj->{'type'} eq 'channel')) {
if ($me->{'can_delete_messages'}) { if ($me->{'can_delete_messages'}) {
$can_delete = 1; $can_delete = 1;
} }

View File

@@ -14,12 +14,13 @@ use util qw (utf2sha1);
use version; our $VERSION = qw (1.0); use version; our $VERSION = qw (1.0);
use Exporter qw (import); use Exporter qw (import);
# to export array we need @ISA here
our @ISA = qw / Exporter /; ## no critic (ClassHierarchies::ProhibitExplicitISA) our @ISA = qw / Exporter /; ## no critic (ClassHierarchies::ProhibitExplicitISA)
our @EXPORT_OK = qw (@forbiddenMessageTypes getForbiddenTypes addForbiddenType delForbiddenType listForbidden fortune_toggle fortune_toggle_list fortune_status); our @EXPORT_OK = qw (@forbiddenMessageTypes getForbiddenTypes addForbiddenType delForbiddenType listForbidden fortune_toggle fortune_toggle_list fortune_status);
my $c = loadConf (); my $c = loadConf ();
my $dir = $c->{admin}->{dir}; my $dir = $c->{admin}->{dir};
my @forbiddenMessageTypes = qw (audio voice photo video video_note animation sticker dice game poll document); our @forbiddenMessageTypes = qw (audio voice photo video video_note animation sticker dice game poll document); ## no critic (Variables::ProhibitPackageVars)
sub initCensorDB { sub initCensorDB {
my $backingfile = shift; my $backingfile = shift;
@@ -27,7 +28,7 @@ sub initCensorDB {
tie my %censor, 'SQLite_File', $backingfile || cluck "Unable to tie to $backingfile: $OS_ERROR"; tie my %censor, 'SQLite_File', $backingfile || cluck "Unable to tie to $backingfile: $OS_ERROR";
foreach (@forbiddenMessageTypes) { foreach (@forbiddenMessageTypes) {
$censor($_) = 0; $censor{$_} = 0;
} }
untie %censor; untie %censor;
@@ -38,16 +39,24 @@ sub initCensorDB {
sub getForbiddenTypes { sub getForbiddenTypes {
my $chatid = shift; my $chatid = shift;
unless (-d $dir) {
unless (make_path $dir) {
cluck "Unable to create $dir: $OS_ERROR";
return;
}
}
my $messageTypes; my $messageTypes;
my $backingfile = utf2sha1 $chatid; my $backingfile = utf2sha1 $chatid;
$backingfile =~ s/\//-/xmsg; $backingfile =~ s/\//-/xmsg;
$backingfile = sprintf '%s/censor_%s.db', $dir, $backingfile; $backingfile = sprintf '%s/censor_%s.db', $dir, $backingfile;
my %censor;
unless (-f $backingfile) { unless (-f $backingfile) {
initCensorDB ($backingfile); initCensorDB ($backingfile);
} }
unless (tie my %censor, 'SQLite_File', $backingfile) { unless (tie %censor, 'SQLite_File', $backingfile) {
cluck "Unable to tie to $backingfile: $OS_ERROR"; cluck "Unable to tie to $backingfile: $OS_ERROR";
return $messageTypes; return $messageTypes;
} }
@@ -83,7 +92,9 @@ sub addForbiddenType {
initCensorDB ($backingfile); initCensorDB ($backingfile);
} }
unless (tie my %censor, 'SQLite_File', $backingfile) { my %censor;
unless (tie %censor, 'SQLite_File', $backingfile) {
cluck "Unable to tie to $backingfile: $OS_ERROR"; cluck "Unable to tie to $backingfile: $OS_ERROR";
return; return;
} }
@@ -114,7 +125,9 @@ sub delForbiddenType {
return; return;
} }
unless (tie my %censor, 'SQLite_File', $backingfile) { my %censor;
unless (tie %censor, 'SQLite_File', $backingfile) {
cluck "Unable to tie to $backingfile: $OS_ERROR"; cluck "Unable to tie to $backingfile: $OS_ERROR";
return; return;
} }
@@ -128,6 +141,13 @@ sub delForbiddenType {
sub listForbidden { sub listForbidden {
my $chatid = shift; my $chatid = shift;
unless (-d $dir) {
unless (make_path $dir) {
cluck "Unable to create $dir: $OS_ERROR";
return;
}
}
my @list; my @list;
my $backingfile = utf2sha1 $chatid; my $backingfile = utf2sha1 $chatid;
$backingfile =~ s/\//-/xmsg; $backingfile =~ s/\//-/xmsg;
@@ -137,8 +157,10 @@ sub listForbidden {
initCensorDB ($backingfile); initCensorDB ($backingfile);
} }
my %censor;
# FIXME: return list with all types alowed # FIXME: return list with all types alowed
unless (tie my %censor, 'SQLite_File', $backingfile) { unless (tie %censor, 'SQLite_File', $backingfile) {
cluck "Unable to tie to $backingfile: $OS_ERROR"; cluck "Unable to tie to $backingfile: $OS_ERROR";
return ''; return '';
} }

View File

@@ -53,7 +53,7 @@ sub command {
my $text = shift; my $text = shift;
my $chatid = shift; my $chatid = shift;
my $reply; my $reply;
my ($userid, $username, $fullname, $highlight, $visavi) = highlight $msg; my ($userid, $username, $fullname, $highlight, $visavi) = highlight ($msg);
if (substr ($text, 1) eq 'ping') { if (substr ($text, 1) eq 'ping') {
$reply = 'Pong.'; $reply = 'Pong.';
@@ -130,10 +130,10 @@ MYHELP
$msg->replyMd ($reply); $msg->replyMd ($reply);
return; return;
} elsif (substr ($text, 1) eq 'admin' || substr ($text, 1) eq 'админ') { } elsif (substr ($text, 1) eq 'admin' || substr ($text, 1) eq 'админ') {
my $member = $self->getChatMember ($msg->from->id); my $member = $self->getChatMember ({ 'chat_id' => $msg->chat->id, 'user_id' => $msg->from->id });
# this msg should be shown only if admins of chat request it # this msg should be shown only if admins of chat request it
if ($member->status eq 'administrator') || ($member->status eq 'creator') { if (($member->status eq 'administrator') || ($member->status eq 'creator')) {
$reply = << "MYADMIN"; $reply = << "MYADMIN";
``` ```
${csign}admin censor type # - где 1 - вкл, 0 - выкл цензуры для означенного типа сообщений ${csign}admin censor type # - где 1 - вкл, 0 - выкл цензуры для означенного типа сообщений
@@ -146,25 +146,26 @@ ${csign}admin fortune - показываем ли с утра фортун
${csign}admin фортунка - показываем ли с утра фортунку для чата ${csign}admin фортунка - показываем ли с утра фортунку для чата
``` ```
Типы сообщений: Типы сообщений:
audio voice photo video video_note animation sticker dice game poll document audio voice photo video animation sticker dice game poll document
MYADMIN MYADMIN
$msg->replyMd ($reply); $msg->replyMd ($reply);
} }
return; return;
} elsif ((substr ($text, 1, 5) eq 'admin' || substr ($text, 1, 5) eq 'админ') and (length ($text >= 8))) { } elsif ((substr ($text, 1, 5) eq 'admin' || substr ($text, 1, 5) eq 'админ') && (length ($text) >= 8)) {
my $member = $self->getChatMember ($msg->from->id); my $member = $self->getChatMember ({ 'chat_id' => $msg->chat->id, 'user_id' => $msg->from->id });
# this msg should be shown only if admins of chat request it # this msg should be shown only if admins of chat request it
if ($member->status eq 'administrator') || ($member->status eq 'creator') { if (($member->status eq 'administrator') || ($member->status eq 'creator')) {
my $command = trim (substr $text, 7); my $command = trim (substr $text, 7);
my ($cmd, $args) = split /\s+/, $command, 2; my ($cmd, $args) = split /\s+/, $command, 2;
if ($cmd eq '') { if ($cmd eq '') {
return; return;
} elsif ($cmd eq 'censor' || $cmd eq 'ценз') { } elsif ($cmd eq 'censor' || $cmd eq 'ценз') {
if (defined $args && args ne '') { if (defined ($args) && ($args ne '')) {
my ($msgType, $toggle) = split /\s/, $args; my ($msgType, $toggle) = split /\s/, $args, 2;
if (defined $toggle) { if (defined $toggle) {
foreach (@forbiddenMessageTypes) { foreach (@forbiddenMessageTypes) {
@@ -260,9 +261,9 @@ sub botsleep {
sub isCensored { sub isCensored {
my $msg = shift; my $msg = shift;
$forbidden = getForbiddenTypes ($msg->{chat}->{id}); my $forbidden = getForbiddenTypes ($msg->chat->id);
foreach (keys %{%forbidden}) { foreach (keys %{$forbidden}) {
if ($forbidden->{$_} && $msg->can ($_) && (defined $msg->{$_})) { if ($forbidden->{$_} && $msg->can ($_) && (defined $msg->{$_})) {
return 1; return 1;
} }

View File

@@ -221,7 +221,7 @@ sub __on_msg {
my $reply; my $reply;
if (isCensored $msg) { if (isCensored $msg) {
carp '[INFO] In public chat %s (%s) message from %s was censored', $chatname, $chatid, $vis_a_vi; carp sprintf '[INFO] In public chat %s (%s) message from %s was censored', $chatname, $chatid, $vis_a_vi;
$self->deleteMessage ({chat_id => $chatid, message_id => $msg->{message_id}}); $self->deleteMessage ({chat_id => $chatid, message_id => $msg->{message_id}});
} }