User Control Panel
Advertisements

HELP US, HELP YOU!

On-Off Command

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


Joined: 12 Nov 2003
Posts: 624

Reputation: 42.9Reputation: 42.9Reputation: 42.9Reputation: 42.9

PostPosted: Wed Jan 21, 2004 7:55 am    Post subject: Reply with quote

Hey...I was reading Eric's Tutorial on Hashes, and well I thought I'd try to write something that involved real hashes. But naturally, I fail.

The following is a command that "turns on" other commands. What it does, is it opens up a text file called onoff.txt, and runs down the list of commands. When I type !on sendim (for example), it will search through the file, and its supposed to change the numerical value from 0 to 1. However, the way it works now, if I'm lucky, is it just erases the entire onoff.txt file.

Code:
######################################<br />#                                    #<br />#                            ##      #<br />#           ###########     # #      #<br />#          #           #     #       #<br />#         #     ###     #   ###      #<br />#        #     #  #      #           #<br />#        #      ###      #           #<br />#        #     #  #      #           #<br />#         #    ##########            #<br />#          #                         #<br />#           ###########              #<br />#                                    #<br />#      @-Squared Productions         #<br />#                                    #<br />######################################<br /><br />sub on {<br /><br /> my ($victim,$msg,$aim) = (shift,shift,shift);<br /> my $sn = $screenname;<br /> my $toprint;<br /> my %onoff;<br /> my @new_comm;<br /> my $new;<br /><br /> $msg =~ s/(\!|\.|\/)on //ig;<br /><br /> my $adminonly = 1;<br /><br /> open (FILE, "commands/admin.txt");<br /> my @admin = <FILE>;<br /> close(FILE);<br /> chomp @admin;<br /><br /> my $is_admin = 0;<br /><br /> foreach my $admin (@admin) {<br />   if ($victim eq $admin) {<br />    $is_admin = 1;<br />    break;<br />   }<br /> } <br /><br /> open (COMM, "texts/onoff.txt");<br /> my @existing_comm = <COMM>;<br /> close (COMM);<br /> chomp(@existing_comm);<br /><br /> my $final; <br /><br /> foreach my $line (@existing_comm) {<br />   my ($comms,$val) = split(/\]\[/, $line);<br />   $onoff->{$comms} = "$val";<br />   $toprint .= "Current line...$comms ($onoff->{$comms})\n";<br /> }<br /><br /> if ($adminonly == 1 && $is_admin == 1) {<br />   my $com = $msg;<br />    if ($onoff->{$com} == 0) {<br />      $onoff->{$com} = "1";<br />      $toprint .= "I have turned on command $com\n";<br />      $reply = "I have turned on command $com."; <br />    } else {<br />      $reply = "$com is already on!";<br />    }<br /> } else {<br />   $reply = "Sorry, this is an admin only command";<br /> }<br /><br /> @new_comm = sort keys %onoff;<br /> chomp(@new_comm);<br /> open (NEW, ">texts/onoff.txt");<br /> foreach $new (@new_comm) {<br />   print NEW "$new\]\[$onoff->{$new}\n";<br /> }<br /> close (NEW);<br /><br /> &print($toprint);<br /> return $reply;<br /><br />}1;


