HxBot/HxBot.hx

131 lines
4.3 KiB
Haxe
Raw Permalink Normal View History

2025-02-10 20:54:51 -06:00
import sys.net.Host;
import sys.net.Socket;
2025-02-10 22:30:16 -06:00
using StringTools;
2025-02-10 20:54:51 -06:00
class HxBot {
2025-02-13 20:24:44 -06:00
// Initialize variables
static var server = '127.0.0.1';
static var port = 6667;
static var autojoin = ['#lobby'];
static var botnick = 'HxRawBot';
static var irc = new Socket();
static var commandPrefix = "?";
static var authorized = ["Izwzyzx", "Ikewise"];
static var ping = false;
static var greetings = ["hi", "hello", "hey", "yo", "'lo"];
static function main() {
2025-02-10 22:45:08 -06:00
// Connect to server
Sys.println('Connecting to ${server}:${Std.string(port)}...');
2025-02-10 20:54:51 -06:00
Sys.println("");
2025-02-10 22:45:08 -06:00
irc.connect(new Host(server), port);
2025-02-10 20:54:51 -06:00
2025-02-10 22:45:08 -06:00
irc.write('NICK ${botnick}\n');
irc.write('USER ${botnick} ${botnick} ${botnick} :Haxe IRC bot\r\n');
2025-02-10 20:54:51 -06:00
2025-02-10 22:45:08 -06:00
for (i in autojoin) {
join(i);
}
2025-02-10 20:54:51 -06:00
// Main program loop
while (true) {
2025-02-10 22:45:08 -06:00
try {
var data = irc.input.readLine();
if (data.charAt(0) == ":") {data = data.substr(1);}; // Strip leading colons
2025-02-10 22:30:16 -06:00
2025-02-10 22:45:08 -06:00
// Respond to server pings, but only print the first to avoid console spam
if (data.indexOf("PING") == 0) {
irc.write('PONG${data.substr(data.indexOf(" "))}\r\n');
2025-02-13 20:24:44 -06:00
if (!ping) {
Sys.println('${data} (No further notifications)');
ping = true;
}
2025-02-10 22:45:08 -06:00
}
// Print everything that's not a server ping
if (data.indexOf("PING") != 0) {Sys.println(data);}
// Begin processing data
2025-02-13 20:24:44 -06:00
if (StringTools.contains(data.substring(0, data.indexOf(" ")), "!")) { // Checking for "nickname!user@host"
2025-02-10 22:45:08 -06:00
var sender = data.substring(0, data.indexOf("!"));
var hostmask = data.substring(data.indexOf("!")+1, data.indexOf(" "));
var action = data.substring(data.indexOf(" ")+1, data.indexOf(" ", data.indexOf(hostmask)+hostmask.length+1));
var channel = data.substring(data.indexOf(" ", data.indexOf(action))+1, data.indexOf(" ", data.indexOf(action)+action.length+1));
var msg = data.substring(data.indexOf(" ", data.indexOf(channel))+1);
2025-02-13 20:24:44 -06:00
2025-02-10 22:45:08 -06:00
if (msg.charAt(0) == ":") {msg = msg.substring(1);} // Strip leading colons
// Auto-Rejoin when kicked
if ((action == "KICK") && (msg.substring(0, msg.indexOf(" ")) == botnick)) {
irc.write('JOIN ${channel}\r\n');
}
// HxBot chat responses begin here
if (action == "PRIVMSG") {
// Command processor
2025-02-13 20:24:44 -06:00
if (msg.charAt(0) == commandPrefix) {
2025-02-10 22:45:08 -06:00
var command = "";
if (msg.indexOf(" ") != -1) {
// Separate command from rest of message, if necessary
command = msg.substring(1, msg.indexOf(" ")).toLowerCase();
msg = msg.substring(msg.indexOf(" ")+1);
} else {
// Otherwise, remove msg
command = msg.substring(1).toLowerCase();
msg = "";
}
switch (command) {
case "quit":
2025-02-13 20:24:44 -06:00
if (authorized.contains(sender)) {irc.write('QUIT :Received shutdown command from ${sender}\r\n');}
2025-02-10 22:45:08 -06:00
case "join":
2025-02-13 20:24:44 -06:00
if ((authorized.contains(sender)) && (msg.length > 0)) {join(msg);}
2025-02-10 22:45:08 -06:00
case "part":
2025-02-13 20:24:44 -06:00
if ((authorized.contains(sender)) && (msg.length > 0)) {part(msg);}
2025-02-10 22:45:08 -06:00
case "chk":
say(channel, '${sender}: ack');
case "slap":
if (msg.length != 0) {ctcp('PRIVMSG', channel, 'ACTION slaps ${msg} around a bit with a large trout');}
}
}
2025-02-13 20:24:44 -06:00
if (greetings.contains(msg.substring(0, msg.indexOf(" ")).toLowerCase())) {
2025-02-10 22:45:08 -06:00
if (msg.substring(msg.indexOf(" ")+1, msg.indexOf(" ")+botnick.length+1).toLowerCase() == botnick.toLowerCase()) {
say(channel, 'Hello, ${sender}!');
}
}
if (msg.contains('slaps ${botnick} around a bit with a large trout')) {
say(channel, "This is the Trout Protection Agency. Please put the Trout down and walk away with your hands in the air.");
}
if ((msg.charAt(0) == ".") && (msg.length == 1)) {say(channel, "omg a meteor");}
}
}
} catch (e:haxe.io.Eof) {
Sys.println("");
Sys.println('[Eof] Connection terminated.');
return;
} catch (e) {
trace('Uncaught: ${e}'); // throw e;
return;
2025-02-10 20:54:51 -06:00
}
}
}
2025-02-13 20:24:44 -06:00
// Define functions
public static function say(sendtarget, sendmsg) {
irc.write('PRIVMSG ${sendtarget} :${sendmsg}\r\n');
}
public static function ctcp(sendtype, sendtarget, sendmsg) {
irc.write('${sendtype} ${sendtarget} :\x01${sendmsg}\x01\r\n');
}
public static function join(chan) {
irc.write('JOIN :${chan}\r\n');
}
public static function part(chan) {
irc.write('PART '+ chan +'\r\n');
}
2025-02-10 20:54:51 -06:00
}