Posted: Sat Jul 26, 2008 4:21 pm Post subject: IRC Bot
I made an PHP IRC Bot for a channel and if you have any requests for features just tell me. By the way, you will need a MySQL Database with a table `users` with the columns `user` and `level`. You can then insert yourself in it with the values `(your_nick)` and `2`.
if($split[0] == "PING ") {
fputs($sock, "PONG :{$split[1]}\n");
} else {
if($level != 0) {
if($cmd == "*help") {
fputs($sock, "PRIVMSG {$them} ~ Menu for {$nick} Bot v1.7 ~\n");
fputs($sock, "PRIVMSG {$them} ~ These commands work from any channel. This means you can private message him a command also. ~\n");
fputs($sock, "PRIVMSG {$them} ~ *help [shows this menu] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *info [shows bot info] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *flip [flips a coin] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *masters [shows the botmasters] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *cmds [show botmaster commands] ~\n");
fputs($sock, "PRIVMSG {$them} ~ End of Menu for {$nick} ~\n");
} elseif($cmd == "*info") {
fputs($sock, "PRIVMSG {$them} This is a bot made by Rezert for ZeroIdentity.org. He loves me very much, so he programmed me with lots of commands and more come very often.\n");
} elseif($cmd == "*flip") {
$coin = rand(1, 2);
if($coin == 1) {
fputs($sock, "PRIVMSG {$chan} Flipping coin...Heads!\n");
} else {
fputs($sock, "PRIVMSG {$chan} Flipping coin...Tails!\n");
}
} elseif($cmd == "*masters") {
$result = mysql_query("SELECT * FROM `users` WHERE `level`='2'", $conn);
$check = mysql_num_rows($result);
$i = 1;
unset($masters);
while($row = mysql_fetch_array($result)) {
if(!isset($masters)) {
$masters = $row['user'];
} else {
if($i == $check) {
$masters .= " and " . $row['user'];
} else {
$masters .= ", " . $row['user'];
}
}
$i++;
}
if($masters == "") {
fputs($sock, "PRIVMSG {$chan} I have no botmasters.\n");
}
if($masters != "") {
if($check == 1) {
fputs($sock, "PRIVMSG {$chan} {$masters} is my botmaster.\n");
} else {
fputs($sock, "PRIVMSG {$chan} {$masters} are my botmasters.\n");
}
}
} elseif($cmd == "*ignored") {
$result = mysql_query("SELECT * FROM `users` WHERE `level`='0'", $conn);
$check = mysql_num_rows($result);
$i = 1;
unset($ignored);
while($row = mysql_fetch_array($result)) {
if(!isset($ignored)) {
$ignored = $row['user'];
} else {
if($i == $check) {
$ignored .= " and " . $row['user'];
} else {
$ignored .= ", " . $row['user'];
}
}
$i++;
}
if($ignored == "") {
fputs($sock, "PRIVMSG {$chan} Nobody is on my ignore list.\n");
}
if($ignored != "") {
if($check == 1) {
fputs($sock, "PRIVMSG {$chan} {$ignored} is the only person on my ignore list.\n");
} else {
fputs($sock, "PRIVMSG {$chan} {$ignored} are on my ignore list.\n");
}
}
} elseif($cmd == "*cmds") {
if($level == 2) {
fputs($sock, "PRIVMSG {$them} ~ Botmaster Menu for {$nick} Bot v1.7 ~\n");
fputs($sock, "PRIVMSG {$them} ~ These commands work from any channel. This means you can private message him a command also. ~\n");
fputs($sock, "PRIVMSG {$them} ~ *say (nick/channel) (message) [says to nick/channel the message] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *me (nick/channel) (message) [says to nick/channel /me the message] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *nick (new nick) (pass..optional) [changes to new nick and if you specified a password it will identify itself] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *givev (nick) [gives nick voice] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *takev (nick) [takes voice from nick] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *giveo (nick) [gives nick ops] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *takeo (nick) [takes ops from nick] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *addbm (nick) [set nick to botmaster] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *reg (nick) [set nicks level to regular] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *ignore (nick) [sets nicks level to ignore] ~\n");
fputs($sock, "PRIVMSG {$them} ~ *quit [quits the bot] ~\n");
fputs($sock, "PRIVMSG {$them} ~ End of Botmaster Menu for {$nick} ~\n");
}
} elseif($cmd == "*givev") {
if($level == 2) {
if($arg == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
} else {
fputs($sock, "MODE {$chan} +v {$arg}\n");
}
}
} elseif($cmd == "*takev") {
if($level == 2) {
if($arg == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
} else {
fputs($sock, "MODE {$chan} -v {$arg}\n");
}
}
} elseif($cmd == "*giveo") {
if($level == 2) {
if($arg == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
} else {
fputs($sock, "MODE {$chan} +o {$arg}\n");
}
}
} elseif($cmd == "*takeo") {
if($level == 2) {
if($arg == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
} else {
fputs($sock, "MODE {$chan} -o {$arg}\n");
}
}
} elseif($cmd == "*addbm") {
if($level == 2) {
if($arg == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
} else {
$check = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `user`='{$arg}'", $conn));
if($check != 1) {
mysql_query("INSERT INTO `users` (user, level) VALUES('{$arg}', '2')", $conn);
} else {
mysql_query("UPDATE `users` SET `level`='2' WHERE `user`='{$arg}'", $conn);
}
fputs($sock, "PRIVMSG {$chan} {$arg} is now one of {$nick}'s botmasters.\n");
}
}
} elseif($cmd == "*reg") {
if($level == 2) {
if($arg == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
} else {
mysql_query("DELETE FROM `users` WHERE `user`='{$arg}'", $conn);
fputs($sock, "PRIVMSG {$chan} {$arg} is now a regular user.\n");
}
}
} elseif($cmd == "*ignore") {
if($level == 2) {
if($arg == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
} else {
$check = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `user`='{$arg}'"));
if($check != 1) {
mysql_query("INSERT INTO `users` (user, level) VALUES('{$arg}', '0')", $conn);
} else {
mysql_query("UPDATE `users` SET `level`='0' WHERE`user`='{$arg}'", $conn);
}
fputs($sock, "PRIVMSG {$chan} {$arg} is now ignored by {$nick}\n");
}
}
} elseif($cmd == "*say") {
if($level == 2) {
if($arg == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing channel/nick argument.\n");
} else {
if($arg2 == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing message argument.\n");
} else {
fputs($sock, "PRIVMSG {$arg} {$args}\n");
}
}
}
} elseif($cmd == "*me") {
if($level == 2) {
if($arg == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing channel/nick argument.\n");
} else {
if($arg2 == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing message argument.\n");
} else {
fputs($sock, "PRIVMSG {$arg} ACTION {$args}\n");
}
}
}
} elseif($cmd == "*nick") {
if($level == 2) {
if($arg == NULL) {
fputs($sock, "PRIVMSG {$chan} Missing new nick argument.\n");
} else {
fputs($sock, "NICK {$arg}\n");
print "<script>document.getElementById('nick').innerHTML='{$arg}';</script>";
if($arg2 != "") {
fputs($sock, "PRIVMSG NickServ IDENTIFY {$arg2}\n");
} $nick = $arg;
}
}
} elseif($cmd == "*quit") {
if($level == 2) {
fputs($sock, "PRIVMSG {$chan} Later.\n");
}
fputs($sock, "QUIT");
fclose($handle);
die("
</body>
</html>");
}
}
}
}
}
print "
</body>
</html>";
}
Joined: 25 Jun 2004 Posts: 130 Location: Liverpool(home) or Derby(uni), UK votes: 3
Posted: Mon Jul 28, 2008 12:37 pm Post subject:
How are you executing this script to get it in a browser window without your web server killing it for taking too long to execut?
How come you open a text file for the settings but you have users in a database? Wouldnt it better to have it one way or the other?
Also im not sure everyone is going to like having "Rezert's bot" as their bots "real name" in IRC.
Looks like youve put a lot of work into it though and done some research on the IRC protocol so well done, a refreshing change from most of the other new people who come here and just ask people to make their bot for them. How long did it take you? Looks like you have a decent grasp of PHP too, what other languages do you know?