Last active
August 29, 2015 13:58
-
-
Save CoderPuppy/10158696 to your computer and use it in GitHub Desktop.
Chat Bot API for my Chat Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Chat Bot API by CoderPuppy | |
| local protocol = 'cpup/chat' | |
| function createBot(hostname, username) | |
| local bot = { | |
| id = math.random(1, 2147483647); | |
| hostname = hostname; | |
| username = username; | |
| } | |
| if hostname == nil or username == nil then | |
| error("createBot expects string, string, got: " .. textutils.serialize({ hostname, username })) | |
| end | |
| -- Connect | |
| local host = rednet.lookup(protocol, hostname) | |
| if host == nil then | |
| error('Couldn\'t find chat host: ' .. hostname, 2) | |
| end | |
| function send(msgType, msg) | |
| -- print('sending: ' .. textutils.serialize({ | |
| -- msgType = msgType; | |
| -- msg = msg; | |
| -- })) | |
| rednet.send(host, { | |
| side = 'client'; | |
| msgType = msgType; | |
| msg = msg; | |
| userID = bot.userID; | |
| }, protocol) | |
| end | |
| -- Login | |
| bot.userID = math.random(1, 2147483647) | |
| send('login', bot.username) | |
| -- Setup ping pong | |
| local bPingPonged = true | |
| local pingPongTimer = os.startTimer(15) | |
| function ping() | |
| send('pingts') | |
| bPingPonged = false | |
| pingPongTimer = os.startTimer(15) | |
| end | |
| function bot.handle(ev) | |
| if ev[1] == 'timer' and ev[2] == pingPongTimer then | |
| if bPingPonged then | |
| ping() | |
| else | |
| os.queueEvent('bot_quit', bot.id, 'timeout') | |
| end | |
| elseif ev[1] == 'rednet_message' and ev[4] == protocol and ev[2] == host and type(ev[3]) == "table" and ev[3].userID == bot.userID then | |
| local msg = ev[3] | |
| -- print('got ' .. msg.msgType) | |
| -- print('got: '.. textutils.serialize({ | |
| -- msgType = msg.msgType; | |
| -- msg = msg.msg; | |
| -- })) | |
| -- userid, username, msg | |
| if msg.msgType == 'msg' then | |
| os.queueEvent('bot_user_message', bot.id, msg.msg[2], msg.msg[3], msg.msg[1]) | |
| -- userid, username, action | |
| elseif msg.msgType == 'action' then | |
| os.queueEvent('bot_user_action', bot.id, msg.msg[2], msg.msg[3], msg.msg[1]) | |
| -- userid, oldusername, newusername | |
| elseif msg.msgType == 'nick' then | |
| os.queueEvent('bot_user_nickchange', bot.id, msg.msg[2], msg.msg[3], msg.msg[1]) | |
| if msg.msg[1] == bot.userID then | |
| bot.username = msg.msg[3] | |
| end | |
| -- userid, username | |
| elseif msg.msgType == 'join' then | |
| os.queueEvent('bot_user_join', bot.id, msg.msg[2], msg.msg[1]) | |
| -- userid, username, reason | |
| elseif msg.msgType == 'leave' then | |
| os.queueEvent('bot_user_leave', bot.id, msg.msg[2], msg.msg[3], msg.msg[1]) | |
| if msg.msg[1] == bot.userID then | |
| os.queueEvent('bot_quit', bot.id, msg.msg[3]) | |
| end | |
| elseif msg.msgType == "pingtc" then | |
| send('pongts') | |
| elseif msg.msgType == "pongtc" then | |
| bPingPonged = true | |
| elseif msg.msgType == "kick" then | |
| os.queueEvent('bot_quit', bot.id, 'kick') | |
| else | |
| print(msg.msgType) | |
| end | |
| end | |
| end | |
| function bot.send(msg) | |
| send('chat', tostring(msg)) | |
| end | |
| function bot.logout(reason) | |
| send('logout', tostring(reason or 'Quit.')) | |
| end | |
| return bot | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment