Perl 与 Bot::BasicBot 比较(测试相等性)
我发现我似乎正在单独测试每个键,这就是为什么它一直说我不在操作员列表中。
技巧是将 else 语句从 foreach 中移出,并将其更改为 if 语句。
然后在测试中对不等于
原始:
if($config->{'OP'}[$key] ne $message->{who})
新:
if($config->{'OP'}[$key-1] ne $message->{who})
最终完整代码进行了相当令人讨厌的黑客攻击:
#!/usr/bin/perl
use strict;
use warnings;
package kbot;
use base qw(Bot::BasicBot);
use YAML;
use Data::Dumper;
my $bot = kbot->new(
server => 'irc.saurik.com',
channels => ['#spam','#kbot'],
nick => 'kbot',);
sub reload{
system("perl kbot.pl");
}
sub said {
my ($self, $message) = @_;
my $config = YAML::LoadFile('kelbot.yml');
if($message->{body} =~ 'reload'){
reload();
}
if($message->{body} =~ 'opme'){
foreach $::key (keys $config->{OP}){
print $config->{OP}[$::key],"\n";
if($config->{OP}[$::key] eq $message->{who}){
$bot->mode($message->{channel}.' +o '.$message->{who});
} #end of if op
} #end of foreach
if($config->{OP}[$::key-1] ne $message->{who}){
$bot->say( channel => $message->{channel},
body => 'You aren\'t in the Operators list.',
address => $message->{who},
);
} #end of optest
} #end of opme
} #end of said
sub chanjoin {
my ($self, $message) = @_;
return 'kbot now online!';
}
$bot->run();
I've figured it out it appears that I was testing each key individually and thats why it kept saying i wasn't in the Operators list.
trick was to move the else statement out of foreach and to change it to a if statement.
then a rather nasty hack inside the test for not equal
original:
if($config->{'OP'}[$key] ne $message->{who})
new:
if($config->{'OP'}[$key-1] ne $message->{who})
final complete code:
#!/usr/bin/perl
use strict;
use warnings;
package kbot;
use base qw(Bot::BasicBot);
use YAML;
use Data::Dumper;
my $bot = kbot->new(
server => 'irc.saurik.com',
channels => ['#spam','#kbot'],
nick => 'kbot',);
sub reload{
system("perl kbot.pl");
}
sub said {
my ($self, $message) = @_;
my $config = YAML::LoadFile('kelbot.yml');
if($message->{body} =~ 'reload'){
reload();
}
if($message->{body} =~ 'opme'){
foreach $::key (keys $config->{OP}){
print $config->{OP}[$::key],"\n";
if($config->{OP}[$::key] eq $message->{who}){
$bot->mode($message->{channel}.' +o '.$message->{who});
} #end of if op
} #end of foreach
if($config->{OP}[$::key-1] ne $message->{who}){
$bot->say( channel => $message->{channel},
body => 'You aren\'t in the Operators list.',
address => $message->{who},
);
} #end of optest
} #end of opme
} #end of said
sub chanjoin {
my ($self, $message) = @_;
return 'kbot now online!';
}
$bot->run();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Perl 中,有比尝试创建 C 式循环更好的方法。从代码来看,
$config->{'OP'}
是一个数组,但我对您使用数组下标([]
) 和同时按下键
。如果它是一个数组,那么只需使用grep
此代码将完全替换您的 foreach 循环。
grep
命令是循环遍历整个列表并查找$message->{who}
是否出现在其中的命令。由于这可以在一条语句中轻松完成,因此关于在它出现或不出现的情况下做什么的 if 条件非常简单。There are much better ways of doing that in perl than trying to create a c-esque loop. From the code it looks like
$config->{'OP'}
is an array, but I'm a bit confused by your use of the array subscript ([]
) andkeys
on it at the same time. If it is an array then just usegrep
This code would completely replace your foreach loop. The
grep
command is the thing that loops over the entire list and finds whether$message->{who}
appears in it. Since that is done so easily in a single statement, the if condition about what to do in the case of it appearing or not is very straightforward.