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();
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 ([]) andkeyson it at the same time. If it is an array then just usegrepThis code would completely replace your foreach loop. The
grepcommand 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.