Perl 与 Bot::BasicBot 比较(测试相等性)

发布于 2024-12-11 01:16:20 字数 1776 浏览 0 评论 0原文

我发现我似乎正在单独测试每个键,这就是为什么它一直说我不在操作员列表中。

技巧是将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯了 2024-12-18 01:16:20

在 Perl 中,有比尝试创建 C 式循环更好的方法。从代码来看, $config->{'OP'} 是一个数组,但我对您使用数组下标([]) 和同时按下。如果它是一个数组,那么只需使用 grep

if (grep { $_ eq $message->{who} } @{ $config->{OP} }) {
    $bot->mode("$message->{channel} +o $message->{who}");
} else {
    $bot->say(channel => $message->{channel},
              body => q{You aren't in the Operators list.},
              address => $message->{who});
}

此代码将完全替换您的 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 ([]) and keys on it at the same time. If it is an array then just use grep

if (grep { $_ eq $message->{who} } @{ $config->{OP} }) {
    $bot->mode("$message->{channel} +o $message->{who}");
} else {
    $bot->say(channel => $message->{channel},
              body => q{You aren't in the Operators list.},
              address => $message->{who});
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文