From c74d1913a448c370a809a63b9cdd68a6026f27a2 Mon Sep 17 00:00:00 2001 From: topkecleon Date: Sat, 2 Apr 2016 19:20:28 -0400 Subject: [PATCH] New method to escape markdown. Improvements to patterns.lua. --- bot.lua | 5 +---- plugins/patterns.lua | 8 ++++---- plugins/whoami.lua | 7 +------ utilities.lua | 25 +++++++++++-------------- 4 files changed, 17 insertions(+), 28 deletions(-) diff --git a/bot.lua b/bot.lua index a276d25..46faecf 100755 --- a/bot.lua +++ b/bot.lua @@ -84,10 +84,7 @@ on_msg_receive = function(msg) -- The fn run whenever a message is received. msg.chat.id_str = tostring(msg.chat.id) msg.from.id_str = tostring(msg.from.id) msg.text_lower = msg.text:lower() - msg.from.name = msg.from.first_name - if msg.from.last_name then - msg.from.name = msg.from.first_name .. ' ' .. msg.from.last_name - end + msg.from.name = build_name(msg.from.first_name, msg.from.last_name) local success, result = pcall(function() return v.action(msg) diff --git a/plugins/patterns.lua b/plugins/patterns.lua index d6f3de8..74c024c 100644 --- a/plugins/patterns.lua +++ b/plugins/patterns.lua @@ -2,16 +2,16 @@ -- he upset himself over the very thought of me doing so. local triggers = { - '^/s/.-/.-/?$' + '^/?s/.-/.-/?$' } local action = function(msg) if not msg.reply_to_message then return end local output = msg.reply_to_message.text or '' - output = output:gsub( - msg.text:match('^/s/(.-)/(.-)/?$') - ) + local m1, m2 = msg.text:match('^/?s/(.-)/(.-)/?$') + if not m2 then return true end + output = output:gsub(m1, m2) output = 'Did you mean:\n"' .. output:sub(1, 4000) .. '"' sendReply(msg.reply_to_message, output) diff --git a/plugins/whoami.lua b/plugins/whoami.lua index af2ed48..c7fc247 100755 --- a/plugins/whoami.lua +++ b/plugins/whoami.lua @@ -12,12 +12,7 @@ local action = function(msg) if msg.reply_to_message then msg = msg.reply_to_message - end - - if msg.from.last_name then - msg.from.name = msg.from.first_name .. ' ' .. msg.from.last_name - else - msg.from.name = msg.from.first_name + msg.from.name = build_name(msg.from.first_name, msg.from.last_name) end local chat_id = math.abs(msg.chat.id) diff --git a/utilities.lua b/utilities.lua index 758112e..2e5fe94 100755 --- a/utilities.lua +++ b/utilities.lua @@ -77,7 +77,7 @@ local lc_list = { -- Replaces letters with corresponding Cyrillic characters. latcyr = function(str) for k,v in pairs(lc_list) do - str = string.gsub(str, k, v) + str = str:gsub(k, v) end return str end @@ -140,6 +140,15 @@ table_size = function(tab) end + -- Just an easy way to get a user's full name. +build_name = function(first, last) + if last then + return first .. ' ' .. last + else + return first + end +end + resolve_username = function(input) input = input:gsub('^@', '') @@ -186,10 +195,7 @@ user_from_message = function(msg) if not target.first_name then target.first_name = 'User' end - target.name = target.first_name - if target.last_name then - target.name = target.first_name .. ' ' .. target.last_name - end + target.name = build_name(target.first_name, target.last_name) return target @@ -264,12 +270,3 @@ function string:md_escape() text = text:gsub('`', '\\`') return text end - - -- Just an easy way to get a user's full name. -build_name = function(first, last) - if last then - return first .. ' ' .. last - else - return first - end -end