diff --git a/README.md b/README.md index 82a3885..5d5312f 100755 --- a/README.md +++ b/README.md @@ -119,13 +119,14 @@ For support for otouto and bots in general, join my Bot Development group. Follo ##Development Everybody is free to contribute to otouto. Here I will explain various things that are important to know about the plugin system. -A plugin can have four components, and two of them are optional: action, triggers, doc, cron. +A plugin can have five components, and three of them are optional: action, triggers, doc, command, and cron. | Component | Description | Optional? | |-----------|-------------|-----------| | action | The main function of a plugin. It accepts the `msg` table. | No. | | triggers | A table of strings which, when one is matched in a message's text, will cause `action` to be run. | No. | -| doc | The help text to be returned when a plugin is run with improper syntax or arguments. The first line is also what goes in the help text. | Yes | +| doc | The help text to be returned when a plugin is run with improper syntax or arguments. | Yes | +| command | The command with its syntax, without the slash. This is used to generate the help text. | Yes | | cron | A function to be run every five seconds. | Yes | The on_msg_receive function adds a few variables to the "msg" table: msg.from.id_str, msg.to.id_str, msg.text_lower. These are self-explanatory and can make your code a lot neater. diff --git a/plugins/about.lua b/plugins/about.lua index 2c1cbcd..f869efe 100755 --- a/plugins/about.lua +++ b/plugins/about.lua @@ -1,7 +1,5 @@ -local doc = [[ - /about - Get info about the bot. -]] +local command = 'about' +local doc = '`Returns information about the bot.`' local triggers = { '' @@ -26,5 +24,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/bandersnatch.lua b/plugins/bandersnatch.lua index 8c9d9d4..5950ebe 100755 --- a/plugins/bandersnatch.lua +++ b/plugins/bandersnatch.lua @@ -1,7 +1,8 @@ -local doc = [[ - /bandersnatch - Shun the frumious Bandersnatch. -]] +local command = 'bandersnatch' +local doc = [[``` +Shun the frumious Bandersnatch. +Alias: /bc +```]] local triggers = { '^/bandersnatch[@'..bot.username..']*', @@ -31,5 +32,6 @@ end return { action = action, triggers = triggers, - doc = doc + command = command, + desc = desc } diff --git a/plugins/bible.lua b/plugins/bible.lua index f981d76..297e766 100755 --- a/plugins/bible.lua +++ b/plugins/bible.lua @@ -4,10 +4,12 @@ if not config.biblia_api_key then return end -local doc = [[ - /bible - Returns a verse from the American Standard Version of the Bible, or an apocryphal verse from the King James Version. Results from biblia.com. -]] +local command = 'bible ' +local doc = [[``` +/bible +Returns a verse from the American Standard Version of the Bible, or an apocryphal verse from the King James Version. Results from biblia.com. +Alias: /b +```]] local triggers = { '^/bible*[@'..bot.username..']*', @@ -19,7 +21,7 @@ local action = function(msg) local input = msg.text:input() if not input then - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end @@ -47,5 +49,6 @@ end return { action = action, triggers = triggers, + command = command, doc = doc } diff --git a/plugins/calc.lua b/plugins/calc.lua index 3dd904e..0d854d1 100755 --- a/plugins/calc.lua +++ b/plugins/calc.lua @@ -1,7 +1,8 @@ -local doc = [[ - /calc - Returns solutions to mathematical expressions and conversions between common units. Results provided by mathjs.org. -]] +local command = 'calc ' +local doc = [[``` +/calc +Returns solutions to mathematical expressions and conversions between common units. Results provided by mathjs.org. +```]] local triggers = { '^/calc[@'..bot.username..']*' @@ -14,25 +15,28 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end local url = 'https://api.mathjs.org/v1/?expr=' .. URL.escape(input) - local ans, res = HTTPS.request(url) - if not ans then + local output = HTTPS.request(url) + if not output then sendReply(msg, config.errors.connection) return end - sendReply(msg, ans) + output = '`' .. output .. '`' + + sendMessage(msg.chat.id, output, true, msg.message_id, true) end return { action = action, triggers = triggers, + command = command, doc = doc } diff --git a/plugins/cats.lua b/plugins/cats.lua index c60b5ab..d0b6fb8 100755 --- a/plugins/cats.lua +++ b/plugins/cats.lua @@ -3,10 +3,8 @@ if not config.thecatapi_key then print('cats.lua will be enabled, but there are more features with a key.') end -local doc = [[ - /cat - Returns a cat! -]] +local command = 'cat' +local doc = '`Returns a cat!`' local triggers = { '^/cat[@'..bot.username..']*$' @@ -34,5 +32,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/commit.lua b/plugins/commit.lua index 96332c4..0f4a342 100755 --- a/plugins/commit.lua +++ b/plugins/commit.lua @@ -1,9 +1,7 @@ -- Commits from https://github.com/ngerakines/commitment. -local doc = [[ - /commit - Returns a commit message from whatthecommit.com. -]] +local command = 'commit' +local doc = '`Returns a commit message from whatthecommit.com.`' local triggers = { '^/commit[@'..bot.username..']*' @@ -414,12 +412,13 @@ local commits = { local action = function(msg) - sendMessage(msg.chat.id, commits[math.random(#commits)]) + sendMessage(msg.chat.id, '`'..commits[math.random(#commits)]..'`', true, nil, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/currency.lua b/plugins/currency.lua index 2702957..21ac676 100755 --- a/plugins/currency.lua +++ b/plugins/currency.lua @@ -1,8 +1,9 @@ -local doc = [[ - /cash [amount] to - Example: /cash 5 USD to EUR - Returns exchange rates for various currencies. -]] +local command = 'cash [amount] to ' +local doc = [[``` +/cash [amount] to +Example: /cash 5 USD to EUR +Returns exchange rates for various currencies. +```]] local triggers = { '^/cash[@'..bot.username..']*' @@ -12,7 +13,7 @@ local action = function(msg) local input = msg.text:upper() if not input:match('%a%a%a TO %a%a%a') then - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end @@ -42,13 +43,17 @@ local action = function(msg) end - local message = amount .. ' ' .. from .. ' = ' .. result .. ' ' .. to - sendReply(msg, message) + local output = amount .. ' ' .. from .. ' = ' .. result .. ' ' .. to .. '\n' + output = output .. os.date('!%F %T UTC') + output = '`' .. output .. '`' + + sendMessage(msg.chat.id, output, true, nil, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/dice.lua b/plugins/dice.lua index e40ec6e..7ae3345 100755 --- a/plugins/dice.lua +++ b/plugins/dice.lua @@ -1,7 +1,8 @@ -local doc = [[ - /roll - Returns a set of dice rolls, where n is the number of rolls and r is the range. If only a range is given, returns only one roll. -]] +local command = 'roll ' +local doc = [[``` +/roll +Returns a set of dice rolls, where n is the number of rolls and r is the range. If only a range is given, returns only one roll. +```]] local triggers = { '^/roll[@'..bot.username..']*' @@ -11,7 +12,7 @@ local action = function(msg) local input = msg.text_lower:input() if not input then - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end @@ -22,7 +23,7 @@ local action = function(msg) count = 1 range = input:match('^d?([%d]+)$') else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end @@ -38,17 +39,19 @@ local action = function(msg) return end - local message = '' + local output = '*' .. count .. 'd' .. range .. '*\n`' for i = 1, count do - message = message .. math.random(range) .. '\t' + output = output .. math.random(range) .. '\t' end + output = output .. '`' - sendReply(msg, message) + sendMessage(msg.chat.id, output, true, msg.message_id, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/echo.lua b/plugins/echo.lua index 5db8dc6..2808a34 100755 --- a/plugins/echo.lua +++ b/plugins/echo.lua @@ -1,7 +1,8 @@ -local doc = [[ - /echo - Repeat a string of text! -]] +local command = 'echo ' +local doc = [[``` +/echo +Repeats a string of text. +```]] local triggers = { '^/echo[@'..bot.username..']*' @@ -14,7 +15,7 @@ local action = function(msg) if input then sendMessage(msg.chat.id, latcyr(input)) else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) end end @@ -22,5 +23,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/eightball.lua b/plugins/eightball.lua index a8095b3..54b8fb1 100755 --- a/plugins/eightball.lua +++ b/plugins/eightball.lua @@ -1,7 +1,5 @@ -local doc = [[ - /8ball - Returns an answer from a magic 8-ball! -]] +local command = '8ball' +local doc = '`Returns an answer from a magic 8-ball!`' local triggers = { '^/8ball', @@ -60,5 +58,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/fortune.lua b/plugins/fortune.lua index 68aefbd..95c4f56 100755 --- a/plugins/fortune.lua +++ b/plugins/fortune.lua @@ -7,10 +7,8 @@ if s:match('fortune: command not found') then return end -local doc = [[ - /fortune - Returns a UNIX fortune. -]] +local command = 'fortune' +local doc = '`Returns a UNIX fortune.`' local triggers = { '^/fortune[@'..bot.username..']*' @@ -26,5 +24,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/gImages.lua b/plugins/gImages.lua index a1900a8..e1f98c2 100755 --- a/plugins/gImages.lua +++ b/plugins/gImages.lua @@ -11,10 +11,12 @@ elseif not config.google_cse_key then return end -local doc = [[ - /image - Returns a randomized top result from Google Images. Safe search is enabled by default; use "/insfw" to disable it. NSFW results will not display an image preview. -]] +local command = 'image ' +local doc = [[``` +/image +Returns a randomized top result from Google Images. Safe search is enabled by default; use "/insfw" to disable it. NSFW results will not display an image preview. +Alias: /i +```]] local triggers = { '^/image[@'..bot.username..']*', @@ -30,7 +32,7 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end @@ -69,5 +71,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/gMaps.lua b/plugins/gMaps.lua index 5a049b2..e087c40 100755 --- a/plugins/gMaps.lua +++ b/plugins/gMaps.lua @@ -1,7 +1,9 @@ -local doc = [[ - /location - Returns a location from Google Maps. -]] +local command = 'location ' +local doc = [[``` +/location +Returns a location from Google Maps. +Alias: /loc +```]] triggers = { '^/location[@'..bot.username..']*', @@ -16,7 +18,7 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end @@ -34,5 +36,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/gSearch.lua b/plugins/gSearch.lua index a246eb6..eef673a 100755 --- a/plugins/gSearch.lua +++ b/plugins/gSearch.lua @@ -1,7 +1,9 @@ -local doc = [[ - /google - Returns four (if group) or eight (if private message) results from Google. Safe search is enabled by default, use "/gnsfw" to disable it. -]] +local command = 'google ' +local doc = [[``` +/google +Returns four (if group) or eight (if private message) results from Google. Safe search is enabled by default, use "/gnsfw" to disable it. +Alias: /g +```]] local triggers = { '^/g[@'..bot.username..']*$', @@ -17,7 +19,7 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end @@ -43,22 +45,36 @@ local action = function(msg) end local jdat = JSON.decode(jstr) - if #jdat.responseData.results < 1 then + if not jdat.responseData then + sendReply(msg, config.errors.connection) + return + end + if not jdat.responseData.results[1] then sendReply(msg, config.errors.results) return end - local message = '' + local output = '*Google: Results for* _' .. input .. '_ *:*\n' for i,v in ipairs(jdat.responseData.results) do - message = message .. jdat.responseData.results[i].titleNoFormatting .. '\n ' .. jdat.responseData.results[i].unescapedUrl .. '\n' + local title = jdat.responseData.results[i].titleNoFormatting:gsub('%[.+%]', ''):gsub('&', '&') + if title:len() > 48 then + title = title:sub(1, 45) .. '...' + end + local url = jdat.responseData.results[i].unescapedUrl + if url:find('%)') then + output = output .. '• ' .. title .. '\n' .. url:gsub('_', '\\_') .. '\n' + else + output = output .. '• [' .. title .. '](' .. url .. ')\n' + end end - sendReply(msg, message) + sendMessage(msg.chat.id, output, true, nil, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/hackernews.lua b/plugins/hackernews.lua index 97654d6..03afea6 100755 --- a/plugins/hackernews.lua +++ b/plugins/hackernews.lua @@ -1,7 +1,8 @@ -local doc = [[ - /hackernews - Returns four (if group) or eight (if private message) top stories from Hacker News. -]] +local command = 'hackernews' +local doc = [[``` +Returns four (if group) or eight (if private message) top stories from Hacker News. +Alias: /hn +```]] local triggers = { '^/hackernews[@'..bot.username..']*', @@ -25,7 +26,7 @@ local action = function(msg) res_count = 8 end - local message = '' + local output = '*Hacker News:*\n' for i = 1, res_count do local res_url = 'https://hacker-news.firebaseio.com/v0/item/' .. jdat[i] .. '.json' jstr, res = HTTPS.request(res_url) @@ -34,15 +35,26 @@ local action = function(msg) return end local res_jdat = JSON.decode(jstr) - message = message .. res_jdat.title .. '\n ' .. res_jdat.url .. '\n' + local title = res_jdat.title:gsub('%[.+%]', ''):gsub('%(.+%)', ''):gsub('&', '&') + if title:len() > 48 then + title = title:sub(1, 45) .. '...' + end + local url = res_jdat.url + if url:find('%(') then + output = output .. '• ' .. title .. '\n' .. url:gsub('_', '\\_') .. '\n' + else + output = output .. '• [' .. title .. '](' .. url .. ')\n' + end + end - sendReply(msg, message) + sendMessage(msg.chat.id, output, true, nil, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/hearthstone.lua b/plugins/hearthstone.lua index aa1ec80..5696067 100755 --- a/plugins/hearthstone.lua +++ b/plugins/hearthstone.lua @@ -19,10 +19,12 @@ if not hs_dat then end -local doc = [[ - /hearthstone - Returns Hearthstone card info. -]] +local command = 'hearthstone ' +local doc = [[``` +/hearthstone +Returns Hearthstone card info. +Alias: /hn +```]] local triggers = { '^/hearthstone[@'..bot.username..']*', @@ -65,7 +67,7 @@ local format_card = function(card) if card.text then info = card.text:gsub('',''):gsub('%$','') if card.flavor then - info = info .. '\n' .. card.flavor + info = info .. '\n_' .. card.flavor .. '_' end elseif card.flavor then info = card.flavor @@ -73,7 +75,7 @@ local format_card = function(card) info = nil end - local s = card.name .. '\n' .. ctype + local s = '*' .. card.name .. '*\n' .. ctype if stats then s = s .. '\n' .. stats end @@ -89,7 +91,7 @@ local action = function(msg) local input = msg.text_lower:input() if not input then - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end @@ -106,12 +108,13 @@ local action = function(msg) return end - sendReply(msg, output) + sendMessage(msg.chat.id, output, true, msg.message_id, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/help.lua b/plugins/help.lua index 040d417..997cd67 100755 --- a/plugins/help.lua +++ b/plugins/help.lua @@ -1,16 +1,18 @@ -- This plugin should go at the end of your plugin list in -- config.lua, but not after greetings.lua. -local help_text = 'Available commands:\n' +local help_text = '*Available commands:*' for i,v in ipairs(plugins) do - if v.doc then - local a = string.sub(v.doc, 1, string.find(v.doc, '\n')-1) - help_text = help_text .. a .. '\n' + if v.command then + help_text = help_text .. '\n /' .. v.command:gsub('%[', '\\[') end end -local help_text = help_text .. 'Arguments: [optional]' +help_text = help_text .. [[\n + /help +Arguments: \[optional] +]] local triggers = { '^/help[@'..bot.username..']*', @@ -20,16 +22,30 @@ local triggers = { local action = function(msg) - if msg.from.id ~= msg.chat.id then - if sendMessage(msg.from.id, help_text) then + local input = msg.text_lower:input() + + -- Attempts to send the help message via PM. + -- If msg is from a group, it tells the group whether the PM was successful. + if not input then + local res = sendMessage(msg.from.id, help_text, true, nil, true) + if not res then + sendReply(msg, 'Please message me privately for a list of commands.') + elseif msg.chat.type ~= 'private' then sendReply(msg, 'I have sent you the requested information in a private message.') - else - sendReply(msg, help_text) end - else - sendReply(msg, help_text) + return end + for i,v in ipairs(plugins) do + if v.command and get_word(v.command, 1) == input and v.doc then + local output = '*Help for* _' .. get_word(v.command, 1) .. '_ *:*\n' .. v.doc + sendMessage(msg.chat.id, output, true, nil, true) + return + end + end + + sendReply(msg, 'Sorry, there is no help for that command.') + end return { diff --git a/plugins/imdb.lua b/plugins/imdb.lua index eed77ef..45cb543 100755 --- a/plugins/imdb.lua +++ b/plugins/imdb.lua @@ -1,7 +1,8 @@ -local doc = [[ - /imdb - Returns an IMDb entry. -]] +local command = 'imdb ' +local doc = [[``` +/imdb +Returns an IMDb entry. +```]] local triggers = { '^/imdb[@'..bot.username..']*' @@ -14,7 +15,7 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end @@ -34,17 +35,18 @@ local action = function(msg) return end - local message = jdat.Title ..' ('.. jdat.Year ..')\n' - message = message .. jdat.imdbRating ..' | '.. jdat.Runtime ..' | '.. jdat.Genre ..'\n' - message = message .. jdat.Plot .. '\n' - message = message .. 'http://imdb.com/title/' .. jdat.imdbID + local output = '[' .. jdat.Title .. '](http://imdb.com/title/' + output = output .. jdat.imdbID .. ') ('.. jdat.Year ..')\n' + output = output .. jdat.imdbRating ..'/10 | '.. jdat.Runtime ..' | '.. jdat.Genre ..'\n' + output = output .. jdat.Plot - sendReply(msg, message) + sendMessage(msg.chat.id, output, true, nil, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/lastfm.lua b/plugins/lastfm.lua index 22d05e7..a6f1b2d 100755 --- a/plugins/lastfm.lua +++ b/plugins/lastfm.lua @@ -4,13 +4,14 @@ if not config.lastfm_api_key then return end -local doc = [[ - /lastfm - /np [username] - Returns what you are or were last listening to. If you specify a username, info will be returned for that username. - /fmset - Sets your last.fm username. Otherwise, /np will use your Telegram username. Use "/fmset -" to delete it. -]] +local command = 'lastfm' +local doc = [[``` +/np [username] +Returns what you are or were last listening to. If you specify a username, info will be returned for that username. + +/fmset +Sets your last.fm username. Otherwise, /np will use your Telegram username. Use "/fmset -" to delete it. +```]] local triggers = { '^/lastfm[@'..bot.username..']*', @@ -28,7 +29,7 @@ local action = function(msg) return elseif string.match(msg.text, '^/fmset') then if not input then - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) elseif input == '-' then lastfm[msg.from.id_str] = nil sendReply(msg, 'Your last.fm username has been forgotten.') @@ -101,5 +102,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/nick.lua b/plugins/nick.lua index 4369663..90bac1e 100755 --- a/plugins/nick.lua +++ b/plugins/nick.lua @@ -1,7 +1,8 @@ -local doc = [[ - /nick - Set your nickname. Use "/whoami" to check your nickname and "/nick -" to delete it. -]] +local command = 'nick ' +local doc = [[``` +/nick +Set your nickname. Use "/whoami" to check your nickname and "/nick -" to delete it. +```]] local triggers = { '^/nick[@'..bot.username..']*' @@ -11,7 +12,7 @@ local action = function(msg) local input = msg.text:input() if not input then - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return true end @@ -38,5 +39,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/pokedex.lua b/plugins/pokedex.lua index bed2c37..2a7e57a 100755 --- a/plugins/pokedex.lua +++ b/plugins/pokedex.lua @@ -1,7 +1,9 @@ -local doc = [[ - /pokedex - Returns a Pokedex entry from pokeapi.co. -]] +local command = 'pokedex ' +local doc = [[``` +/pokedex +Returns a Pokedex entry from pokeapi.co. +Alias: /dex +```]] local triggers = { '^/pokedex[@'..bot.username..']*', @@ -15,7 +17,7 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end @@ -51,14 +53,16 @@ local action = function(msg) end poke_type = poke_type .. ' type' - local message = dex_jdat.name .. ' #' .. dex_jdat.national_id .. '\n' .. poke_type .. '\nHeight: ' .. dex_jdat.height/10 .. 'm, Weight: ' .. dex_jdat.weight/10 .. 'kg\n' .. desc_jdat.description:gsub('POKMON', 'POKeMON') + local output = '*' .. dex_jdat.name .. '*\n#' .. dex_jdat.national_id .. ' | ' .. poke_type .. '\n_' .. desc_jdat.description:gsub('POKMON', 'Pokémon'):gsub('Pokmon', 'Pokémon') .. '_' - sendReply(msg, message) + + sendMessage(msg.chat.id, output, true, nil, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/pun.lua b/plugins/pun.lua index 1a653f6..715205f 100755 --- a/plugins/pun.lua +++ b/plugins/pun.lua @@ -1,7 +1,5 @@ -local doc = [[ - /pun - Returns a pun. -]] +local command = 'pun' +local doc = '`Returns a pun.`' local triggers = { '^/pun[@'..bot.username..']*' @@ -140,5 +138,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/reactions.lua b/plugins/reactions.lua index ec90c98..e236375 100755 --- a/plugins/reactions.lua +++ b/plugins/reactions.lua @@ -1,7 +1,5 @@ -local doc = [[ - /reactions - Returns a list of "reaction" emoticon commands. -]] +local command = 'reactions' +local doc = '`Returns a list of "reaction" emoticon commands.`' local triggers = { ['¯\\_(ツ)_/¯'] = '/shrug$', @@ -33,5 +31,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/reddit.lua b/plugins/reddit.lua index df72e1e..713e030 100755 --- a/plugins/reddit.lua +++ b/plugins/reddit.lua @@ -1,7 +1,9 @@ -local doc = [[ - /reddit [r/subreddit | query] - Returns the four (if group) or eight (if private message) top posts for the given subreddit or query, or from the frontpage. -]] +local command = 'reddit [r/subreddit | query]' +local doc = [[``` +/reddit [r/subreddit | query] +Returns the four (if group) or eight (if private message) top posts for the given subreddit or query, or from the frontpage. +Aliases: /r, /r/[subreddit] +```]] local triggers = { '^/reddit[@'..bot.username..']*', @@ -21,14 +23,18 @@ local action = function(msg) limit = 8 end + local source if input then - if input:match('^r/') then + if input:match('^r/.') then url = 'http://www.reddit.com/' .. input .. '/.json?limit=' .. limit + source = '*/r/' .. input:match('^r/(.+)') .. '*\n' else url = 'http://www.reddit.com/search.json?q=' .. input .. '&limit=' .. limit + source = '*reddit: Results for* _' .. input .. '_ *:*\n' end else url = 'http://www.reddit.com/.json?limit=' .. limit + source = '*/r/all*\n' end local jstr, res = HTTP.request(url) @@ -43,25 +49,31 @@ local action = function(msg) return end - local message = '' + local output = '' for i,v in ipairs(jdat.data.children) do + local title = v.data.title:gsub('%[.+%]', ''):gsub('&', '&') + if title:len() > 48 then + title = title:sub(1,45) .. '...' + end if v.data.over_18 then - message = message .. '[NSFW] ' + v.data.is_self = true end - local long_url = '\n' + local short_url = 'redd.it/' .. v.data.id + output = output .. '• [' .. title .. '](' .. short_url .. ')\n' if not v.data.is_self then - long_url = '\n' .. v.data.url .. '\n' + output = output .. v.data.url:gsub('_', '\\_') .. '\n' end - local short_url = '[redd.it/' .. v.data.id .. '] ' - message = message .. short_url .. v.data.title .. long_url end - sendReply(msg, message) + output = source .. output + + sendMessage(msg.chat.id, output, true, nil, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/shout.lua b/plugins/shout.lua index 60bb8b8..09d8dfb 100644 --- a/plugins/shout.lua +++ b/plugins/shout.lua @@ -1,7 +1,8 @@ -local doc = [[ - /shout - Shout something! -]] +local command = 'shout ' +local doc = [[``` +/shout +Shouts something. +```]] local triggers = { '^/shout[@'..bot.username..']*' @@ -12,7 +13,7 @@ local action = function(msg) local input = msg.text:input() if not input then - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end input = input:trim() @@ -44,5 +45,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/slap.lua b/plugins/slap.lua index 2bd3dda..0bd2484 100755 --- a/plugins/slap.lua +++ b/plugins/slap.lua @@ -1,7 +1,8 @@ -local doc = [[ - /slap [target] - Give someone a good slap (or worse) through reply or specification of a target. -]] +local command = 'slap [target]' +local doc = [[``` +/slap [target] +Slap somebody. +```]] local triggers = { '^/slap[@'..bot.username..']*' @@ -125,5 +126,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/time.lua b/plugins/time.lua index 4978477..1accc2e 100755 --- a/plugins/time.lua +++ b/plugins/time.lua @@ -1,7 +1,8 @@ -local doc = [[ - /time - Returns the time, date, and timezone for the given location. -]] +local command = 'time ' +local doc = [[``` +/time +Returns the time, date, and timezone for the given location. +```]] local triggers = { '^/time[@'..bot.username..']*' @@ -14,7 +15,7 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end @@ -49,5 +50,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/translate.lua b/plugins/translate.lua index 2d46831..7da05b7 100755 --- a/plugins/translate.lua +++ b/plugins/translate.lua @@ -1,7 +1,8 @@ -local doc = [[ - /translate [text] - Translates input or the replied-to message into the bot's language. -]] +local command = 'translate [text]' +local doc = [[``` +/translate [text] +Translates input or the replied-to message into the bot's language. +```]] local triggers = { '^/translate[@'..bot.username..']*' @@ -14,7 +15,7 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end @@ -36,5 +37,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/urbandictionary.lua b/plugins/urbandictionary.lua index ee1879e..9cbef94 100755 --- a/plugins/urbandictionary.lua +++ b/plugins/urbandictionary.lua @@ -1,7 +1,9 @@ -local doc = [[ - /urbandictionary - Returns a definition from Urban Dictionary. -]] +local command = 'urbandictionary ' +local doc = [[``` +/urbandictionary +Returns a definition from Urban Dictionary. +Aliases: /ud, /urban +```]] local triggers = { '^/urbandictionary[@'..bot.username..']*', @@ -17,7 +19,7 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end @@ -36,17 +38,20 @@ local action = function(msg) return end - local message = '"' .. jdat.list[1].word .. '"\n' .. jdat.list[1].definition:trim() + local output = '*' .. jdat.list[1].word .. '*\n\n' .. jdat.list[1].definition:trim() if string.len(jdat.list[1].example) > 0 then - message = message .. '\n\nExample:\n' .. jdat.list[1].example:trim() + output = output .. '_\n\n' .. jdat.list[1].example:trim() .. '_' end - sendReply(msg, message) + output = output:gsub('%[', ''):gsub('%]', '') + + sendMessage(msg.chat.id, output, true, nil, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/weather.lua b/plugins/weather.lua index a8327e1..34ce5b9 100755 --- a/plugins/weather.lua +++ b/plugins/weather.lua @@ -4,10 +4,11 @@ if not config.owm_api_key then return end -local doc = [[ - /weather - Returns the current weather conditions for a given location. -]] +local command = 'weather ' +local doc = [[``` +/weather +Returns the current weather conditions for a given location. +```]] local triggers = { '^/weather[@'..bot.username..']*' @@ -20,7 +21,7 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end @@ -52,5 +53,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/whoami.lua b/plugins/whoami.lua index 38f31e3..7584c7a 100755 --- a/plugins/whoami.lua +++ b/plugins/whoami.lua @@ -1,3 +1,9 @@ +local command = 'whoami' +local doc = [[``` +Returns user and chat info for you or the replied-to message. +Alias: /who +```]] + local triggers = { '^/who[ami]*[@'..bot.username..']*$' } @@ -37,5 +43,7 @@ end return { action = action, - triggers = triggers + triggers = triggers, + doc = doc, + command = command } diff --git a/plugins/wikipedia.lua b/plugins/wikipedia.lua index bcbc986..23079a6 100755 --- a/plugins/wikipedia.lua +++ b/plugins/wikipedia.lua @@ -1,7 +1,9 @@ -local doc = [[ - /wikipedia - Returns an article from Wikipedia. -]] +local command = 'wikipedia ' +local doc = [[``` +/wikipedia +Returns an article from Wikipedia. +Aliases: /w, /wiki +```]] local triggers = { '^/wikipedia[@'..bot.username..']*', @@ -17,7 +19,7 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end @@ -32,6 +34,10 @@ local action = function(msg) end local jdat = JSON.decode(jstr) + if not jdat.responseData then + sendReply(msg, config.errors.connection) + return + end if not jdat.responseData.results[1] then sendReply(msg, config.errors.results) return @@ -61,14 +67,24 @@ local action = function(msg) if l then text = text:sub(1, l-1) end - text = text .. '\n' .. url - sendReply(msg, text) + title = title:gsub('%(.+%)', '') + --local output = '[' .. title .. '](' .. url .. ')\n' .. text:gsub('%[.+]%','') + --local output = '*' .. title .. '*\n' .. text:gsub('%[.+]%','') .. '\n[Read more.](' .. url .. ')' + local output = text:gsub('%[.+%]',''):gsub(title, '*'..title..'*') .. '\n' + if url:find('%(') then + output = output .. url:gsub('_', '\\_') + else + output = output .. '[Read more.](' .. url .. ')' + end + + sendMessage(msg.chat.id, output, true, nil, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua index c4d20ac..9e4ac97 100755 --- a/plugins/xkcd.lua +++ b/plugins/xkcd.lua @@ -1,7 +1,8 @@ -local doc = [[ - /xkcd [query] - Returns an xkcd strip and its alt text. If there is no query, it will be randomized. -]] +local command = 'xkcd [query]' +local doc = [[``` +/xkcd [query] +Returns an xkcd strip and its alt text. If there is no query, it will be randomized. +```]] local triggers = { '^/xkcd[@'..bot.username..']*' @@ -44,13 +45,15 @@ local action = function(msg) end local jdat = JSON.decode(jstr) - local message = '[' .. jdat.num .. '] ' .. jdat.alt .. '\n' .. jdat.img - sendMessage(msg.chat.id, message, false, msg.message_id) + local output = '[' .. jdat.num .. '](' .. jdat.img .. ')\n' .. jdat.alt + + sendMessage(msg.chat.id, output, false, nil, true) end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/plugins/youtube.lua b/plugins/youtube.lua index b87c86d..321ed82 100755 --- a/plugins/youtube.lua +++ b/plugins/youtube.lua @@ -1,9 +1,11 @@ -- Thanks to @TiagoDanin for writing the original plugin. -local doc = [[ - /youtube - Returns the top result from YouTube. -]] +local command = 'youtube ' +local doc = [[``` +/youtube +Returns the top result from YouTube. +Alias: /yt +```]] local triggers = { '^/youtube[@'..bot.username..']*', @@ -18,7 +20,7 @@ local action = function(msg) if msg.reply_to_message and msg.reply_to_message.text then input = msg.reply_to_message.text else - sendReply(msg, doc) + sendMessage(msg.chat.id, doc, true, msg.message_id, true) return end end @@ -42,5 +44,6 @@ end return { action = action, triggers = triggers, - doc = doc + doc = doc, + command = command } diff --git a/utilities.lua b/utilities.lua index b7d6183..dea5716 100755 --- a/utilities.lua +++ b/utilities.lua @@ -3,6 +3,9 @@ function get_word(s, i) -- get the indexed word in a string + s = s or '' + i = i or 1 + local t = {} for w in s:gmatch('%g+') do table.insert(t, w)