Example of onoff.txt
Quote:
sendim][1
blackjack][1
tictactoe][1


I know this isn't a great way to use hashes...or probably not even a "real way". However, I wanted to use "hashes" so I could more easily change the values (0/1). Another way I wrote the command used simple "teach" manipulation:

Code:
######################################<br />#                                    #<br />#                            ##      #<br />#           ###########     # #      #<br />#          #           #     #       #<br />#         #     ###     #   ###      #<br />#        #     #  #      #           #<br />#        #      ###      #           #<br />#        #     #  #      #           #<br />#         #    ##########            #<br />#          #                         #<br />#           ###########              #<br />#                                    #<br />#      @-Squared Productions         #<br />#                                    #<br />######################################<br /><br />sub on {<br /><br /> my ($victim,$msg,$aim) = (shift,shift,shift);<br /> my $sn = $screenname;<br /> my $toprint;<br /> my $found = 0;<br /><br /> $msg =~ s/(\!|\.|\/)on //ig;<br /><br /> my $adminonly = 1;<br /><br /> open (FILE, "commands/admin.txt");<br /> my @admin = <FILE>;<br /> close(FILE);<br /> chomp @admin;<br /><br /> my $is_admin = 0;<br /><br /> foreach my $admin (@admin) {<br />   if ($victim eq $admin) {<br />     $is_admin = 1;<br />     break;<br />   }<br /> } <br /><br /> if ($adminonly == 1 && $is_admin == 1) {<br />   my $com = $msg;<br /><br />   open (COMM, "texts/onoff.txt");<br />   my @existing_comm = <COMM>;<br />   close (COMM);<br />   chomp(@existing_comm);<br /><br />   my $final; <br /><br />   foreach my $line (@existing_comm) {<br />     chomp($line);<br />     my ($comms,$val) = split(/\]\[/, $line);<br />     $toprint .= "Current line...$comms\n";<br />     if ($comms =~ /$com/i) {<br />       if ($val == 0) {<br />         $val = 1;<br />         $line = "$comms\]\[$val";<br />         $reply = "I have turned on command $comms.";<br />       } else {<br />         $reply = "$comms is already on!";<br />       }<br />     } else {<br />       $reply = "$com does not exist, or is not listed";<br />     }<br />     $final .= "$line\n";<br />   }<br /><br />   open (NEW, ">texts/onoff.txt");<br />   print NEW "$final";<br />   close (NEW);<br /><br /> } else {<br />   $reply = "Sorry, this is an admin only command";<br /> }<br /><br /> &print($toprint);<br /> return $reply;<br /><br />}<br />1;
...but I'm trying to move away from this method.
_________________
Anthony Arslan
@-Squared Enterprises
MacroHard Corporation
Back to top
Tony_shu
God Like
God Like


Joined: 12 Nov 2003
Posts: 624

Reputation: 42.9Reputation: 42.9Reputation: 42.9Reputation: 42.9

PostPosted: Wed Jan 21, 2004 5:17 pm    Post subject: Reply with quote

I still cannot get the hash version of onoff() function to work, however, I'm making do with the second version.

Also, here is my command.pl which checks if the user is trying to use a command. I had previously posted this with my modification, so it would check subs instead of files. However, since then I have modified it to use the onoff() function.
Code:
######################################<br />#                                    #<br />#                            ##      #<br />#           ###########     # #      #<br />#          #           #     #       #<br />#         #     ###     #   ###      #<br />#        #     #  #      #           #<br />#        #      ###      #           #<br />#        #     #  #      #           #<br />#         #    ##########            #<br />#          #                         #<br />#           ###########              #<br />#                                    #<br />#      @-Squared Productions         #<br />#                                    #<br />######################################<br /><br />sub commands {<br /><br />  #Retrieves the victim and the message.<br />  my ($victim,$msg,$aim) = (shift,shift,shift);<br />  my $reply;<br /><br />  #Resets the default values.<br />  my $command = 0;<br /><br /><br />  @mess = split(/ /,$msg);<br />  $mess[0] =~ s/(\/|\!|\.)//ig;<br />  if (exists &{$mess[0]}) {<br />   open(FILE, "texts/onoff.txt");<br />   my @activity = <FILE>;<br />   close(FILE);<br />   chomp(@activity);<br />   foreach my $active (@activity) {<br />    my ($what,$how) = split(/\]\[/,$active);<br />    if ($what eq $mess[0] && $how == 1) {<br />     $reply = &{$mess[0]}($victim,$msg,$aim);<br />     $command = 1;<br />    } elsif ($what eq $mess[0] && $how == 0) {<br />     $reply = "Sorry, but the command was turned off by the admin.";<br />     $command = 1;<br />    }<br />   }<br />  } else {<br />   $reply = "This was not a command.";<br />  }<br /><br />  return $command,$reply;<br />}<br />1;


EDITED: What's also a good thing about this additional feature, if you use my version of command.pl is that there used to be concern about people trying to call subs that weren't commands. For Example: When I first posted the new command.pl, Eric said that people could say
Quote:
!commands
...and the bot would try to open the sub command() and the bot would crash because of a loop. However, if you use the onoff addition, the commands won't be caleld unless you list them in the onoff.txt Very Happy
_________________
Anthony Arslan
@-Squared Enterprises
MacroHard Corporation
Back to top
BraveHeart
Senior Member
Senior Member


Joined: 14 Jan 2004
Posts: 285

Reputation: 33.8Reputation: 33.8Reputation: 33.8

PostPosted: Wed Jan 21, 2004 5:21 pm    Post subject: Reply with quote

Nice Command

B)
Back to top
Tony_shu
God Like
God Like


Joined: 12 Nov 2003
Posts: 624

Reputation: 42.9Reputation: 42.9Reputation: 42.9Reputation: 42.9

PostPosted: Wed Jan 21, 2004 5:22 pm    Post subject: Reply with quote

Thanks....but I could really use the help with hashes. I've bought a book on Perl, but it doesn't seem to help me lol :wacko:
_________________
Anthony Arslan
@-Squared Enterprises
MacroHard Corporation
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Wed Jan 21, 2004 5:50 pm    Post subject: Reply with quote

If you 'use Storable;' then you can save hash directly to a file, and then load it back later.

Code:
<br />use Storable;<br />my $onoff = retrieve('onoff.dat');<br /><br />$onoff->{$cmd} = 0;  #set the command off<br />$onoff->{$cmd} = 1;  #set the command on<br /><br />$onoff->{$cmd} = !$onoff->{$cmd};<br /><br />store $onoff, 'onoff.dat';<br />


That should be easily converted into your command. give it a try!

_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
BraveHeart
Senior Member
Senior Member


Joined: 14 Jan 2004
Posts: 285

Reputation: 33.8Reputation: 33.8Reputation: 33.8

PostPosted: Wed Jan 21, 2004 6:29 pm    Post subject: Reply with quote

i have multiple perl books to.

But they don't help me either
Back to top
Tony_shu
God Like
God Like


Joined: 12 Nov 2003
Posts: 624

Reputation: 42.9Reputation: 42.9Reputation: 42.9Reputation: 42.9

PostPosted: Wed Jan 21, 2004 9:43 pm    Post subject: Reply with quote

I'm having an issue with the second form of my on() function. (The form without the hashes.)

It keeps giving me back the reply
Quote:
$com does not exist, or is not listed


I know why it does it...its because after it changes the value for the required command in the onoff.txt file, it continues through the rest of the commands.
I just can't figure out a way around it though, so that it tells me that it properly turned off the command I asked.

_________________
Anthony Arslan
@-Squared Enterprises
MacroHard Corporation
Back to top
Tony_shu
God Like
God Like


Joined: 12 Nov 2003
Posts: 624

Reputation: 42.9Reputation: 42.9Reputation: 42.9Reputation: 42.9

PostPosted: Thu Jan 22, 2004 6:20 pm    Post subject: Reply with quote

Issue resolved (I believe)

Here's the final commands:
http://www.bot-depot.com/forums/index.php?act=ST&f=13&t=564

_________________
Anthony Arslan
@-Squared Enterprises
MacroHard Corporation
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl 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