Rewrote help.lua and added markup support to several plugins.

help.lua has been rewritten to support "/help command".
New variable "command" has been added to plugins for the syntax (w/out slash) to be displayed in main
help message. "doc" will be displayed upon "/help command".

Output of >12 plugins has been reformated to utilize markup.
There is a fairly standard style throughout plugins.

get_word() in utilities.lua now has defaults for nil arguments.
This commit is contained in:
topkecleon 2016-01-08 08:44:37 -05:00
parent 9070a44c8f
commit 26c1299374
36 changed files with 374 additions and 238 deletions

View File

@ -119,13 +119,14 @@ For support for otouto and bots in general, join my Bot Development group. Follo
##Development ##Development
Everybody is free to contribute to otouto. Here I will explain various things that are important to know about the plugin system. 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? | | Component | Description | Optional? |
|-----------|-------------|-----------| |-----------|-------------|-----------|
| action | The main function of a plugin. It accepts the `msg` table. | No. | | 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. | | 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 | | 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. 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.

View File

@ -1,7 +1,5 @@
local doc = [[ local command = 'about'
/about local doc = '`Returns information about the bot.`'
Get info about the bot.
]]
local triggers = { local triggers = {
'' ''
@ -26,5 +24,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'bandersnatch'
/bandersnatch local doc = [[```
Shun the frumious Bandersnatch. Shun the frumious Bandersnatch.
]] Alias: /bc
```]]
local triggers = { local triggers = {
'^/bandersnatch[@'..bot.username..']*', '^/bandersnatch[@'..bot.username..']*',
@ -31,5 +32,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc command = command,
desc = desc
} }

View File

@ -4,10 +4,12 @@ if not config.biblia_api_key then
return return
end end
local doc = [[ local command = 'bible <reference>'
local doc = [[```
/bible <reference> /bible <reference>
Returns a verse from the American Standard Version of the Bible, or an apocryphal verse from the King James Version. Results from biblia.com. 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 = { local triggers = {
'^/bible*[@'..bot.username..']*', '^/bible*[@'..bot.username..']*',
@ -19,7 +21,7 @@ local action = function(msg)
local input = msg.text:input() local input = msg.text:input()
if not input then if not input then
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
@ -47,5 +49,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
command = command,
doc = doc doc = doc
} }

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'calc <expression>'
local doc = [[```
/calc <expression> /calc <expression>
Returns solutions to mathematical expressions and conversions between common units. Results provided by mathjs.org. Returns solutions to mathematical expressions and conversions between common units. Results provided by mathjs.org.
]] ```]]
local triggers = { local triggers = {
'^/calc[@'..bot.username..']*' '^/calc[@'..bot.username..']*'
@ -14,25 +15,28 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
local url = 'https://api.mathjs.org/v1/?expr=' .. URL.escape(input) local url = 'https://api.mathjs.org/v1/?expr=' .. URL.escape(input)
local ans, res = HTTPS.request(url) local output = HTTPS.request(url)
if not ans then if not output then
sendReply(msg, config.errors.connection) sendReply(msg, config.errors.connection)
return return
end end
sendReply(msg, ans) output = '`' .. output .. '`'
sendMessage(msg.chat.id, output, true, msg.message_id, true)
end end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
command = command,
doc = doc doc = doc
} }

View File

@ -3,10 +3,8 @@ if not config.thecatapi_key then
print('cats.lua will be enabled, but there are more features with a key.') print('cats.lua will be enabled, but there are more features with a key.')
end end
local doc = [[ local command = 'cat'
/cat local doc = '`Returns a cat!`'
Returns a cat!
]]
local triggers = { local triggers = {
'^/cat[@'..bot.username..']*$' '^/cat[@'..bot.username..']*$'
@ -34,5 +32,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,9 +1,7 @@
-- Commits from https://github.com/ngerakines/commitment. -- Commits from https://github.com/ngerakines/commitment.
local doc = [[ local command = 'commit'
/commit local doc = '`Returns a commit message from whatthecommit.com.`'
Returns a commit message from whatthecommit.com.
]]
local triggers = { local triggers = {
'^/commit[@'..bot.username..']*' '^/commit[@'..bot.username..']*'
@ -414,12 +412,13 @@ local commits = {
local action = function(msg) local action = function(msg)
sendMessage(msg.chat.id, commits[math.random(#commits)]) sendMessage(msg.chat.id, '`'..commits[math.random(#commits)]..'`', true, nil, true)
end end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,8 +1,9 @@
local doc = [[ local command = 'cash [amount] <from> to <to>'
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.
]] ```]]
local triggers = { local triggers = {
'^/cash[@'..bot.username..']*' '^/cash[@'..bot.username..']*'
@ -12,7 +13,7 @@ local action = function(msg)
local input = msg.text:upper() local input = msg.text:upper()
if not input:match('%a%a%a TO %a%a%a') then 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 return
end end
@ -42,13 +43,17 @@ local action = function(msg)
end end
local message = amount .. ' ' .. from .. ' = ' .. result .. ' ' .. to local output = amount .. ' ' .. from .. ' = ' .. result .. ' ' .. to .. '\n'
sendReply(msg, message) output = output .. os.date('!%F %T UTC')
output = '`' .. output .. '`'
sendMessage(msg.chat.id, output, true, nil, true)
end end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'roll <nDr>'
local doc = [[```
/roll <nDr> /roll <nDr>
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. 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 = { local triggers = {
'^/roll[@'..bot.username..']*' '^/roll[@'..bot.username..']*'
@ -11,7 +12,7 @@ local action = function(msg)
local input = msg.text_lower:input() local input = msg.text_lower:input()
if not input then if not input then
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
@ -22,7 +23,7 @@ local action = function(msg)
count = 1 count = 1
range = input:match('^d?([%d]+)$') range = input:match('^d?([%d]+)$')
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
@ -38,17 +39,19 @@ local action = function(msg)
return return
end end
local message = '' local output = '*' .. count .. 'd' .. range .. '*\n`'
for i = 1, count do for i = 1, count do
message = message .. math.random(range) .. '\t' output = output .. math.random(range) .. '\t'
end end
output = output .. '`'
sendReply(msg, message) sendMessage(msg.chat.id, output, true, msg.message_id, true)
end end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'echo <text>'
local doc = [[```
/echo <text> /echo <text>
Repeat a string of text! Repeats a string of text.
]] ```]]
local triggers = { local triggers = {
'^/echo[@'..bot.username..']*' '^/echo[@'..bot.username..']*'
@ -14,7 +15,7 @@ local action = function(msg)
if input then if input then
sendMessage(msg.chat.id, latcyr(input)) sendMessage(msg.chat.id, latcyr(input))
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
end end
end end
@ -22,5 +23,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,5 @@
local doc = [[ local command = '8ball'
/8ball local doc = '`Returns an answer from a magic 8-ball!`'
Returns an answer from a magic 8-ball!
]]
local triggers = { local triggers = {
'^/8ball', '^/8ball',
@ -60,5 +58,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -7,10 +7,8 @@ if s:match('fortune: command not found') then
return return
end end
local doc = [[ local command = 'fortune'
/fortune local doc = '`Returns a UNIX fortune.`'
Returns a UNIX fortune.
]]
local triggers = { local triggers = {
'^/fortune[@'..bot.username..']*' '^/fortune[@'..bot.username..']*'
@ -26,5 +24,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -11,10 +11,12 @@ elseif not config.google_cse_key then
return return
end end
local doc = [[ local command = 'image <query>'
local doc = [[```
/image <query> /image <query>
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. 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 = { local triggers = {
'^/image[@'..bot.username..']*', '^/image[@'..bot.username..']*',
@ -30,7 +32,7 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
@ -69,5 +71,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,9 @@
local doc = [[ local command = 'location <query>'
local doc = [[```
/location <query> /location <query>
Returns a location from Google Maps. Returns a location from Google Maps.
]] Alias: /loc
```]]
triggers = { triggers = {
'^/location[@'..bot.username..']*', '^/location[@'..bot.username..']*',
@ -16,7 +18,7 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
@ -34,5 +36,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,9 @@
local doc = [[ local command = 'google <query>'
local doc = [[```
/google <query> /google <query>
Returns four (if group) or eight (if private message) results from Google. Safe search is enabled by default, use "/gnsfw" to disable it. 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 = { local triggers = {
'^/g[@'..bot.username..']*$', '^/g[@'..bot.username..']*$',
@ -17,7 +19,7 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
@ -43,22 +45,36 @@ local action = function(msg)
end end
local jdat = JSON.decode(jstr) 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) sendReply(msg, config.errors.results)
return return
end end
local message = '' 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
message = message .. jdat.responseData.results[i].titleNoFormatting .. '\n ' .. jdat.responseData.results[i].unescapedUrl .. '\n' local title = jdat.responseData.results[i].titleNoFormatting:gsub('%[.+%]', ''):gsub('&amp;', '&')
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 end
sendReply(msg, message) sendMessage(msg.chat.id, output, true, nil, true)
end end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'hackernews'
/hackernews local doc = [[```
Returns four (if group) or eight (if private message) top stories from Hacker News. Returns four (if group) or eight (if private message) top stories from Hacker News.
]] Alias: /hn
```]]
local triggers = { local triggers = {
'^/hackernews[@'..bot.username..']*', '^/hackernews[@'..bot.username..']*',
@ -25,7 +26,7 @@ local action = function(msg)
res_count = 8 res_count = 8
end end
local message = '' local output = '*Hacker News:*\n'
for i = 1, res_count do for i = 1, res_count do
local res_url = 'https://hacker-news.firebaseio.com/v0/item/' .. jdat[i] .. '.json' local res_url = 'https://hacker-news.firebaseio.com/v0/item/' .. jdat[i] .. '.json'
jstr, res = HTTPS.request(res_url) jstr, res = HTTPS.request(res_url)
@ -34,15 +35,26 @@ local action = function(msg)
return return
end end
local res_jdat = JSON.decode(jstr) local res_jdat = JSON.decode(jstr)
message = message .. res_jdat.title .. '\n ' .. res_jdat.url .. '\n' local title = res_jdat.title:gsub('%[.+%]', ''):gsub('%(.+%)', ''):gsub('&amp;', '&')
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) end
sendMessage(msg.chat.id, output, true, nil, true)
end end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -19,10 +19,12 @@ if not hs_dat then
end end
local doc = [[ local command = 'hearthstone <query>'
local doc = [[```
/hearthstone <query> /hearthstone <query>
Returns Hearthstone card info. Returns Hearthstone card info.
]] Alias: /hn
```]]
local triggers = { local triggers = {
'^/hearthstone[@'..bot.username..']*', '^/hearthstone[@'..bot.username..']*',
@ -65,7 +67,7 @@ local format_card = function(card)
if card.text then if card.text then
info = card.text:gsub('</?.->',''):gsub('%$','') info = card.text:gsub('</?.->',''):gsub('%$','')
if card.flavor then if card.flavor then
info = info .. '\n' .. card.flavor info = info .. '\n_' .. card.flavor .. '_'
end end
elseif card.flavor then elseif card.flavor then
info = card.flavor info = card.flavor
@ -73,7 +75,7 @@ local format_card = function(card)
info = nil info = nil
end end
local s = card.name .. '\n' .. ctype local s = '*' .. card.name .. '*\n' .. ctype
if stats then if stats then
s = s .. '\n' .. stats s = s .. '\n' .. stats
end end
@ -89,7 +91,7 @@ local action = function(msg)
local input = msg.text_lower:input() local input = msg.text_lower:input()
if not input then if not input then
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
@ -106,12 +108,13 @@ local action = function(msg)
return return
end end
sendReply(msg, output) sendMessage(msg.chat.id, output, true, msg.message_id, true)
end end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,16 +1,18 @@
-- This plugin should go at the end of your plugin list in -- This plugin should go at the end of your plugin list in
-- config.lua, but not after greetings.lua. -- 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 for i,v in ipairs(plugins) do
if v.doc then if v.command then
local a = string.sub(v.doc, 1, string.find(v.doc, '\n')-1) help_text = help_text .. '\n /' .. v.command:gsub('%[', '\\[')
help_text = help_text .. a .. '\n'
end end
end end
local help_text = help_text .. 'Arguments: <required> [optional]' help_text = help_text .. [[\n
/help <command>
Arguments: <required> \[optional]
]]
local triggers = { local triggers = {
'^/help[@'..bot.username..']*', '^/help[@'..bot.username..']*',
@ -20,16 +22,30 @@ local triggers = {
local action = function(msg) local action = function(msg)
if msg.from.id ~= msg.chat.id then local input = msg.text_lower:input()
if sendMessage(msg.from.id, help_text) then
-- 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.') sendReply(msg, 'I have sent you the requested information in a private message.')
else
sendReply(msg, help_text)
end end
else return
sendReply(msg, help_text)
end 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 end
return { return {

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'imdb <query>'
local doc = [[```
/imdb <query> /imdb <query>
Returns an IMDb entry. Returns an IMDb entry.
]] ```]]
local triggers = { local triggers = {
'^/imdb[@'..bot.username..']*' '^/imdb[@'..bot.username..']*'
@ -14,7 +15,7 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
@ -34,17 +35,18 @@ local action = function(msg)
return return
end end
local message = jdat.Title ..' ('.. jdat.Year ..')\n' local output = '[' .. jdat.Title .. '](http://imdb.com/title/'
message = message .. jdat.imdbRating ..' | '.. jdat.Runtime ..' | '.. jdat.Genre ..'\n' output = output .. jdat.imdbID .. ') ('.. jdat.Year ..')\n'
message = message .. jdat.Plot .. '\n' output = output .. jdat.imdbRating ..'/10 | '.. jdat.Runtime ..' | '.. jdat.Genre ..'\n'
message = message .. 'http://imdb.com/title/' .. jdat.imdbID output = output .. jdat.Plot
sendReply(msg, message) sendMessage(msg.chat.id, output, true, nil, true)
end end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -4,13 +4,14 @@ if not config.lastfm_api_key then
return return
end end
local doc = [[ local command = 'lastfm'
/lastfm local doc = [[```
/np [username] /np [username]
Returns what you are or were last listening to. If you specify a username, info will be returned for that username. Returns what you are or were last listening to. If you specify a username, info will be returned for that username.
/fmset <username> /fmset <username>
Sets your last.fm username. Otherwise, /np will use your Telegram username. Use "/fmset -" to delete it. Sets your last.fm username. Otherwise, /np will use your Telegram username. Use "/fmset -" to delete it.
]] ```]]
local triggers = { local triggers = {
'^/lastfm[@'..bot.username..']*', '^/lastfm[@'..bot.username..']*',
@ -28,7 +29,7 @@ local action = function(msg)
return return
elseif string.match(msg.text, '^/fmset') then elseif string.match(msg.text, '^/fmset') then
if not input then if not input then
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
elseif input == '-' then elseif input == '-' then
lastfm[msg.from.id_str] = nil lastfm[msg.from.id_str] = nil
sendReply(msg, 'Your last.fm username has been forgotten.') sendReply(msg, 'Your last.fm username has been forgotten.')
@ -101,5 +102,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'nick <nickname>'
local doc = [[```
/nick <nickname> /nick <nickname>
Set your nickname. Use "/whoami" to check your nickname and "/nick -" to delete it. Set your nickname. Use "/whoami" to check your nickname and "/nick -" to delete it.
]] ```]]
local triggers = { local triggers = {
'^/nick[@'..bot.username..']*' '^/nick[@'..bot.username..']*'
@ -11,7 +12,7 @@ local action = function(msg)
local input = msg.text:input() local input = msg.text:input()
if not input then if not input then
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return true return true
end end
@ -38,5 +39,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,9 @@
local doc = [[ local command = 'pokedex <query>'
local doc = [[```
/pokedex <query> /pokedex <query>
Returns a Pokedex entry from pokeapi.co. Returns a Pokedex entry from pokeapi.co.
]] Alias: /dex
```]]
local triggers = { local triggers = {
'^/pokedex[@'..bot.username..']*', '^/pokedex[@'..bot.username..']*',
@ -15,7 +17,7 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
@ -51,14 +53,16 @@ local action = function(msg)
end end
poke_type = poke_type .. ' type' 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 end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,5 @@
local doc = [[ local command = 'pun'
/pun local doc = '`Returns a pun.`'
Returns a pun.
]]
local triggers = { local triggers = {
'^/pun[@'..bot.username..']*' '^/pun[@'..bot.username..']*'
@ -140,5 +138,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,5 @@
local doc = [[ local command = 'reactions'
/reactions local doc = '`Returns a list of "reaction" emoticon commands.`'
Returns a list of "reaction" emoticon commands.
]]
local triggers = { local triggers = {
['¯\\_(ツ)_/¯'] = '/shrug$', ['¯\\_(ツ)_/¯'] = '/shrug$',
@ -33,5 +31,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,9 @@
local doc = [[ local command = 'reddit [r/subreddit | query]'
local doc = [[```
/reddit [r/subreddit | query] /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. 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 = { local triggers = {
'^/reddit[@'..bot.username..']*', '^/reddit[@'..bot.username..']*',
@ -21,14 +23,18 @@ local action = function(msg)
limit = 8 limit = 8
end end
local source
if input then if input then
if input:match('^r/') then if input:match('^r/.') then
url = 'http://www.reddit.com/' .. input .. '/.json?limit=' .. limit url = 'http://www.reddit.com/' .. input .. '/.json?limit=' .. limit
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'
end end
else else
url = 'http://www.reddit.com/.json?limit=' .. limit url = 'http://www.reddit.com/.json?limit=' .. limit
source = '*/r/all*\n'
end end
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
@ -43,25 +49,31 @@ local action = function(msg)
return return
end end
local message = '' local output = ''
for i,v in ipairs(jdat.data.children) do for i,v in ipairs(jdat.data.children) do
local title = v.data.title:gsub('%[.+%]', ''):gsub('&amp;', '&')
if title:len() > 48 then
title = title:sub(1,45) .. '...'
end
if v.data.over_18 then if v.data.over_18 then
message = message .. '[NSFW] ' v.data.is_self = true
end 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 if not v.data.is_self then
long_url = '\n' .. v.data.url .. '\n' output = output .. v.data.url:gsub('_', '\\_') .. '\n'
end end
local short_url = '[redd.it/' .. v.data.id .. '] '
message = message .. short_url .. v.data.title .. long_url
end end
sendReply(msg, message) output = source .. output
sendMessage(msg.chat.id, output, true, nil, true)
end end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'shout <text>'
/shout <term> local doc = [[```
Shout something! /shout <text>
]] Shouts something.
```]]
local triggers = { local triggers = {
'^/shout[@'..bot.username..']*' '^/shout[@'..bot.username..']*'
@ -12,7 +13,7 @@ local action = function(msg)
local input = msg.text:input() local input = msg.text:input()
if not input then if not input then
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
input = input:trim() input = input:trim()
@ -44,5 +45,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'slap [target]'
local doc = [[```
/slap [target] /slap [target]
Give someone a good slap (or worse) through reply or specification of a target. Slap somebody.
]] ```]]
local triggers = { local triggers = {
'^/slap[@'..bot.username..']*' '^/slap[@'..bot.username..']*'
@ -125,5 +126,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'time <location>'
local doc = [[```
/time <location> /time <location>
Returns the time, date, and timezone for the given location. Returns the time, date, and timezone for the given location.
]] ```]]
local triggers = { local triggers = {
'^/time[@'..bot.username..']*' '^/time[@'..bot.username..']*'
@ -14,7 +15,7 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
@ -49,5 +50,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'translate [text]'
local doc = [[```
/translate [text] /translate [text]
Translates input or the replied-to message into the bot's language. Translates input or the replied-to message into the bot's language.
]] ```]]
local triggers = { local triggers = {
'^/translate[@'..bot.username..']*' '^/translate[@'..bot.username..']*'
@ -14,7 +15,7 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
@ -36,5 +37,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,9 @@
local doc = [[ local command = 'urbandictionary <query>'
local doc = [[```
/urbandictionary <query> /urbandictionary <query>
Returns a definition from Urban Dictionary. Returns a definition from Urban Dictionary.
]] Aliases: /ud, /urban
```]]
local triggers = { local triggers = {
'^/urbandictionary[@'..bot.username..']*', '^/urbandictionary[@'..bot.username..']*',
@ -17,7 +19,7 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
@ -36,17 +38,20 @@ local action = function(msg)
return return
end 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 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 end
sendReply(msg, message) output = output:gsub('%[', ''):gsub('%]', '')
sendMessage(msg.chat.id, output, true, nil, true)
end end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -4,10 +4,11 @@ if not config.owm_api_key then
return return
end end
local doc = [[ local command = 'weather <location>'
local doc = [[```
/weather <location> /weather <location>
Returns the current weather conditions for a given location. Returns the current weather conditions for a given location.
]] ```]]
local triggers = { local triggers = {
'^/weather[@'..bot.username..']*' '^/weather[@'..bot.username..']*'
@ -20,7 +21,7 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
@ -52,5 +53,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -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 = { local triggers = {
'^/who[ami]*[@'..bot.username..']*$' '^/who[ami]*[@'..bot.username..']*$'
} }
@ -37,5 +43,7 @@ end
return { return {
action = action, action = action,
triggers = triggers triggers = triggers,
doc = doc,
command = command
} }

View File

@ -1,7 +1,9 @@
local doc = [[ local command = 'wikipedia <query>'
local doc = [[```
/wikipedia <query> /wikipedia <query>
Returns an article from Wikipedia. Returns an article from Wikipedia.
]] Aliases: /w, /wiki
```]]
local triggers = { local triggers = {
'^/wikipedia[@'..bot.username..']*', '^/wikipedia[@'..bot.username..']*',
@ -17,7 +19,7 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
@ -32,6 +34,10 @@ local action = function(msg)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
if not jdat.responseData then
sendReply(msg, config.errors.connection)
return
end
if not jdat.responseData.results[1] then if not jdat.responseData.results[1] then
sendReply(msg, config.errors.results) sendReply(msg, config.errors.results)
return return
@ -61,14 +67,24 @@ local action = function(msg)
if l then if l then
text = text:sub(1, l-1) text = text:sub(1, l-1)
end 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 end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,7 +1,8 @@
local doc = [[ local command = 'xkcd [query]'
local doc = [[```
/xkcd [query] /xkcd [query]
Returns an xkcd strip and its alt text. If there is no query, it will be randomized. Returns an xkcd strip and its alt text. If there is no query, it will be randomized.
]] ```]]
local triggers = { local triggers = {
'^/xkcd[@'..bot.username..']*' '^/xkcd[@'..bot.username..']*'
@ -44,13 +45,15 @@ local action = function(msg)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
local message = '[' .. jdat.num .. '] ' .. jdat.alt .. '\n' .. jdat.img local output = '[' .. jdat.num .. '](' .. jdat.img .. ')\n' .. jdat.alt
sendMessage(msg.chat.id, message, false, msg.message_id)
sendMessage(msg.chat.id, output, false, nil, true)
end end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -1,9 +1,11 @@
-- Thanks to @TiagoDanin for writing the original plugin. -- Thanks to @TiagoDanin for writing the original plugin.
local doc = [[ local command = 'youtube <query>'
local doc = [[```
/youtube <query> /youtube <query>
Returns the top result from YouTube. Returns the top result from YouTube.
]] Alias: /yt
```]]
local triggers = { local triggers = {
'^/youtube[@'..bot.username..']*', '^/youtube[@'..bot.username..']*',
@ -18,7 +20,7 @@ local action = function(msg)
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text input = msg.reply_to_message.text
else else
sendReply(msg, doc) sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return return
end end
end end
@ -42,5 +44,6 @@ end
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
doc = doc doc = doc,
command = command
} }

View File

@ -3,6 +3,9 @@
function get_word(s, i) -- get the indexed word in a string function get_word(s, i) -- get the indexed word in a string
s = s or ''
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)