User Control Panel
Advertisements

HELP US, HELP YOU!

!timer

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Commands
View unanswered posts
Author Message
JTW
God Like
God Like


Joined: 07 Mar 2004
Posts: 579
Location: Maidstone
Reputation: 67.1
votes: 4

PostPosted: Tue Oct 10, 2006 7:00 pm    Post subject: !timer Reply with quote

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
Code:

      foreach my $who (keys %{$bot->{timer}->{users}}){
         if(defined $bot->{timer}->{users}->{$who} && $bot->{timer}->{users}->{$who}->{timer} < time){
            
                      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
Back to top
darkmonkey
The Merovingian
The Merovingian


Joined: 18 Apr 2004
Posts: 2557
Location: London, England
Reputation: 39.3Reputation: 39.3Reputation: 39.3Reputation: 39.3
votes: 7

PostPosted: Wed Oct 11, 2006 4:06 pm    Post subject: Reply with quote

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. ]
Back to top
JTW
God Like
God Like


Joined: 07 Mar 2004
Posts: 579
Location: Maidstone
Reputation: 67.1
votes: 4

PostPosted: Wed Oct 11, 2006 4:25 pm    Post subject: Reply with quote

Opps I didn't mean 10 mins I meant 10 secs.
_________________
"Help us, Help you" - BotDepot
Back to top
darkmonkey
The Merovingian
The Merovingian


Joined: 18 Apr 2004
Posts: 2557
Location: London, England
Reputation: 39.3Reputation: 39.3Reputation: 39.3Reputation: 39.3
votes: 7

PostPosted: Wed Oct 11, 2006 10:09 pm    Post subject: Reply with quote

Ah, ok, cool. Good work Smile
_________________
~ Josh
[ Need bot hosting on a dedicated server? PM me. ]
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Thu Oct 12, 2006 2:44 am    Post subject: Reply with quote

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);

$message = ($mins == 0) ? "$rem Seconds" : "$mins Minutes and $rem Seconds";


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.
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Thu Oct 12, 2006 2:53 am    Post subject: Reply with quote

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";
    }

     $self->sendMessage( $message );
};
Back to top
JTW
God Like
God Like


Joined: 07 Mar 2004
Posts: 579
Location: Maidstone
Reputation: 67.1
votes: 4

PostPosted: Thu Oct 12, 2006 5:10 pm    Post subject: Reply with quote

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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Commands All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group