Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local lfs = require("lfs")
- local json = require("JSON")
- local cqueues = require("cqueues")
- local csleep = cqueues.sleep
- local websocket = require("http.websocket")
- _G.irc = require("irc")
- _G.ulib = assert(loadfile("ulib.lua"))() --useful stuff
- _G.current_dir = lfs.currentdir() --have this here for later
- _G.loaded_modules = {}
- _G.commands = {}
- _G.callbacks = {}
- _G.history = {}
- local loop = cqueues.new()
- if(ulib.file_exists(current_dir.."/_sconfig.lua")) then
- local _sconfig = require("_sconfig")
- print("Secret config loaded.")
- _G.s_pass = _sconfig.pass
- _G.s_ns_pass = _sconfig.nickserv_pass
- _G.google_apikey = _sconfig.google_api_key
- _G.discord_token = _sconfig.discord_token
- end
- local function do_callback(name, ...)
- if(type(callbacks[name]) ~= "table") then
- return nil
- end
- for k,v in pairs(callbacks[name]) do
- if(type(v) == "function") then
- local ok, err = pcall(v, ...)
- if not ok then
- print("[DEBUG]: Callback "..k.." errored with error \""..err.."\".")
- end
- else
- print(k.." wasn't func, it was "..type(v))
- end
- end
- end
- function _G.do_ignore(name)
- for k,v in pairs(config.ignore_list) do
- if string.lower(name) == string.lower(v) then
- return true
- end
- end
- local ignore_list = json:decode(ulib.readAll(_G.current_dir.."/ignore_list.json") or "{}") or {}
- if(ulib.in_table(ignore_list, string.lower(name))) then
- return true
- end
- return false
- end
- local function module_unloaded(name)
- local ul = ulib.readAll(current_dir.."/disable_modules.json") or "{}"
- for k,v in pairs((json:decode(ul) or "{}")) do
- if string.lower(name) == string.lower(v) then
- return true
- end
- end
- return false
- end
- function _G.join_channel(channel)
- irc.join(channel)
- _G.history[channel] = {}
- print("Joining channel "..channel.."!")
- end
- function _G.is_auth(name, chan)
- return (ulib.in_table(_G.config.authorized_users, string.lower(tostring(name))) or ulib.in_table(chan:allops(), string.lower(tostring(name))))
- end
- irc.register_callback("connect", function()
- if(s_ns_pass) then
- print("Identifying with NickServ!")
- irc.say("NickServ", "IDENTIFY "..s_ns_pass)
- end
- for k,v in pairs(config.channels) do
- join_channel(v)
- end
- do_callback("connect", {irc = irc, conf = config})
- end)
- irc.register_callback("channel_msg", function(channel, from, message)
- print("[IRC] ["..channel.."] "..from..": "..message)
- if(type(_G.history[channel]) ~= "table") then
- _G.history[channel] = {}
- end
- table.insert(_G.history[channel], {
- user = from,
- data = message
- })
- if(do_ignore(from) == false) then
- if(string.sub(message,1,1) == config.command_prefix) then
- local message_split = ulib.split(message, " ")
- if(type(message_split[1]) == "string") then
- local command = string.gsub(message_split[1], config.command_prefix, "", string.len(config.command_prefix))
- print("found command: "..command)
- if(type(commands[command]) == "function") then
- local args = ulib.split(message, " ")
- table.remove(args, 1)
- local ok, err = pcall(commands[command], {
- irc = irc,
- conf = config,
- from = from,
- channel = channel,
- message = message,
- params = args
- })
- if not ok then
- irc.say(channel, "Command '"..command.."' errored with error: \""..err.."\"")
- print("[DEBUG]: Command '"..command.."' errored with error: \""..err.."\"")
- end
- else
- print(command.." wasn't a function.")
- end
- end
- end
- do_callback("channel_msg", {
- irc = irc,
- conf = config,
- from = from,
- channel = channel,
- message = message
- })
- end
- end)
- irc.register_callback("private_msg", function(from, message)
- print("[IRC] "..from.." -> Bot: "..message)
- if(string.sub(message,1,1) == config.command_prefix) then
- local message_split = ulib.split(message, " ")
- if(type(message_split[1]) == "string") then
- local command = string.gsub(message_split[1], config.command_prefix, "", string.len(config.command_prefix))
- print("found command: "..command)
- if(type(commands[command]) == "function") then
- local args = ulib.split(message, " ")
- table.remove(args, 1)
- local ok, err = pcall(commands[command], {
- irc = irc,
- conf = config,
- from = from,
- channel = from,
- message = message,
- params = args
- })
- if not ok then
- irc.say(channel, "Command '"..command.."' errored with error: \""..err.."\"")
- print("[DEBUG]: Command '"..command.."' errored with error: \""..err.."\"")
- end
- else
- print(command.." wasn't a function.")
- end
- end
- end
- if(do_ignore(from) == false) then
- do_callback("private_msg", {
- irc = irc,
- conf = config,
- from = from,
- message = message
- })
- end
- end)
- --Here we just load all callback registers and external commands
- function _G.load_modules()
- loaded_modules = {}
- for file in lfs.dir( current_dir.."/modules" ) do
- if (file ~= "." and file ~= "..") then
- local trimmed_name = string.gsub(file, ".lua", "")
- if(not module_unloaded(trimmed_name)) then
- local module_func = assert(loadfile(current_dir.."/modules/"..file))
- loaded_modules[trimmed_name] = module_func()
- print("Loaded module '"..trimmed_name.."'! "..current_dir.."/modules/"..file)
- else
- print("Skipped unloaded module '"..trimmed_name.."'! "..current_dir.."/modules/"..file)
- end
- end
- end
- callbacks = {}
- commands = {}
- for k,v in pairs(loaded_modules) do
- if(type(v.callbacks) == "table") then
- for a,b in pairs(v.callbacks) do
- if(type(callbacks[a]) ~= "table") then
- callbacks[a] = {}
- end
- table.insert(callbacks[a], b)
- end
- end
- for a,b in pairs(v) do
- if(type(b) == "function") then
- commands[a] = b
- end
- end
- end
- end
- local function load_loops()
- for k,v in pairs(loaded_modules) do
- if(type(v.loops) == "table") then
- for a,b in pairs(v.loops) do
- if(type(b) == "function") then
- loop:wrap( b )
- end
- end
- end
- end
- end
- function _G.load_config()
- _G.config = nil
- _G.config = require("config")
- end
- load_modules()
- load_loops()
- load_config()
- irc.DEBUG = config.DEBUG
- loop:wrap(function()
- if(type(_G.discord_token) == "string") then
- _G.discord_ws = websocket.new_from_uri("wss://gateway.discord.gg/?v=6&encoding=json")
- assert(discord_ws:connect())
- while true do
- local data, opcode, thingy = discord_ws:receive()
- if(data ~= nil and type(data) == "string") then
- print(tostring(data).." / "..tostring(opcode).." / "..tostring(thingy))
- local dat = assert(json:decode(data))
- if(type(dat) == "table") then
- if(dat.s ~= nil) then
- _G.last_s = dat.s
- end
- if(dat.op == 10) then --OPCODE 10 hello
- _G.heartbeat_interval = (dat.d.heartbeat_interval/1000)
- cqueues.running():wrap(function()
- print("Heartbeat setup!")
- while true do
- print("Heartbeat pre! "..tostring(_G.heartbeat_interval))
- cqueues.sleep(1)
- print("Heartbeat sent!")
- assert(_G.discord_ws:send(json:encode({op=1, d=_G.last_s}), 1, 3))
- end
- end)
- print("Sending discord identify!")
- 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))
- end
- end
- end
- end
- end
- end)
- loop:wrap(function()
- irc.connect{
- network = config.server,
- nick = config.nick,
- port = config.port,
- username = config.username,
- realname = config.realname,
- pass = s_pass
- }
- end)
- while not loop:empty() do
- local ok, err = loop:step()
- if not ok then
- error("cqueue: " .. err)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement