Advertisement
Guest User

Untitled

a guest
Feb 14th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.40 KB | None | 0 0
  1. local lfs = require("lfs")
  2. local json = require("JSON")
  3. local cqueues = require("cqueues")
  4. local csleep = cqueues.sleep
  5. local websocket = require("http.websocket")
  6.  
  7. _G.irc = require("irc")
  8. _G.ulib = assert(loadfile("ulib.lua"))() --useful stuff
  9.  
  10. _G.current_dir = lfs.currentdir() --have this here for later
  11. _G.loaded_modules = {}
  12. _G.commands = {}
  13. _G.callbacks = {}
  14.  
  15. _G.history = {}
  16.  
  17. local loop = cqueues.new()
  18.  
  19. if(ulib.file_exists(current_dir.."/_sconfig.lua")) then
  20.     local _sconfig = require("_sconfig")
  21.     print("Secret config loaded.")
  22.     _G.s_pass = _sconfig.pass
  23.     _G.s_ns_pass = _sconfig.nickserv_pass
  24.     _G.google_apikey = _sconfig.google_api_key
  25.     _G.discord_token = _sconfig.discord_token
  26. end
  27.  
  28. local function do_callback(name, ...)
  29.     if(type(callbacks[name]) ~= "table") then
  30.         return nil
  31.     end
  32.     for k,v in pairs(callbacks[name]) do
  33.         if(type(v) == "function") then
  34.             local ok, err = pcall(v, ...)
  35.             if not ok then
  36.                 print("[DEBUG]: Callback "..k.." errored with error \""..err.."\".")
  37.             end
  38.         else
  39.             print(k.." wasn't func, it was "..type(v))
  40.         end
  41.     end
  42. end
  43.  
  44. function _G.do_ignore(name)
  45.     for k,v in pairs(config.ignore_list) do
  46.         if string.lower(name) == string.lower(v) then
  47.             return true
  48.         end
  49.     end
  50.     local ignore_list = json:decode(ulib.readAll(_G.current_dir.."/ignore_list.json") or "{}") or {}
  51.     if(ulib.in_table(ignore_list, string.lower(name))) then
  52.         return true
  53.     end
  54.     return false
  55. end
  56.  
  57. local function module_unloaded(name)
  58.     local ul = ulib.readAll(current_dir.."/disable_modules.json") or "{}"
  59.     for k,v in pairs((json:decode(ul) or "{}")) do
  60.         if string.lower(name) == string.lower(v) then
  61.             return true
  62.         end
  63.     end
  64.     return false
  65. end
  66.  
  67.  
  68. function _G.join_channel(channel)
  69.     irc.join(channel)
  70.     _G.history[channel] = {}
  71.     print("Joining channel "..channel.."!")
  72. end
  73.  
  74. function _G.is_auth(name, chan)
  75.     return (ulib.in_table(_G.config.authorized_users, string.lower(tostring(name))) or ulib.in_table(chan:allops(), string.lower(tostring(name))))
  76. end
  77.  
  78. irc.register_callback("connect", function()
  79.     if(s_ns_pass) then
  80.         print("Identifying with NickServ!")
  81.         irc.say("NickServ", "IDENTIFY "..s_ns_pass)
  82.     end
  83.     for k,v in pairs(config.channels) do
  84.         join_channel(v)
  85.     end
  86.     do_callback("connect", {irc = irc, conf = config})
  87. end)
  88.  
  89. irc.register_callback("channel_msg", function(channel, from, message)
  90.     print("[IRC] ["..channel.."] "..from..": "..message)
  91.     if(type(_G.history[channel]) ~= "table") then
  92.         _G.history[channel] = {}
  93.     end
  94.     table.insert(_G.history[channel], {
  95.         user = from,
  96.         data = message
  97.     })
  98.     if(do_ignore(from) == false) then
  99.         if(string.sub(message,1,1) == config.command_prefix) then
  100.             local message_split = ulib.split(message, " ")
  101.             if(type(message_split[1]) == "string") then
  102.                 local command = string.gsub(message_split[1], config.command_prefix, "", string.len(config.command_prefix))
  103.                 print("found command: "..command)
  104.                 if(type(commands[command]) == "function") then
  105.                     local args = ulib.split(message, " ")
  106.                     table.remove(args, 1)
  107.                     local ok, err = pcall(commands[command], {
  108.                         irc = irc,
  109.                         conf = config,
  110.                         from = from,
  111.                         channel = channel,
  112.                         message = message,
  113.                         params = args
  114.                     })
  115.                     if not ok then
  116.                         irc.say(channel, "Command '"..command.."' errored with error: \""..err.."\"")
  117.                         print("[DEBUG]: Command '"..command.."' errored with error: \""..err.."\"")
  118.                     end
  119.                 else
  120.                     print(command.." wasn't a function.")
  121.                 end
  122.             end
  123.         end
  124.         do_callback("channel_msg", {
  125.             irc = irc,
  126.             conf = config,
  127.             from = from,
  128.             channel = channel,
  129.             message = message
  130.         })
  131.     end
  132. end)
  133.  
  134. irc.register_callback("private_msg", function(from, message)
  135.     print("[IRC] "..from.." -> Bot: "..message)
  136.     if(string.sub(message,1,1) == config.command_prefix) then
  137.         local message_split = ulib.split(message, " ")
  138.         if(type(message_split[1]) == "string") then
  139.             local command = string.gsub(message_split[1], config.command_prefix, "", string.len(config.command_prefix))
  140.             print("found command: "..command)
  141.             if(type(commands[command]) == "function") then
  142.                 local args = ulib.split(message, " ")
  143.                 table.remove(args, 1)
  144.                 local ok, err = pcall(commands[command], {
  145.                     irc = irc,
  146.                     conf = config,
  147.                     from = from,
  148.                     channel = from,
  149.                     message = message,
  150.                     params = args
  151.                 })
  152.                 if not ok then
  153.                     irc.say(channel, "Command '"..command.."' errored with error: \""..err.."\"")
  154.                     print("[DEBUG]: Command '"..command.."' errored with error: \""..err.."\"")
  155.                 end
  156.             else
  157.                 print(command.." wasn't a function.")
  158.             end
  159.         end
  160.     end
  161.     if(do_ignore(from) == false) then
  162.         do_callback("private_msg", {
  163.             irc = irc,
  164.             conf = config,
  165.             from = from,
  166.             message = message
  167.         })
  168.     end
  169. end)
  170.  
  171.  
  172. --Here we just load all callback registers and external commands
  173. function _G.load_modules()
  174.     loaded_modules = {}
  175.     for file in lfs.dir( current_dir.."/modules" ) do
  176.         if (file ~= "." and file ~= "..") then
  177.             local trimmed_name = string.gsub(file, ".lua", "")
  178.             if(not module_unloaded(trimmed_name)) then
  179.                 local module_func = assert(loadfile(current_dir.."/modules/"..file))
  180.                 loaded_modules[trimmed_name] = module_func()
  181.                 print("Loaded module '"..trimmed_name.."'! "..current_dir.."/modules/"..file)
  182.             else
  183.                 print("Skipped unloaded module '"..trimmed_name.."'! "..current_dir.."/modules/"..file)
  184.             end
  185.         end
  186.     end
  187.     callbacks = {}
  188.     commands = {}
  189.     for k,v in pairs(loaded_modules) do
  190.         if(type(v.callbacks) == "table") then
  191.             for a,b in pairs(v.callbacks) do
  192.                 if(type(callbacks[a]) ~= "table") then
  193.                     callbacks[a] = {}
  194.                 end
  195.                 table.insert(callbacks[a], b)
  196.             end
  197.         end
  198.         for a,b in pairs(v) do
  199.             if(type(b) == "function") then
  200.                 commands[a] = b
  201.             end
  202.         end
  203.     end
  204. end
  205.  
  206. local function load_loops()
  207.     for k,v in pairs(loaded_modules) do
  208.         if(type(v.loops) == "table") then
  209.             for a,b in pairs(v.loops) do
  210.                 if(type(b) == "function") then
  211.                     loop:wrap( b )
  212.                 end
  213.             end
  214.         end
  215.     end
  216. end
  217.  
  218. function _G.load_config()
  219.     _G.config = nil
  220.     _G.config = require("config")
  221. end
  222.  
  223.  
  224. load_modules()
  225. load_loops()
  226. load_config()
  227.  
  228. irc.DEBUG = config.DEBUG
  229.  
  230.  
  231. loop:wrap(function()
  232.     if(type(_G.discord_token) == "string") then
  233.         _G.discord_ws = websocket.new_from_uri("wss://gateway.discord.gg/?v=6&encoding=json")
  234.         assert(discord_ws:connect())
  235.         while true do
  236.             local data, opcode, thingy = discord_ws:receive()
  237.             if(data ~= nil and type(data) == "string") then
  238.                 print(tostring(data).." / "..tostring(opcode).." / "..tostring(thingy))
  239.                 local dat = assert(json:decode(data))
  240.                 if(type(dat) == "table") then
  241.                     if(dat.s ~= nil) then
  242.                         _G.last_s = dat.s
  243.                     end
  244.                     if(dat.op == 10) then --OPCODE 10 hello
  245.                         _G.heartbeat_interval = (dat.d.heartbeat_interval/1000)
  246.                         cqueues.running():wrap(function()
  247.                             print("Heartbeat setup!")
  248.                             while true do
  249.                                 print("Heartbeat pre! "..tostring(_G.heartbeat_interval))
  250.                                 cqueues.sleep(1)
  251.                                 print("Heartbeat sent!")
  252.                                 assert(_G.discord_ws:send(json:encode({op=1, d=_G.last_s}), 1, 3))
  253.                             end
  254.                         end)
  255.                         print("Sending discord identify!")
  256.                         assert(_G.discord_ws:send(json:encode({token = _G.discord_token, properties = {device = "MidoriBot", browser = "MidoriBot", os = ((type(jit) == "table" and jit.os) or "Non-JIT")}}), 2, 5))
  257.                     end
  258.                 end
  259.             end
  260.         end
  261.     end
  262. end)
  263.  
  264. loop:wrap(function()
  265.     irc.connect{
  266.         network = config.server,
  267.         nick = config.nick,
  268.         port = config.port,
  269.         username = config.username,
  270.         realname = config.realname,
  271.         pass = s_pass
  272.     }
  273. end)
  274.  
  275. while not loop:empty() do
  276.     local ok, err = loop:step()
  277.     if not ok then
  278.         error("cqueue: " .. err)
  279.     end
  280. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement