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')
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

View File

@ -8,6 +8,7 @@ return {
thecatapi_key = '',
time_offset = 0,
lang = 'en',
antisquig = false,
cli_port = 4567,
admin = 00000000,
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'
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

View File

@ -3,6 +3,7 @@ local doc = [[```
/cash [amount] <from> to <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)

View File

@ -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('&amp;', '&')
if title:len() > 48 then

View File

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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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