Rewrite to my current style
This commit is contained in:
parent
da04c46e20
commit
f667492208
85
HxBot.hx
85
HxBot.hx
@ -1,39 +1,27 @@
|
|||||||
// Haxe RawBot v1.1
|
|
||||||
// Catches connection problems
|
|
||||||
|
|
||||||
import sys.net.Host;
|
import sys.net.Host;
|
||||||
import sys.net.Socket;
|
import sys.net.Socket;
|
||||||
using StringTools;
|
using StringTools;
|
||||||
|
|
||||||
class HxBot {
|
class HxBot {
|
||||||
public static function main() {
|
// Initialize variables
|
||||||
// Initialize variables
|
static var server = '127.0.0.1';
|
||||||
var server = '127.0.0.1';
|
static var port = 6667;
|
||||||
var port = 6667;
|
static var autojoin = ['#lobby'];
|
||||||
var autojoin = ['#lobby'];
|
static var botnick = 'HxRawBot';
|
||||||
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() {
|
||||||
// Connect to server
|
// Connect to server
|
||||||
Sys.println('Connecting to ${server}:${Std.string(port)}...');
|
Sys.println('Connecting to ${server}:${Std.string(port)}...');
|
||||||
Sys.println("");
|
Sys.println("");
|
||||||
|
|
||||||
var irc = new Socket();
|
|
||||||
irc.connect(new Host(server), port);
|
irc.connect(new Host(server), port);
|
||||||
|
|
||||||
// Define functions
|
|
||||||
function say(sendtarget, sendmsg) {
|
|
||||||
irc.write('PRIVMSG ${sendtarget} :${sendmsg}\r\n');
|
|
||||||
}
|
|
||||||
function ctcp(sendtype, sendtarget, sendmsg) {
|
|
||||||
irc.write('${sendtype} ${sendtarget} :\x01${sendmsg}\x01\r\n');
|
|
||||||
}
|
|
||||||
function join(chan) {
|
|
||||||
irc.write('JOIN :${chan}\r\n');
|
|
||||||
}
|
|
||||||
function part(chan) {
|
|
||||||
irc.write('PART '+ chan +'\r\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
irc.write('NICK ${botnick}\n');
|
irc.write('NICK ${botnick}\n');
|
||||||
irc.write('USER ${botnick} ${botnick} ${botnick} :Haxe IRC bot\r\n');
|
irc.write('USER ${botnick} ${botnick} ${botnick} :Haxe IRC bot\r\n');
|
||||||
|
|
||||||
@ -46,26 +34,27 @@ class HxBot {
|
|||||||
try {
|
try {
|
||||||
var data = irc.input.readLine();
|
var data = irc.input.readLine();
|
||||||
if (data.charAt(0) == ":") {data = data.substr(1);}; // Strip leading colons
|
if (data.charAt(0) == ":") {data = data.substr(1);}; // Strip leading colons
|
||||||
|
|
||||||
//var timestamp = '[${DateTools.format(Date.now(), "%T")}]'; // For later
|
|
||||||
|
|
||||||
// Respond to server pings, but only print the first to avoid console spam
|
// Respond to server pings, but only print the first to avoid console spam
|
||||||
if (data.indexOf("PING") == 0) {
|
if (data.indexOf("PING") == 0) {
|
||||||
irc.write('PONG${data.substr(data.indexOf(" "))}\r\n');
|
irc.write('PONG${data.substr(data.indexOf(" "))}\r\n');
|
||||||
if (!Global.ping) {Sys.println('${data} (No further notifications)');Global.ping = true;}
|
if (!ping) {
|
||||||
|
Sys.println('${data} (No further notifications)');
|
||||||
|
ping = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print everything that's not a server ping
|
// Print everything that's not a server ping
|
||||||
if (data.indexOf("PING") != 0) {Sys.println(data);}
|
if (data.indexOf("PING") != 0) {Sys.println(data);}
|
||||||
|
|
||||||
// Begin processing data
|
// Begin processing data
|
||||||
if (StringTools.contains(data.substring(0, data.indexOf(" ")), "!")) {
|
if (StringTools.contains(data.substring(0, data.indexOf(" ")), "!")) { // Checking for "nickname!user@host"
|
||||||
var sender = data.substring(0, data.indexOf("!"));
|
var sender = data.substring(0, data.indexOf("!"));
|
||||||
var hostmask = data.substring(data.indexOf("!")+1, data.indexOf(" "));
|
var hostmask = data.substring(data.indexOf("!")+1, data.indexOf(" "));
|
||||||
// var action = data.substring(data.indexOf(" ")+1, data.indexOf(" ", data.indexOf(" ")+1)); // Works but not very extensible
|
|
||||||
var action = data.substring(data.indexOf(" ")+1, data.indexOf(" ", data.indexOf(hostmask)+hostmask.length+1));
|
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 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);
|
var msg = data.substring(data.indexOf(" ", data.indexOf(channel))+1);
|
||||||
|
|
||||||
if (msg.charAt(0) == ":") {msg = msg.substring(1);} // Strip leading colons
|
if (msg.charAt(0) == ":") {msg = msg.substring(1);} // Strip leading colons
|
||||||
|
|
||||||
// Auto-Rejoin when kicked
|
// Auto-Rejoin when kicked
|
||||||
@ -77,7 +66,7 @@ class HxBot {
|
|||||||
// HxBot chat responses begin here
|
// HxBot chat responses begin here
|
||||||
if (action == "PRIVMSG") {
|
if (action == "PRIVMSG") {
|
||||||
// Command processor
|
// Command processor
|
||||||
if (msg.charAt(0) == Global.prompt) {
|
if (msg.charAt(0) == commandPrefix) {
|
||||||
var command = "";
|
var command = "";
|
||||||
|
|
||||||
if (msg.indexOf(" ") != -1) {
|
if (msg.indexOf(" ") != -1) {
|
||||||
@ -92,11 +81,11 @@ class HxBot {
|
|||||||
|
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "quit":
|
case "quit":
|
||||||
if (Global.authorized.contains(sender)) {irc.write('QUIT :Received shutdown command from ${sender}\r\n');}
|
if (authorized.contains(sender)) {irc.write('QUIT :Received shutdown command from ${sender}\r\n');}
|
||||||
case "join":
|
case "join":
|
||||||
if ((Global.authorized.contains(sender)) && (msg.length > 0)) {join(msg);}
|
if ((authorized.contains(sender)) && (msg.length > 0)) {join(msg);}
|
||||||
case "part":
|
case "part":
|
||||||
if ((Global.authorized.contains(sender)) && (msg.length > 0)) {part(msg);}
|
if ((authorized.contains(sender)) && (msg.length > 0)) {part(msg);}
|
||||||
case "chk":
|
case "chk":
|
||||||
say(channel, '${sender}: ack');
|
say(channel, '${sender}: ack');
|
||||||
case "slap":
|
case "slap":
|
||||||
@ -104,19 +93,11 @@ class HxBot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Non-command responses
|
if (greetings.contains(msg.substring(0, msg.indexOf(" ")).toLowerCase())) {
|
||||||
/*switch (msg.substring(0, msg.indexOf(" ")).toLowerCase()) {
|
|
||||||
case "hi" | "hi," | "hello" | "hello," | "hey" | "hey," | "yo" | "yo," | "'lo" | "'lo,": // This could be a Global array
|
|
||||||
if (msg.substring(msg.indexOf(" ")+1, msg.indexOf(" ")+botnick.length+1).toLowerCase() == botnick.toLowerCase()) {
|
|
||||||
say(channel, 'Hello, ${sender}!');
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
if (Global.greetings.contains(msg.substring(0, msg.indexOf(" ")).toLowerCase())) {
|
|
||||||
if (msg.substring(msg.indexOf(" ")+1, msg.indexOf(" ")+botnick.length+1).toLowerCase() == botnick.toLowerCase()) {
|
if (msg.substring(msg.indexOf(" ")+1, msg.indexOf(" ")+botnick.length+1).toLowerCase() == botnick.toLowerCase()) {
|
||||||
say(channel, 'Hello, ${sender}!');
|
say(channel, 'Hello, ${sender}!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// if (msg.toString() == 'ACTION slaps ${botnick} around a bit with a large trout') {} // This doesn't work apparently
|
|
||||||
if (msg.contains('slaps ${botnick} around a bit with a large trout')) {
|
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.");
|
say(channel, "This is the Trout Protection Agency. Please put the Trout down and walk away with your hands in the air.");
|
||||||
}
|
}
|
||||||
@ -133,12 +114,18 @@ class HxBot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Not strictly necessary but I wanted to test feasibility
|
// Define functions
|
||||||
class Global {
|
public static function say(sendtarget, sendmsg) {
|
||||||
static public var authorized = ["Izwzyzx", "Ikewise"];
|
irc.write('PRIVMSG ${sendtarget} :${sendmsg}\r\n');
|
||||||
static public var prompt = "?";
|
}
|
||||||
static public var ping = false;
|
public static function ctcp(sendtype, sendtarget, sendmsg) {
|
||||||
static public var greetings = ["hi", "hi,", "hello", "hello,", "hey", "hey,", "yo", "yo,", "'lo", "'lo,"];
|
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');
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user