Skip to content
Extraits de code Groupes Projets
Valider 7b9afb0c rédigé par Aorimn's avatar Aorimn
Parcourir les fichiers

Ajout de commandes au module Kaamelott

kaamelott count: compte le nombre de quotes
kaamelott get: récupère une quote particulière
parent 54343bf7
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -39,6 +39,8 @@ my $people = undef;
#
# Usage :
# !kaamelott [<personnage>] [<regex>]
# !kaamelott count [<personnage>] [<regex>]
# !kaamelott get [<personnage>] [<regex>] <id>
# ###
sub kaamelott_main
{
......@@ -50,6 +52,26 @@ sub kaamelott_main
}
if(defined($ref_params->[0]))
{
if($ref_params->[0] =~ /count/i)
{
shift @{$ref_params};
my $ret = kaamelott_count_quote_from_person_or_regex(
$reply_to,
$ref_params
);
return 0 unless $ret;
}
elsif($ref_params->[0] =~ /get/i)
{
shift @{$ref_params};
my $ret = kaamelott_get_quote_from_person_or_regex(
$reply_to,
$ref_params
);
return 0 unless $ret;
}
else
{
my $ret = kaamelott_quote_from_person_or_regex(
$reply_to,
......@@ -57,6 +79,7 @@ sub kaamelott_main
);
return 0 unless $ret;
}
}
else
{
kaamelott_display_quote(
......@@ -228,6 +251,147 @@ sub kaamelott_quote_from_person_or_regex
} # Fin kaamelott_quote_from_person_or_regex
# ###
# kaamelott_count_quote_from_person_or_regex
# Récupération et comptage de quotes
# ###
sub kaamelott_count_quote_from_person_or_regex
{
my ($reply_to, $ref_params) = @_;
my $count = 0;
if(!defined($ref_params->[0]))
{
$count = scalar(grep {defined $_} keys %{$quote_people});
$::displayer->sendto($reply_to, "There are currently $count quotes loaded");
return 1;
}
my $person = undef;
my $regex = qr/.*/;
my @quotes = keys %{$quote_people};
my $sanitized = kaamelott_sanitize_from_user($ref_params->[0]);
if(defined($people->{$sanitized}) and defined($person_quotes->{$people->{$sanitized}}))
{
shift @{$ref_params};
$person = $people->{$sanitized};
@quotes = keys %{$person_quotes->{$person}};
}
if(defined($ref_params->[0]))
{
my $tmp = join('\s+', @{$ref_params});
if(!eval{ $regex = qr/$tmp/i })
{
$::displayer->sendto($reply_to, "'$tmp' doesn't seem to be a correct regex");
return 1;
}
}
my @choices = ();
foreach my $quote (@quotes)
{
if($quote =~ $regex)
{
push @choices, $quote;
}
}
if(@choices)
{
$count = scalar(grep {defined $_} @choices);
my $quote = 'quote';
$quote .= 's' if($count > 1);
$::displayer->sendto($reply_to, "$count $quote found");
}
else
{
$::displayer->sendto($reply_to, 'No quote found');
}
return 1;
} # Fin kaamelott_count_quote_from_person_or_regex
# ###
# kaamelott_get_quote_from_person_or_regex
# Récupération et affichage de quotes
# ###
sub kaamelott_get_quote_from_person_or_regex
{
my ($reply_to, $ref_params) = @_;
return 0 unless defined($ref_params->[0]);
my $id = pop @{$ref_params};
return 0 unless $id =~ /^\d+$/;
my $person = undef;
my $regex = qr/.*/;
my @quotes = keys %{$quote_people};
if(!defined($ref_params->[0]))
{
if(defined($quotes[$id]))
{
kaamelott_display_quote($reply_to, $quotes[$id]);
}
else
{
$::displayer->sendto($reply_to, 'Quote not found');
}
return 1;
}
my $sanitized = kaamelott_sanitize_from_user($ref_params->[0]);
if(defined($people->{$sanitized}) and defined($person_quotes->{$people->{$sanitized}}))
{
shift @{$ref_params};
$person = $people->{$sanitized};
@quotes = keys %{$person_quotes->{$person}};
}
if(defined($ref_params->[0]))
{
my $tmp = join('\s+', @{$ref_params});
if(!eval{ $regex = qr/$tmp/i })
{
$::displayer->sendto($reply_to, "'$tmp' doesn't seem to be a correct regex");
return 1;
}
}
my @choices = ();
foreach my $quote (@quotes)
{
if($quote =~ $regex)
{
push @choices, $quote;
}
}
if(@choices)
{
if(defined($choices[$id]))
{
kaamelott_display_quote($reply_to, $choices[$id]);
}
else
{
$::displayer->sendto($reply_to, 'Quote not found');
}
}
else
{
$::displayer->sendto($reply_to, 'Quote not found');
}
return 1;
} # Fin kaamelott_get_quote_from_person_or_regex
# ###
# kaamelott_help
# Affiche l'aide de la commande '!kaamelott'
......@@ -239,7 +403,10 @@ sub kaamelott_help
$conn->privmsg($reply_to, "`".$Config::command_sign."kaamelott [<personnage>] [<regex>]` => "
."Affiche une citation de Kaamelott, dans les citation de <personnage> si fourni, "
."contenant <regex> si fourni (https://fr.wikiquote.org/wiki/Kaamelott)");
$conn->privmsg($reply_to, "`".$Config::command_sign."kaamelott count [<personnage>] [<regex>]` => "
."Compte le nombre de citations de <personnage> contenant <regex>");
$conn->privmsg($reply_to, "`".$Config::command_sign."kaamelott get [<personnage>] [<regex>] <id>` => "
."Affiche la citation numéro <id> de <personnage> contenant <regex>");
return 1;
} # Fin join_help
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter