Username cachine and various fixes.

Usernames seen by the bot are now cached in the $usernames table. To get the ID associated with a
username, use the resolve_username() function from utilities.lua.
The table_size() function in utilities.lua will tell you the number of items in a key/pair table.
about.lua no longer displays a link preview in the about message.
currency.lua now accepts decimal arguments for the amount.
luarun.lua now correctly displays "false" return values.
moderation.lua will no longer send "I do not administrate this group".
This commit is contained in:
topkecleon 2016-01-12 05:22:28 -05:00
parent d1e0e939b5
commit c50b1ca3fa
10 changed files with 69 additions and 40 deletions

13
bot.lua
View File

@ -3,7 +3,7 @@ HTTPS = require('ssl.https')
URL = require('socket.url') URL = require('socket.url')
JSON = require('dkjson') JSON = require('dkjson')
version = '3.1' version = '3.2'
bot_init = function() -- The function run when the bot is started or reloaded. 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('bindings.lua') -- Load Telegram bindings.
dofile('utilities.lua') -- Load miscellaneous and cross-plugin functions. dofile('utilities.lua') -- Load miscellaneous and cross-plugin functions.
bot = nil -- Fetch bot information. Try until it succeeds.
while not bot do -- Get bot info and retry if unable to connect. repeat bot = getMe() until bot
bot = getMe()
end
bot = bot.result bot = bot.result
plugins = {} -- Load plugins. 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_update = last_update or 0 -- Set loop variables: Update offset,
last_cron = last_cron or os.time() -- the time of the last cron job, 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. is_started = true -- whether the bot should be running or not.
usernames = usernames or {} -- Table to cache usernames by user ID.
end end
on_msg_receive = function(msg) -- The fn run whenever a message is received. 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 msg.date < os.time() - 5 then return end -- Do not process old messages.
if not msg.text then msg.text = msg.caption or '' end if not msg.text then msg.text = msg.caption or '' end

View File

