From ca17b716ba9811411956d86c2357052409f4e12d Mon Sep 17 00:00:00 2001 From: Yessiest Date: Fri, 20 May 2022 22:20:49 +0400 Subject: [PATCH] ain't afraid of no sleep, --- libraries/air.lua | 368 ++++++++++++++++++------------------- libraries/fake_message.lua | 92 +++++----- libraries/file.lua | 166 ++++++++--------- libraries/import.lua | 64 +++---- libraries/luaqalc | 2 +- libraries/markov.lua | 172 ++++++++--------- libraries/purify.lua | 44 ++--- libraries/table-utils.lua | 40 ++-- libraries/unixToString.lua | 14 +- 9 files changed, 481 insertions(+), 481 deletions(-) diff --git a/libraries/air.lua b/libraries/air.lua index 048a2e9..10314e0 100644 --- a/libraries/air.lua +++ b/libraries/air.lua @@ -2,202 +2,202 @@ --P.S: air stands for Advanced Input Recognition, although technically it's not all that advanced air = {} air.match_strings = function(string) - local strings = {} - string = string:gsub("\"(.-[^\\])\"",function(capt) - string_id = string_id + 1 - strings["%str"..string_id] = capt:gsub("\\\"","\"") - return " %str"..string_id - end) - return string,strings + local strings = {} + string = string:gsub("\"(.-[^\\])\"",function(capt) + string_id = string_id + 1 + strings["%str"..string_id] = capt:gsub("\\\"","\"") + return " %str"..string_id + end) + return string,strings end local function parse_strings(thing,strings) - --find all strings and replace them with string ids with no spaces - local strings = strings or {} - local string_count = 0 - local function get_string(text) - string_count = string_count + 1 - local id = "&;"..tostring(string_count)..";&" - strings[string_count] = text - return id - end - thing = thing:gsub("(\")\"",get_string) - thing = thing:gsub("(\".-[^\\])\"",get_string) - thing = thing:gsub("(\')\'",get_string) - thing = thing:gsub("(\'.-[^\\])\'",get_string) - return thing,strings + --find all strings and replace them with string ids with no spaces + local strings = strings or {} + local string_count = 0 + local function get_string(text) + string_count = string_count + 1 + local id = "&;"..tostring(string_count)..";&" + strings[string_count] = text + return id + end + thing = thing:gsub("(\")\"",get_string) + thing = thing:gsub("(\".-[^\\])\"",get_string) + thing = thing:gsub("(\')\'",get_string) + thing = thing:gsub("(\'.-[^\\])\'",get_string) + return thing,strings end --this table will look up special types special_case = { - ["voiceChannel"] = function(id,client,guild_id) - local guild = client:getGuild(guild_id) - local channel = guild:getChannel(id:match("(%d+)[^%d]*$")) - if tostring(channel):match("^GuildVoiceChannel: ") then - return true,channel - else - return false + ["voiceChannel"] = function(id,client,guild_id) + local guild = client:getGuild(guild_id) + local channel = guild:getChannel(id:match("(%d+)[^%d]*$")) + if tostring(channel):match("^GuildVoiceChannel: ") then + return true,channel + else + return false + end + end, + ["textChannel"] = function(id,client,guild_id) + local guild = client:getGuild(guild_id) + local channel = guild:getChannel(id:match("(%d+)[^%d]*$")) + if tostring(channel):match("^GuildTextChannel: ") then + return true,channel + else + return false + end + end, + ["messageLink"] = function(id,client,guild_id) + local guild = client:getGuild(guild_id) + local channelId,messageId = id:match("(%d+)/(%d+)[^%d]*$") + channel = guild:getChannel(channelId) + if tostring(channel):find("GuildTextChannel") then + message = channel:getMessage(messageId) + if message then + return true,message + end + end + return false + end, + ["role"] = function(id,client,guild_id) + local guild = client:getGuild(guild_id) + local role = guild:getRole(id:match("(%d+)[^%d]*$")) + if role then + return true,role + else + return false + end + end, + ["member"] = function(id,client,guild_id) + local guild = client:getGuild(guild_id) + local member = guild:getMember(id:match("(%d+)[^%d]*$")) + if member then + return true,member + else + return false + end + end, + ["emoji"] = function(id,client,guild_id) + local guild = client:getGuild(guild_id) + local emoji = guild:getEmoji(id:match("(%d+)[^%d]*$")) + if emoji then + return true,emoji + else + return false + end + end, + ["ban"] = function(id,client,guild_id) + local guild = client:getGuild(guild_id) + local ban = guild:getBan(id:match("(%d+)[^%d]*$")) + if ban then + return true,ban + else + return false + end + end, + ["channel"] = function(id,client,guild_id) + local guild = client:getGuild(guild_id) + local channel = guild:getChannel(id:match("(%d+)[^%d]*$")) + if channel then + return true,channel + else + return false + end + end, + ["user"] = function(id,client,guild_id) + local user = client:getUser(id:match("(%d+)[^%d]*$")) + if user then + return true,user + end + return false + end, + ["id"] = function(id) + if tonumber(id:match("(%d+)[^%d]*$")) and tostring(id:match("(%d+)[^%d]*$")):len() > 10 then + return true,id + end + return false end - end, - ["textChannel"] = function(id,client,guild_id) - local guild = client:getGuild(guild_id) - local channel = guild:getChannel(id:match("(%d+)[^%d]*$")) - if tostring(channel):match("^GuildTextChannel: ") then - return true,channel - else - return false - end - end, - ["messageLink"] = function(id,client,guild_id) - local guild = client:getGuild(guild_id) - local channelId,messageId = id:match("(%d+)/(%d+)[^%d]*$") - channel = guild:getChannel(channelId) - if tostring(channel):find("GuildTextChannel") then - message = channel:getMessage(messageId) - if message then - return true,message - end - end - return false - end, - ["role"] = function(id,client,guild_id) - local guild = client:getGuild(guild_id) - local role = guild:getRole(id:match("(%d+)[^%d]*$")) - if role then - return true,role - else - return false - end - end, - ["member"] = function(id,client,guild_id) - local guild = client:getGuild(guild_id) - local member = guild:getMember(id:match("(%d+)[^%d]*$")) - if member then - return true,member - else - return false - end - end, - ["emoji"] = function(id,client,guild_id) - local guild = client:getGuild(guild_id) - local emoji = guild:getEmoji(id:match("(%d+)[^%d]*$")) - if emoji then - return true,emoji - else - return false - end - end, - ["ban"] = function(id,client,guild_id) - local guild = client:getGuild(guild_id) - local ban = guild:getBan(id:match("(%d+)[^%d]*$")) - if ban then - return true,ban - else - return false - end - end, - ["channel"] = function(id,client,guild_id) - local guild = client:getGuild(guild_id) - local channel = guild:getChannel(id:match("(%d+)[^%d]*$")) - if channel then - return true,channel - else - return false - end - end, - ["user"] = function(id,client,guild_id) - local user = client:getUser(id:match("(%d+)[^%d]*$")) - if user then - return true,user - end - return false - end, - ["id"] = function(id) - if tonumber(id:match("(%d+)[^%d]*$")) and tostring(id:match("(%d+)[^%d]*$")):len() > 10 then - return true,id - end - return false - end } air.parse = function(string,argmatch,client,guild_id) - local args,opts = {},{} - local string_id = 0 - local strings = {} - string = string:gsub("[%s\n]+\"\"",function(capt) - string_id = string_id + 1 - strings["%str"..string_id] = "" - return " %str"..string_id - end) - string = string:gsub("[%s\n]+\"(.-[^\\])\"",function(capt) - string_id = string_id + 1 - strings["%str"..string_id] = capt:gsub("\\\"","\"") - return " %str"..string_id - end) - string = string:gsub("[%s\n]+%-%-(%w+)=\"\"",function(name) - opts[name] = "" - return "" - end) - string = string:gsub("[%s\n]+%-%-(%w+)=\"(.-[^\\])\"",function(name,value) - opts[name] = value:gsub("\\\"","\"") - return "" - end) - string = string:gsub("[%s\n]+%-%-(%w+)=(%S+)",function(name,value) - opts[name] = value - return "" - end) - string = string:gsub("[%s\n]+%-%-(%w+)",function(name) - opts[name] = true - return "" - end) - string = string:gsub("[%s\n]+%-(%w+)",function(args) - args:gsub(".",function(key) - opts[key] = true + local args,opts = {},{} + local string_id = 0 + local strings = {} + string = string:gsub("[%s\n]+\"\"",function(capt) + string_id = string_id + 1 + strings["%str"..string_id] = "" + return " %str"..string_id + end) + string = string:gsub("[%s\n]+\"(.-[^\\])\"",function(capt) + string_id = string_id + 1 + strings["%str"..string_id] = capt:gsub("\\\"","\"") + return " %str"..string_id + end) + string = string:gsub("[%s\n]+%-%-(%w+)=\"\"",function(name) + opts[name] = "" + return "" + end) + string = string:gsub("[%s\n]+%-%-(%w+)=\"(.-[^\\])\"",function(name,value) + opts[name] = value:gsub("\\\"","\"") + return "" + end) + string = string:gsub("[%s\n]+%-%-(%w+)=(%S+)",function(name,value) + opts[name] = value + return "" + end) + string = string:gsub("[%s\n]+%-%-(%w+)",function(name) + opts[name] = true + return "" + end) + string = string:gsub("[%s\n]+%-(%w+)",function(args) + args:gsub(".",function(key) + opts[key] = true + end) + return "" + end) + string:gsub("([^%s\n]+)",function(match) + table.insert(args,match) end) - return "" - end) - string:gsub("([^%s\n]+)",function(match) - table.insert(args,match) - end) - for k,v in pairs(args) do - if v:match("%%str%d+") then - if strings[v] then - args[k] = strings[v] - end - end - end - if argmatch and #argmatch > 0 then - local match,err = false - local new_args = {} - for k,v in pairs(argmatch) do - if not args[k] then - match = false - err = "Missing arguments: "..table.concat(argmatch,", ",k) - break - end - if v == "number" and tonumber(args[k]) then - match = true - new_args[k] = tonumber(args[k]) - elseif v == "string" then - match = true - new_args[k] = args[k] - elseif special_case[v] then - match,new_args[k] = special_case[v](args[k],client,guild_id) - else - match = false - end - if match == false then - err = "Type mismatch for argument "..k..": "..argmatch[k].." expected." - break - end - end for k,v in pairs(args) do - if not new_args[k] then - new_args[k] = v - end + if v:match("%%str%d+") then + if strings[v] then + args[k] = strings[v] + end + end + end + if argmatch and #argmatch > 0 then + local match,err = false + local new_args = {} + for k,v in pairs(argmatch) do + if not args[k] then + match = false + err = "Missing arguments: "..table.concat(argmatch,", ",k) + break + end + if v == "number" and tonumber(args[k]) then + match = true + new_args[k] = tonumber(args[k]) + elseif v == "string" then + match = true + new_args[k] = args[k] + elseif special_case[v] then + match,new_args[k] = special_case[v](args[k],client,guild_id) + else + match = false + end + if match == false then + err = "Type mismatch for argument "..k..": "..argmatch[k].." expected." + break + end + end + for k,v in pairs(args) do + if not new_args[k] then + new_args[k] = v + end + end + return match,new_args,opts,err + else + return true,args,opts end - return match,new_args,opts,err - else - return true,args,opts - end end return air diff --git a/libraries/fake_message.lua b/libraries/fake_message.lua index c912305..c0d2e36 100644 --- a/libraries/fake_message.lua +++ b/libraries/fake_message.lua @@ -1,48 +1,48 @@ return function(message,overrides) - assert(type(message) == "table","table expected, got "..type(message)) - assert(type(overrides) == "table","table expected for arg#2, got "..type(overrides)) - local fake = {content = message.content, - author = message.author, - member = message.guild:getMember(message.author.id), - channel = message.channel, - client = message.client, - guild = message.guild, - delete = function() message:delete() end, - reply = function(thing,...) - message.channel:send(...) - end, - createdAt = message.createdAt, - _parent = message.parent, - parent = message.parent, - id = message.id, - attachment = message.attachment, - attachments = message.attachments, - cleanContent = message.cleanContent, - editedTimestamp = message.editedTimestamp, - embed = message.embed, - embeds = message.embeds, - link = message.link, - mentionedChannels = message.mentionedChannels, - mentionedEmojis = message.mentionedEmojis, - mentionedRoles = message.mentionedRoles, - mentionedUsers = message.mentionedUsers, - nonce = message.nonce, - oldContent = message.oldContent, - pinned = message.pinned, - reactions = message.reactions, - tts = message.tts, - type = message.type, - webhookId = message.webhookId, - addReaction = function(self,...) - message:addReaction(...) - end, - removeReaction = function(self,...) - message:removeReaction(...) - end, - emulated = true - } - for k,v in pairs(overrides) do - fake[k] = v - end - return fake + assert(type(message) == "table","table expected, got "..type(message)) + assert(type(overrides) == "table","table expected for arg#2, got "..type(overrides)) + local fake = {content = message.content, + author = message.author, + member = message.guild:getMember(message.author.id), + channel = message.channel, + client = message.client, + guild = message.guild, + delete = function() message:delete() end, + reply = function(thing,...) + message.channel:send(...) + end, + createdAt = message.createdAt, + _parent = message.parent, + parent = message.parent, + id = message.id, + attachment = message.attachment, + attachments = message.attachments, + cleanContent = message.cleanContent, + editedTimestamp = message.editedTimestamp, + embed = message.embed, + embeds = message.embeds, + link = message.link, + mentionedChannels = message.mentionedChannels, + mentionedEmojis = message.mentionedEmojis, + mentionedRoles = message.mentionedRoles, + mentionedUsers = message.mentionedUsers, + nonce = message.nonce, + oldContent = message.oldContent, + pinned = message.pinned, + reactions = message.reactions, + tts = message.tts, + type = message.type, + webhookId = message.webhookId, + addReaction = function(self,...) + message:addReaction(...) + end, + removeReaction = function(self,...) + message:removeReaction(...) + end, + emulated = true + } + for k,v in pairs(overrides) do + fake[k] = v + end + return fake end diff --git a/libraries/file.lua b/libraries/file.lua index b349bff..10e9d4b 100644 --- a/libraries/file.lua +++ b/libraries/file.lua @@ -2,112 +2,112 @@ local file = {} local json if pcall(import,"json") then - json = import("json") + json = import("json") elseif pcall(require,"json") then - json = require("json") + json = require("json") end file.safe = true file.read = function(filename,mode) - assert(type(filename) == "string","string expected, got "..type(filename)) - local mode = mode or "*a" - local temp_file,err = io.open(filename,r) - if err then - if not file.safe then error(err) else - ret_string = "" + assert(type(filename) == "string","string expected, got "..type(filename)) + local mode = mode or "*a" + local temp_file,err = io.open(filename,r) + if err then + if not file.safe then error(err) else + ret_string = "" + end + else + ret_string = temp_file:read(mode) + temp_file:close() end - else - ret_string = temp_file:read(mode) - temp_file:close() - end - return ret_string,err + return ret_string,err end file.write = function(filename,write) - assert(type(filename) == "string", "string expected, got "..type(filename)) - assert(type(write) == "string", "string expected for argument #2, got "..type(write)) - local temp_file,err = io.open(filename,"w+") - local status = false - if err then - if not file.safe then error(err) else - status = false + assert(type(filename) == "string", "string expected, got "..type(filename)) + assert(type(write) == "string", "string expected for argument #2, got "..type(write)) + local temp_file,err = io.open(filename,"w+") + local status = false + if err then + if not file.safe then error(err) else + status = false + end + else + temp_file:write(write) + temp_file:close() + status = true end - else - temp_file:write(write) - temp_file:close() - status = true - end - return status,err + return status,err end file.exists = function(filename) - local file = io.open(filename,"r") - if file then - file:close() - return true - else - return false - end + local file = io.open(filename,"r") + if file then + file:close() + return true + else + return false + end end file.existsDir = function(filename) - local file = io.open(filename.."/stuff","w") - if file then - file:close() - os.remove(filename.."/stuff") - return true - else - return false - end + local file = io.open(filename.."/stuff","w") + if file then + file:close() + os.remove(filename.."/stuff") + return true + else + return false + end end file.ls = function(path) - if file.existsDir(path) then - local ls_handle = io.popen("ls -1 "..path,"r") - local ls_data = ls_handle:read("*a") - ls_handle:close() - return ls_data - else - return false, "No such file or directory" - end + if file.existsDir(path) then + local ls_handle = io.popen("ls -1 "..path,"r") + local ls_data = ls_handle:read("*a") + ls_handle:close() + return ls_data + else + return false, "No such file or directory" + end end if json then - file.readJSON = function(filename,default) - assert(type(filename) == "string","string expected, got "..type(filename)) - local json_data,err = file.read(filename,"*a") - local table_data, status - if err then - if not file.safe then error(err) else - status = err - table_data = default or {} - end - else - table_data,_,err = json.decode(json_data) - if not table_data then - if not file.safe then error(err) else - status = err - table_data = default or {} + file.readJSON = function(filename,default) + assert(type(filename) == "string","string expected, got "..type(filename)) + local json_data,err = file.read(filename,"*a") + local table_data, status + if err then + if not file.safe then error(err) else + status = err + table_data = default or {} + end + else + table_data,_,err = json.decode(json_data) + if not table_data then + if not file.safe then error(err) else + status = err + table_data = default or {} + end + end end - end + return table_data, status end - return table_data, status - end - file.writeJSON = function(filename,table_data) - assert(type(filename) == "string","string expected, got "..type(filename)) - assert(type(table_data) == "table","table expected, got "..type(table_data)) - local status = false - local status,json_object,_,err = pcall(function() return json.encode(table_data) end) - if not status then - if not file.safe then error(err) else - status = false - err = json_object - end - else - if json_object then - status,err = file.write(filename,json_object) - end + file.writeJSON = function(filename,table_data) + assert(type(filename) == "string","string expected, got "..type(filename)) + assert(type(table_data) == "table","table expected, got "..type(table_data)) + local status = false + local status,json_object,_,err = pcall(function() return json.encode(table_data) end) + if not status then + if not file.safe then error(err) else + status = false + err = json_object + end + else + if json_object then + status,err = file.write(filename,json_object) + end + end + return status, err end - return status, err - end end return file diff --git a/libraries/import.lua b/libraries/import.lua index 4cbc416..3febeed 100644 --- a/libraries/import.lua +++ b/libraries/import.lua @@ -1,39 +1,39 @@ --Luvit's deadly sin - a library that fixes the dumbest problem in luvit --That is, unability to load core modules from the files that were required return function(reqfunc) - local function import(path) - local paths = { } - package.path:gsub("[^;]+",function(path) - table.insert(paths,path) - end) - local filename = path:gsub("%.","/") - local file = io.open(filename..".lua","r") - local iterator = 0 - local last_filename = "" - while not file do - iterator = iterator + 1 - if paths[iterator] then - file = io.open(paths[iterator]:gsub("%?",filename),"r") - last_filename = paths[iterator] - else - break - end + local function import(path) + local paths = {} + package.path:gsub("[^;]+",function(path) + table.insert(paths,path) + end) + local filename = path:gsub("%.","/") + local file = io.open(filename..".lua","r") + local iterator = 0 + local last_filename = "" + while not file do + iterator = iterator + 1 + if paths[iterator] then + file = io.open(paths[iterator]:gsub("%?",filename),"r") + last_filename = paths[iterator] + else + break + end + end + if not file then + return reqfunc(path) + else + content = file:read("*a") + local f,err = load(content,"import: "..filename,nil,setmetatable({ + require = reqfunc, + import = import, + },{__index = _G})) + if err then + error("[import: "..filname.."] "..tostring(err)) + end + return f() + end end - if not file then - return reqfunc(path) - else - content = file:read("*a") - local func,err = load(content,"import: "..filename,nil,setmetatable({ - require = reqfunc, - import = import, - },{__index = _G})) - if err then - error(err) - end - return func() - end - end - return import + return import end --[[ Usage: diff --git a/libraries/luaqalc b/libraries/luaqalc index 9e6f090..23fcea1 160000 --- a/libraries/luaqalc +++ b/libraries/luaqalc @@ -1 +1 @@ -Subproject commit 9e6f09050f9ebecc4ffcea1234b920468254a79b +Subproject commit 23fcea17c2eb380b6a6b1ba848d5bc7ebdc38b05 diff --git a/libraries/markov.lua b/libraries/markov.lua index 0b7e1af..696f69c 100644 --- a/libraries/markov.lua +++ b/libraries/markov.lua @@ -1,123 +1,123 @@ local markov = {} local function node(relations) - local node = {} - local total = 0 - for k,v in pairs(relations) do - total = total + v.occurences - end - for k,v in pairs(relations) do - node[k] = {probability = v.occurences/total,occurences = v.occurences} - end - return node + local node = {} + local total = 0 + for k,v in pairs(relations) do + total = total + v.occurences + end + for k,v in pairs(relations) do + node[k] = {probability = v.occurences/total,occurences = v.occurences} + end + return node end local function escape(str) - return str:gsub("([%%%*%(%)%^%.%[%]%+%-%$%?])","%%%1") + return str:gsub("([%%%*%(%)%^%.%[%]%+%-%$%?])","%%%1") end local function register_words(str,word_list) - local word_list = word_list or {} - str:gsub("%S+",function(word) - if not word_list[word] then - word_list[word] = {} - end - local current_word = word_list[word] - local escaped_word = escape(word) - str:gsub("%s+" .. escaped_word .. "%s+(%S+)",function(word2) - if not current_word[word2] then - current_word[word2] = {} + local word_list = word_list or {} + str:gsub("%S+",function(word) + if not word_list[word] then + word_list[word] = {} end - if not current_word[word2].occurences then - current_word[word2].occurences = 1 - else - current_word[word2].occurences = current_word[word2].occurences + 1 - end - end) - end) - for k,v in pairs(word_list) do - word_list[k] = node(v) - end - return word_list + local current_word = word_list[word] + local escaped_word = escape(word) + str:gsub("%s+" .. escaped_word .. "%s+(%S+)",function(word2) + if not current_word[word2] then + current_word[word2] = {} + end + if not current_word[word2].occurences then + current_word[word2].occurences = 1 + else + current_word[word2].occurences = current_word[word2].occurences + 1 + end + end) + end) + for k,v in pairs(word_list) do + word_list[k] = node(v) + end + return word_list end local table_length = function(tab) - local len = 0 - for k,v in pairs(tab) do - len = len + 1 - end - return len + local len = 0 + for k,v in pairs(tab) do + len = len + 1 + end + return len end function markov.walk(self,start) - if not self.init then - error("Attempted to use an instance method on an uninitialized instance") - end - local random = math.random(0,1e7)/1e7 - local words = {} - words.count = 0 - local word = nil - if self.net[start] then - while (words.count < 1) and (table_length(self.net[start]) > 0) do - for k,v in pairs(self.net[start]) do - if (random <= v.probability) then - words.count = words.count + 1 - table.insert(words,k) - end - end - random = math.random(0,1e7)/1e7 + if not self.init then + error("Attempted to use method on uninitialized instances") end - end - if words.count > 0 then - word = words[math.random(1,#words)] - end - return word + local random = math.random(0,1e7)/1e7 + local words = {} + words.count = 0 + local word = nil + if self.net[start] then + while (words.count < 1) and (table_length(self.net[start]) > 0) do + for k,v in pairs(self.net[start]) do + if (random <= v.probability) then + words.count = words.count + 1 + table.insert(words,k) + end + end + random = math.random(0,1e7)/1e7 + end + end + if words.count > 0 then + word = words[math.random(1,#words)] + end + return word end function markov.expand_vocabulary(self,source) - if not self.init then - error("Attempted to use an instance method on an uninitialized instance") - end - self.net = register_words(source,self.net) + if not self.init then + error("Attempted to use method on uninitialized instances") + end + self.net = register_words(source,self.net) end function markov.save_state(self) - return self.net + return self.net end function markov.load_state(self,new_state) - self.net = new_state + self.net = new_state end function markov.run(self,start,count) - if not self.init then - error("Attempted to use an instance method on an uninitialized instance") - end - if not start then - for k,v in pairs(self.net) do - start = k - break + if not self.init then + error("Attempted to use an instance method on an uninitialized instance") end - end - local sequence = "" - local current_word = start - while current_word do - sequence = sequence..current_word.." " - local _,counter = sequence:gsub("(%S+)","%1") - current_word = self:walk(current_word) - if counter > (count or 200) then - sequence = sequence:sub(1,-2).."..." - break + if not start then + for k,v in pairs(self.net) do + start = k + break + end end - end - return sequence + local sequence = "" + local current_word = start + while current_word do + sequence = sequence..current_word.." " + local _,counter = sequence:gsub("(%S+)","%1") + current_word = self:walk(current_word) + if counter > (count or 200) then + sequence = sequence:sub(1,-2).."..." + break + end + end + return sequence end function markov.new(str) - local self = setmetatable({},{__index = markov}) - self.net = register_words(str or "") - self.init = true - return self + local self = setmetatable({},{__index = markov}) + self.net = register_words(str or "") + self.init = true + return self end return markov diff --git a/libraries/purify.lua b/libraries/purify.lua index 1294730..d77dd38 100644 --- a/libraries/purify.lua +++ b/libraries/purify.lua @@ -1,32 +1,32 @@ --string purifier library local purify = {} purify.purify_pings = function(msg,input) - local text = input - while text:match("<@(%D*)(%d*)>") do - local obj,id = text:match("<@(%D*)(%d*)>") - local substitution = "" - if obj:match("!") then - local member = msg.guild:getMember(id) - if member then - substitution = "@"..member.name - end - elseif obj:match("&") then - local role = msg.guild:getRole(id) - if role then - substitution = "@"..role.name - end + local text = input + while text:match("<@(%D*)(%d*)>") do + local obj,id = text:match("<@(%D*)(%d*)>") + local substitution = "" + if obj:match("!") then + local member = msg.guild:getMember(id) + if member then + substitution = "@"..member.name + end + elseif obj:match("&") then + local role = msg.guild:getRole(id) + if role then + substitution = "@"..role.name + end + end + if substitution == "" then + substitution = "<\\@"..obj..id..">" + end + text = text:gsub("<@(%D*)"..id..">",substitution) end - if substitution == "" then - substitution = "<\\@"..obj..id..">" - end - text = text:gsub("<@(%D*)"..id..">",substitution) - end - return text + return text end purify.purify_escapes = function(text) - local match = "([%(%)%.%%%+%-%*%?%[%]%^%$])" - return text:gsub(match,"%%%1") + local match = "([%(%)%.%%%+%-%*%?%[%]%^%$])" + return text:gsub(match,"%%%1") end return purify diff --git a/libraries/table-utils.lua b/libraries/table-utils.lua index 240e2f3..0bfd817 100644 --- a/libraries/table-utils.lua +++ b/libraries/table-utils.lua @@ -15,42 +15,42 @@ utilities.deepcopy = function(orig) return copy end utilities.slice = function(list,start,list_end) - local output = {} - for I = (start or 1),(list_end or #table) do + local output = {} + for I = (start or 1),(list_end or #table) do table.insert(output,list[I]) - end - return output + end + return output end utilities.shallowcopy = function(orig) - local copy = {} - for k,v in pairs(orig) do + local copy = {} + for k,v in pairs(orig) do copy[k] = v - end - return copy + end + return copy end --overwrite the original table's properties with new properties utilities.overwrite = function(original,overwrite) - local new = utilities.shallowcopy(original) - for k,v in pairs(overwrite) do + local new = utilities.shallowcopy(original) + for k,v in pairs(overwrite) do new[k] = v - end - return new + end + return new end --merge all objects passed as arguments into a table. --if the object is a table, merge all of it's contents with the table utilities.merge = function(...) - local args = {...} - local new = {} - for k,v in pairs(args) do + local args = {...} + local new = {} + for k,v in pairs(args) do if type(v) == "table" then - for k2,v2 in pairs(v) do + for k2,v2 in pairs(v) do table.insert(new,v2) - end + end else - table.insert(new,v) + table.insert(new,v) end - end - return new + end + return new end utilities.remove_value = function(tb,v) local id_to_remove = nil diff --git a/libraries/unixToString.lua b/libraries/unixToString.lua index 056b0d0..2d52013 100644 --- a/libraries/unixToString.lua +++ b/libraries/unixToString.lua @@ -1,9 +1,9 @@ return function(sec) - local hours = math.floor(sec/3600) - local minutes = math.floor((sec - hours*3600)/60) - local seconds = sec - (hours*3600) - (minutes*60) - hours = ((hours < 10) and ("0"..hours)) or hours - minutes = ((minutes < 10) and ("0"..minutes)) or minutes - seconds = ((seconds < 10) and ("0"..seconds)) or seconds - return ((tonumber(hours) > 0 and hours..":") or "")..minutes..":"..seconds + local hours = math.floor(sec/3600) + local minutes = math.floor((sec - hours*3600)/60) + local seconds = sec - (hours*3600) - (minutes*60) + hours = ((hours < 10) and ("0"..hours)) or hours + minutes = ((minutes < 10) and ("0"..minutes)) or minutes + seconds = ((seconds < 10) and ("0"..seconds)) or seconds + return ((tonumber(hours) > 0 and hours..":") or "")..minutes..":"..seconds end