patterns.lua - Bugfix; was gsub-ing twice.

This commit is contained in:
topkecleon 2016-05-13 13:22:10 -04:00
parent 24892a9537
commit 7df8992abe
2 changed files with 18 additions and 41 deletions

View File

@ -1,6 +1,3 @@
-- Shout-out to Kenny, as I didn't want to write this until
-- he upset himself over the very thought of me doing so.
local patterns = {} local patterns = {}
local bindings = require('bindings') local bindings = require('bindings')
@ -25,7 +22,6 @@ function patterns:action(msg)
bindings.sendReply(self, msg, output) bindings.sendReply(self, msg, output)
return return
end end
output = output:gsub(m1, m2)
output = 'Did you mean:\n"' .. output:sub(1, 4000) .. '"' output = 'Did you mean:\n"' .. output:sub(1, 4000) .. '"'
bindings.sendReply(self, msg.reply_to_message, output) bindings.sendReply(self, msg.reply_to_message, output)

View File

@ -12,17 +12,13 @@ local bindings = require('bindings')
-- get the indexed word in a string -- get the indexed word in a string
function utilities.get_word(s, i) function utilities.get_word(s, i)
s = s or '' s = s or ''
i = i or 1 i = i or 1
local t = {} local t = {}
for w in s:gmatch('%g+') do for w in s:gmatch('%g+') do
table.insert(t, w) table.insert(t, w)
end end
return t[i] or false return t[i] or false
end end
-- Like get_word(), but better. -- Like get_word(), but better.
@ -130,16 +126,15 @@ end
-- Get the number of values in a key/value table. -- Get the number of values in a key/value table.
function utilities.table_size(tab) function utilities.table_size(tab)
local i = 0 local i = 0
for _,_ in pairs(tab) do for _,_ in pairs(tab) do
i = i + 1 i = i + 1
end end
return i return i
end end
-- Just an easy way to get a user's full name. -- Just an easy way to get a user's full name.
-- Alternatively, abuse it to concat two strings like I do.
function utilities.build_name(first, last) function utilities.build_name(first, last)
if last then if last then
return first .. ' ' .. last return first .. ' ' .. last
@ -215,51 +210,37 @@ function utilities:handle_exception(err, message)
end end
-- Okay, this one I actually did copy from yagop.
-- https://github.com/yagop/telegram-bot/blob/master/bot/utils.lua
function utilities.download_file(url, filename) function utilities.download_file(url, filename)
if not filename then
local respbody = {} filename = url:match('.+/(.-)$') or os.time()
local options = { filename = '/tmp/' .. filename
url = url,
sink = ltn12.sink.table(respbody),
redirect = true
}
local response
if url:match('^https') then
options.redirect = false
response = { HTTPS.request(options) }
else
response = { HTTP.request(options) }
end end
local body = {}
local code = response[2] local doer = HTTP
local headers = response[3] local do_redir = true
local status = response[4] if url:match('^https') then
doer = HTTPS
if code ~= 200 then return false end do_redir = false
end
filename = filename or '/tmp/' .. os.time() local _, res = doer.request{
url = url,
sink = ltn12.sink.table(body),
redirect = do_redir
}
if res ~= 200 then return false end
local file = io.open(filename, 'w+') local file = io.open(filename, 'w+')
file:write(table.concat(respbody)) file:write(table.concat(body))
file:close() file:close()
return filename return filename
end end
function utilities.markdown_escape(text) function utilities.markdown_escape(text)
text = text:gsub('_', '\\_') text = text:gsub('_', '\\_')
text = text:gsub('%[', '\\[') text = text:gsub('%[', '\\[')
text = text:gsub('%]', '\\]') text = text:gsub('%]', '\\]')
text = text:gsub('%*', '\\*') text = text:gsub('%*', '\\*')
text = text:gsub('`', '\\`') text = text:gsub('`', '\\`')
return text return text
end end
utilities.md_escape = utilities.markdown_escape utilities.md_escape = utilities.markdown_escape