@ -8,6 +8,7 @@ return {
thecatapi_key = '', thecatapi_key = '',
time_offset = 0, time_offset = 0,
lang = 'en', lang = 'en',
antisquig = false,
cli_port = 4567, cli_port = 4567,
admin = 00000000, admin = 00000000,
admin_name = 'John Smith', admin_name = 'John Smith',

View File

@ -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' 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 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 return
elseif string.match(msg.text_lower, '^/about[@'..bot.username..']*') then elseif string.match(msg.text_lower, '^/about[@'..bot.username..']*') then
sendMessage(msg.chat.id, message) sendMessage(msg.chat.id, message, true)
return return
end end

View File

@ -3,6 +3,7 @@ local doc = [[```
/cash [amount] <from> to <to> /cash [amount] <from> to <to>
Example: /cash 5 USD to EUR Example: /cash 5 USD to EUR
Returns exchange rates for various currencies. Returns exchange rates for various currencies.
Source: Google Finance.
```]] ```]]
local triggers = { local triggers = {
@ -19,7 +20,8 @@ local action = function(msg)
local from = input:match('(%a%a%a) TO') local from = input:match('(%a%a%a) TO')
local to = input:match('TO (%a%a%a)') 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 result = 1
local url = 'https://www.google.com/finance/converter' local url = 'https://www.google.com/finance/converter'
@ -39,12 +41,12 @@ local action = function(msg)
return return
end end
result = str:format('%.2f') result = string.format('%.2f', str)
end end
local output = amount .. ' ' .. from .. ' = ' .. result .. ' ' .. to .. '\n' local output = amount .. ' ' .. from .. ' = ' .. result .. ' ' .. to .. '\n\n'
output = output .. os.date('!%F %T UTC') output = output .. os.date('!%F %T UTC') .. '\nSource: Google Finance'
output = '`' .. output .. '`' output = '`' .. output .. '`'
sendMessage(msg.chat.id, output, true, nil, true) sendMessage(msg.chat.id, output, true, nil, true)

View File

@ -54,7 +54,7 @@ local action = function(msg)
return return
end end
local output = '*Google: Results for* _' .. input .. '_ *:*\n' local output = '*Google results for* _' .. input .. '_ *:*\n'
for i,v in ipairs(jdat.responseData.results) do for i,v in ipairs(jdat.responseData.results) do
local title = jdat.responseData.results[i].titleNoFormatting:gsub('%[.+%]', ''):gsub('&amp;', '&') local title = jdat.responseData.results[i].titleNoFormatting:gsub('%[.+%]', ''):gsub('&amp;', '&')
if title:len() > 48 then if title:len() > 48 then

View File

@ -9,7 +9,8 @@ for i,v in ipairs(plugins) do
end end
end end
help_text = help_text .. [[\n help_text = help_text .. [[
/help <command> /help <command>
Arguments: <required> \[optional] Arguments: <required> \[optional]
]] ]]

View File

@ -15,10 +15,12 @@ local action = function(msg)
end end
local output = loadstring(input)() local output = loadstring(input)()
if not output then if output == nil then
output = 'Done!' output = 'Done!'
elseif type(output) == 'table' then
output = 'Done! Table returned.'
else else
output = '```\n' .. output .. '\n```' output = '```\n' .. tostring(output) .. '\n```'
end end
sendMessage(msg.chat.id, output, true, msg.message_id, true) sendMessage(msg.chat.id, output, true, msg.message_id, true)

View File

@ -10,9 +10,7 @@ local commands = {
['^/modhelp[@'..bot.username..']*$'] = function(msg) ['^/modhelp[@'..bot.username..']*$'] = function(msg)
if not moddat[msg.chat.id_str] then if not moddat[msg.chat.id_str] then return end
return config.errors.moderation
end
local message = [[ local message = [[
/modlist - List the moderators and administrators of this group. /modlist - List the moderators and administrators of this group.
@ -33,9 +31,7 @@ local commands = {
['^/modlist[@'..bot.username..']*$'] = function(msg) ['^/modlist[@'..bot.username..']*$'] = function(msg)
if not moddat[msg.chat.id_str] then if not moddat[msg.chat.id_str] then return end
return config.errors.moderation
end
local message = '' local message = ''
@ -113,9 +109,7 @@ local commands = {
['^/modprom[@'..bot.username..']*$'] = function(msg) ['^/modprom[@'..bot.username..']*$'] = function(msg)
if not moddat[msg.chat.id_str] then if not moddat[msg.chat.id_str] then return end
return config.errors.moderation
end
if not config.moderation.admins[msg.from.id_str] then if not config.moderation.admins[msg.from.id_str] then
return config.errors.not_admin return config.errors.not_admin
@ -145,9 +139,7 @@ local commands = {
['^/moddem[@'..bot.username..']*'] = function(msg) ['^/moddem[@'..bot.username..']*'] = function(msg)
if not moddat[msg.chat.id_str] then if not moddat[msg.chat.id_str] then return end
return config.errors.moderation
end
if not config.moderation.admins[msg.from.id_str] then if not config.moderation.admins[msg.from.id_str] then
return config.errors.not_admin return config.errors.not_admin
@ -181,9 +173,7 @@ local commands = {
['/modkick[@'..bot.username..']*'] = function(msg) ['/modkick[@'..bot.username..']*'] = function(msg)
if not moddat[msg.chat.id_str] then if not moddat[msg.chat.id_str] then return end
return config.errors.moderation
end
if not moddat[msg.chat.id_str][msg.from.id_str] then if not moddat[msg.chat.id_str][msg.from.id_str] then
if not config.moderation.admins[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) ['^/modban[@'..bot.username..']*'] = function(msg)
if not moddat[msg.chat.id_str] then if not moddat[msg.chat.id_str] then return end
return config.errors.moderation
end
if not moddat[msg.chat.id_str][msg.from.id_str] then if not moddat[msg.chat.id_str][msg.from.id_str] then
if not config.moderation.admins[msg.from.id_str] then if not config.moderation.admins[msg.from.id_str] then

View File

@ -30,7 +30,7 @@ local action = function(msg)
source = '*/r/' .. input:match('^r/(.+)') .. '*\n' source = '*/r/' .. input:match('^r/(.+)') .. '*\n'
else else
url = 'http://www.reddit.com/search.json?q=' .. input .. '&limit=' .. limit url = 'http://www.reddit.com/search.json?q=' .. input .. '&limit=' .. limit
source = '*reddit: Results for* _' .. input .. '_ *:*\n' source = '*reddit results for* _' .. input .. '_ *:*\n'
end end
else else
url = 'http://www.reddit.com/.json?limit=' .. limit url = 'http://www.reddit.com/.json?limit=' .. limit

View File

@ -1,7 +1,8 @@
-- utilities.lua -- utilities.lua
-- Functions shared among plugins. -- 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 '' s = s or ''
i = i or 1 i = i or 1
@ -15,7 +16,8 @@ function get_word(s, i) -- get the indexed word in a string
end 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 if not self:find(' ') then
return false return false
end 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 for k,v in pairs(lc_list) do
str = string.gsub(str, k, v) str = string.gsub(str, k, v)
end end
return str return str
end 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) local f = io.open(filename)
if not f then if not f then
@ -78,7 +82,8 @@ function load_data(filename) -- Loads a JSON file as a table.
end 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 s = JSON.encode(data)
local f = io.open(filename, 'w') local f = io.open(filename, 'w')
@ -88,7 +93,7 @@ function save_data(filename, data) -- Saves a table to a JSON file.
end end
-- Gets coordinates for a location. Used by gMaps.lua, time.lua, weather.lua. -- 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) 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 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 end