Posted: Tue Oct 10, 2006 7:00 pm Post subject: !timer
This command is to inform you when a set time is reached. you use the command like
Quote:
!timer 10 - Your note for this timer
When the timer is set off it will include the message which you set after the - Please let me know if you use this code or if you have any improvements as I am always looking to improve the quality of my coding
Code:
sub timer{
my ($bot,$self, $user, $name, $msg, $color) = @_;
if($msg eq "stop"){
$bot->{timer}->{users}->{$user}->{timer}=undef;
$bot->{timer}->{users}->{$user}->{message}=undef;
$self->sendMessage("This Timer has been stopped!");
}
if ($msg eq"view"){
if(!defined $bot->{timer}->{users}->{$user}){
$self->sendMessage("You Have no timers Running!");
}else{
my $rem = $bot->{timer}->{users}->{$user}->{timer}-time;
if($rem > 60){
my $mins = int($rem / 60);
$rem = $rem-($mins*60);
$remaining = "$mins Minutes and $rem Seconds";
}else{
$remaining = "$rem Seconds";
}
$self->sendMessage("This timer will go off in: $remaining mins.");
}
}
elsif ($msg ne undef)
{
my ($time,$message) = split(" - ",$msg,2);
$bot->{timer}->{users}->{$user}->{timer} = time+($time*60);
$bot->{timer}->{users}->{$user}->{message} = $message;
$self->sendMessage("This timer has been set for $time Minutes");
}
};
Add this somewhere in your bot.pl if you add it into a loop but you probaly do not want to add it into the loop that your bot uses to run as that runs more than once every second. In my use of the code I make the loop run every 10 secs
foreach my $handle (keys %{$bot->{msn}}) {
my $message = $bot->{timer}->{users}->{$who}->{message};
delete $bot->{timer}->{users}->{$who};
$bot->{msn}->{$handle}->call($who,"Timer Message: $message", Color => '0000FF');
return;
}
}
}
Edit: Sorry i didn't mean 10 minuets for the loop I meant 10 seconds. although how often you chose to run it is up to you. _________________ "Help us, Help you" - BotDepot
Last edited by JTW on Wed Oct 11, 2006 4:27 pm; edited 1 time in total
Nice. If it checks every 10 minutes, how can tell you your timer is up if you set it for 3 minutes, say? _________________ ~ Josh
[ Need bot hosting on a dedicated server? PM me. ]
I skimmed your code and noticed a few bugs/improvements.
You're testing $msg too strictly, imho. You're forcing the user to use lowercase with no spaces. Possibly lc the $msg and use a regex test to deal with garbage spaces.
You should clean up your if/elsif/else tests. You test for 'stop', then test for 'view' unnecessarily, for example.
In 'view', for times less than a minute, you're setting $remaining to "$rem Seconds" and then later printing it as "$remaining mins".
You could clean up your code dramatically by only doing one call to sendMessage at the end of the sub and setting a $message variable in your if tests.
I'd personally not check for $rem > 60 and instead just get the number of minutes and seconds even if there are less than 60 seconds remaining, then you could have cleaner $message setting code
Code:
my $mins = int($rem / 60);
$rem = $rem-($mins*60);
Those kind of one liners make the code much clearer to understand.
In the timer loop, you are looping through all users who have times, but returning after you find one, which means the others won't get their timer message until the next loop. Return in that code is used incorrectly.
One more thing I noticed. You assume the user will use 'stop', 'view' or a number. What if they use something else? So anyway, here's my version of your timer sub.
Code:
sub timer{
my ($bot,$self, $user, $name, $msg, $color) = @_;
my $message = 'Usage: !timer [view|stop|NN]';
if(lc $msg eq "stop"){
$bot->{timer}->{users}->{$user}->{timer}=undef;
$bot->{timer}->{users}->{$user}->{message}=undef;
$message = 'This timer has been stopped!';
}
elsif (lc $msg eq"view"){
if(!defined $bot->{timer}->{users}->{$user}){
$message = 'You have no timers running!';
}else{
my $rem = $bot->{timer}->{users}->{$user}->{timer}-time;
my $mins = int($rem / 60);
$rem = $rem-($mins*60);
$message = 'This timer will go off in: ';
$message .= ($mins == 0) ? "$rem seconds" : "$mins minutes and $rem seconds";
}
}
elsif ($msg =~ /(\d+)\s+-\s+(.+)/ )
{
my ($time,$message) = ($1, $2);
$bot->{timer}->{users}->{$user}->{timer} = time+($time*60);
$bot->{timer}->{users}->{$user}->{message} = $message;
$message = "This timer has been set for $time minutes";
}
Thanks for pointing out the problems i knew it wasn't the best code in the world. But now i know whats wrong I won't make the same mistakes. _________________ "Help us, Help you" - BotDepot