diff --git a/bot.lua b/bot.lua index 7fa8542..d0672a9 100755 --- a/bot.lua +++ b/bot.lua @@ -3,7 +3,7 @@ HTTPS = require('ssl.https') URL = require('socket.url') JSON = require('dkjson') -version = '3.1' +version = '3.2' bot_init = function() -- The function run when the bot is started or reloaded. @@ -11,10 +11,8 @@ bot_init = function() -- The function run when the bot is started or reloaded. dofile('bindings.lua') -- Load Telegram bindings. dofile('utilities.lua') -- Load miscellaneous and cross-plugin functions. - bot = nil - while not bot do -- Get bot info and retry if unable to connect. - bot = getMe() - end + -- Fetch bot information. Try until it succeeds. + repeat bot = getMe() until bot bot = bot.result plugins = {} -- Load plugins. @@ -32,11 +30,16 @@ bot_init = function() -- The function run when the bot is started or reloaded. last_update = last_update or 0 -- Set loop variables: Update offset, last_cron = last_cron or os.time() -- the time of the last cron job, is_started = true -- whether the bot should be running or not. + usernames = usernames or {} -- Table to cache usernames by user ID. end on_msg_receive = function(msg) -- The fn run whenever a message is received. + if msg.from.username then + usernames[msg.from.username:lower()] = msg.from.id + end + if msg.date < os.time() - 5 then return end -- Do not process old messages. if not msg.text then msg.text = msg.caption or '' end diff --git a/config.lua b/config.lua index 6457acd..ee4271f 100755 --- a/config.lua +++ b/config.lua @@ -8,6 +8,7 @@ return { thecatapi_key = '', time_offset = 0, lang = 'en', + antisquig = false, cli_port = 4567, admin = 00000000, admin_name = 'John Smith', diff --git a/plugins/about.lua b/plugins/about.lua index f869efe..620ede8 100755 --- a/plugins/about.lua +++ b/plugins/about.lua @@ -10,10 +10,10 @@ local action = function(msg) local message = config.about_text .. '\nBased on otouto v'..version..' by topkecleon.\notouto v3 is licensed under the GPLv2.\ntopkecleon.github.io/otouto' if msg.new_chat_participant and msg.new_chat_participant.id == bot.id then - sendMessage(msg.chat.id, message) + sendMessage(msg.chat.id, message, true) return elseif string.match(msg.text_lower, '^/about[@'..bot.username..']*') then - sendMessage(msg.chat.id, message) + sendMessage(msg.chat.id, message, true) return end diff --git a/plugins/currency.lua b/plugins/currency.lua index 21ac676..aea0fa6 100755 --- a/plugins/currency.lua +++ b/plugins/currency.lua @@ -3,6 +3,7 @@ local doc = [[``` /cash [amount] to Example: /cash 5 USD to EUR Returns exchange rates for various currencies. +Source: Google Finance. ```]] local triggers = { @@ -19,7 +20,8 @@ local action = function(msg) local from = input:match('(%a%a%a) TO') local to = input:match('TO (%a%a%a)') - local amount = input:match('([%d]+) %a%a%a TO %a%a%a') or 1 + local amount = get_word(input, 2) + amount = tonumber(amount) or 1 local result = 1 local url = 'https://www.google.com/finance/converter' @@ -39,12 +41,12 @@ local action = function(msg) return end - result = str:format('%.2f') + result = string.format('%.2f', str) end - local output = amount .. ' ' .. from .. ' = ' .. result .. ' ' .. to .. '\n' - output = output .. os.date('!%F %T UTC') + local output = amount .. ' ' .. from .. ' = ' .. result .. ' ' .. to .. '\n\n' + output = output .. os.date('!%F %T UTC') .. '\nSource: Google Finance' output = '`' .. output .. '`' sendMessage(msg.chat.id, output, true, nil, true) diff --git a/plugins/gSearch.lua b/plugins/gSearch.lua index eef673a..a20a14b 100755 --- a/plugins/gSearch.lua +++ b/plugins/gSearch.lua @@ -54,7 +54,7 @@ local action = function(msg) return end - local output = '*Google: Results for* _' .. input .. '_ *:*\n' + local output = '*Google results for* _' .. input .. '_ *:*\n' for i,v in ipairs(jdat.responseData.results) do local title = jdat.responseData.results[i].titleNoFormatting:gsub('%[.+%]', ''):gsub('&', '&') if title:len() > 48 then diff --git a/plugins/help.lua b/plugins/help.lua index 997cd67..c2948c8 100755 --- a/plugins/help.lua +++ b/plugins/help.lua @@ -9,7 +9,8 @@ for i,v in ipairs(plugins) do end end -help_text = help_text .. [[\n +help_text = help_text .. [[ + /help Arguments: \[optional] ]] diff --git a/plugins/luarun.lua b/plugins/luarun.lua index cf7d246..a4cd585 100644 --- a/plugins/luarun.lua +++ b/plugins/luarun.lua @@ -15,10 +15,12 @@ local action = function(msg) end local output = loadstring(input)() - if not output then + if output == nil then output = 'Done!' + elseif type(output) == 'table' then + output = 'Done! Table returned.' else - output = '```\n' .. output .. '\n```' + output = '```\n' .. tostring(output) .. '\n```' end sendMessage(msg.chat.id, output, true, msg.message_id, true) diff --git a/plugins/moderation.lua b/plugins/moderation.lua index 1ebfa1d..d69b2f1 100755 --- a/plugins/moderation.lua +++ b/plugins/moderation.lua @@ -10,9 +10,7 @@ local commands = { ['^/modhelp[@'..bot.username..']*$'] = function(msg) - if not moddat[msg.chat.id_str] then - return config.errors.moderation - end + if not moddat[msg.chat.id_str] then return end local message = [[ /modlist - List the moderators and administrators of this group. @@ -33,9 +31,7 @@ local commands = { ['^/modlist[@'..bot.username..']*$'] = function(msg) - if not moddat[msg.chat.id_str] then - return config.errors.moderation - end + if not moddat[msg.chat.id_str] then return end local message = '' @@ -113,9 +109,7 @@ local commands = { ['^/modprom[@'..bot.username..']*$'] = function(msg) - if not moddat[msg.chat.id_str] then - return config.errors.moderation - end + if not moddat[msg.chat.id_str] then return end if not config.moderation.admins[msg.from.id_str] then return config.errors.not_admin @@ -145,9 +139,7 @@ local commands = { ['^/moddem[@'..bot.username..']*'] = function(msg) - if not moddat[msg.chat.id_str] then - return config.errors.moderation - end + if not moddat[msg.chat.id_str] then return end if not config.moderation.admins[msg.from.id_str] then return config.errors.not_admin @@ -181,9 +173,7 @@ local commands = { ['/modkick[@'..bot.username..']*'] = function(msg) - if not moddat[msg.chat.id_str] then - return config.errors.moderation - end + if not moddat[msg.chat.id_str] then return end if not moddat[msg.chat.id_str][msg.from.id_str] then if not config.moderation.admins[msg.from.id_str] then @@ -215,9 +205,7 @@ local commands = { ['^/modban[@'..bot.username..']*'] = function(msg) - if not moddat[msg.chat.id_str] then - return config.errors.moderation - end + if not moddat[msg.chat.id_str] then return end if not moddat[msg.chat.id_str][msg.from.id_str] then if not config.moderation.admins[msg.from.id_str] then diff --git a/plugins/reddit.lua b/plugins/reddit.lua index 713e030..fefea3a 100755 --- a/plugins/reddit.lua +++ b/plugins/reddit.lua @@ -30,7 +30,7 @@ local action = function(msg) source = '*/r/' .. input:match('^r/(.+)') .. '*\n' else url = 'http://www.reddit.com/search.json?q=' .. input .. '&limit=' .. limit - source = '*reddit: Results for* _' .. input .. '_ *:*\n' + source = '*reddit results for* _' .. input .. '_ *:*\n' end else url = 'http://www.reddit.com/.json?limit=' .. limit diff --git a/utilities.lua b/utilities.lua index dea5716..75ed5af 100755 --- a/utilities.lua +++ b/utilities.lua @@ -1,7 +1,8 @@ -- utilities.lua -- Functions shared among plugins. -function get_word(s, i) -- get the indexed word in a string + -- get the indexed word in a string +get_word = function(s, i) s = s or '' i = i or 1 @@ -15,7 +16,8 @@ function get_word(s, i) -- get the indexed word in a string end -function string:input() -- Returns the string after the first space. + -- Returns the string after the first space. +function string:input() if not self:find(' ') then return false end @@ -57,14 +59,16 @@ local lc_list = { ['!'] = 'ǃ' } -function latcyr(str) -- Replaces letters with corresponding Cyrillic characters. + -- Replaces letters with corresponding Cyrillic characters. +latcyr = function(str) for k,v in pairs(lc_list) do str = string.gsub(str, k, v) end return str end -function load_data(filename) -- Loads a JSON file as a table. + -- Loads a JSON file as a table. +load_data = function(filename) local f = io.open(filename) if not f then @@ -78,7 +82,8 @@ function load_data(filename) -- Loads a JSON file as a table. end -function save_data(filename, data) -- Saves a table to a JSON file. + -- Saves a table to a JSON file. +save_data = function(filename, data) local s = JSON.encode(data) local f = io.open(filename, 'w') @@ -88,7 +93,7 @@ function save_data(filename, data) -- Saves a table to a JSON file. end -- Gets coordinates for a location. Used by gMaps.lua, time.lua, weather.lua. -function get_coords(input) +get_coords = function(input) local url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input) @@ -107,4 +112,31 @@ function get_coords(input) lon = jdat.results[1].geometry.location.lng } +end + + -- Get the number of values in a key/value table. +table_size = function(tab) + + local i = 0 + for k,v in pairs(tab) do + i = i + 1 + end + return i + +end + +resolve_username = function(target) + -- If $target is a known username, returns associated ID. + -- If $target is an unknown username, returns nil. + -- If $target is a number, returns that number. + -- Otherwise, returns false. + + local input = tostring(target):lower() + if input:match('^@') then + local uname = input:gsub('^@', '') + return usernames[uname] + else + return tonumber(target) or false + end + end