Sleepaholic88 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i am making an AIM-Bot, and i am very new 2 perl, and i was wondering how i can make the bot recall a file (screenname.emo) and add/subtract a number from that file, when it recieves a specified message. I guess it would go something like this?:
if (! -e "emotions/$victim.emo") { open (DATA, ">emotions/$victim.emo"); print DATA "0"; close (DATA); } if ($msg =~ /^sorry/ || $msg =~ /^srry/ || $msg =~ /^sry/) {open (FILE, ">emotions/$victim.emo"); $emo = <FILE>; print DATA "$emo ++ 1"; close(FILE); return "it's OK."; }
With "sorry", "srry", or "sry" as the message recieved to the bot. Any ideas? Thanks!

Replies are listed 'Best First'.
Re: Need Help w/ AIMBot
by fokat (Deacon) on Mar 02, 2003 at 05:32 UTC

    If I understood your question correctly, then this piece of untested code should set you up (actually, you were close except for the print thing...) I assumed you only needed the file created when the sorry word appeared. If this is not the case, take the existence test out as you put it originally. I believe my version of the regexp is easier to pick up.

    if ($msg =~ /^so?r?ry/) {    if (! -e "emotions/$victim.emo") {   open (DATA, ">emotions/$victim.emo");     print DATA "1\n";     close (DATA);    } else { open (FILE, "+>emotions/$victim.emo");     chomp($emo = <FILE>); seek(FILE, 0, 0);     print FILE ++$emo, "\n";     close(FILE);   return "it's OK."; } }

    Also, note that you need to open the file for reading and writing instead of just writing. Otherwise you would clobber the prior contents.

    Finally, remember to use strict and use warnings.

    Best regards

    -lem, but some call me fokat

      I tryed this, but it just clears everything in screenname.emo and prints 1 (with the line after 1), no matter what the file had in the beginning. If anyone wants to talk to it, its screen name is TestAIMBot thanks.
        Woops, sorry, for got to log in!
Re: Need Help w/ AIMBot
by theorbtwo (Prior) on Mar 02, 2003 at 07:10 UTC

    One thing that both posters so far missed -- unless you're doing some pre-filtering you aren't showing, $msg will be in a psudoHTML, and there will be several HTML tags preceding the visable text, so you don't want to anchor that sorry to the beginning of the string with a ^, or you want to filter out the HTML. To filter out the HTML, somthing like s/<.*?>//g should work for simple cases... and, if I guess correctly, you'll never see a non-simple case in AIM-HTML.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      Yea, that's true, but the main file (bot.pl) covers that.
Re: Need Help w/ AIMBot
by dws (Chancellor) on Mar 02, 2003 at 05:46 UTC
    i was wondering how i can make the bot recall a file (screenname.emo) and add/subtract a number from that file ...

    Change print DATA "$emo ++ 1"; to print FILE ++$emo;