This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/plugins/help.lua

110 lines
2.3 KiB
Lua
Raw Normal View History

2015-04-05 15:00:16 +02:00
do
2015-01-23 23:49:51 +01:00
-- Generate an HTML table for GitHub
2015-04-05 15:00:16 +02:00
local function html_help()
2015-01-23 23:49:51 +01:00
local text = [[<table>
<thead>
<tr>
<td><strong>Name</strong></td>
<td><strong>Description</strong></td>
<td><strong>Usage</strong></td>
</tr>
</thead>
<tbody>]]
2014-11-04 16:09:08 +01:00
2015-01-23 23:49:51 +01:00
for k,v in pairs(plugins_names()) do
t = loadfile("plugins/"..v)()
text = text.."<tr>"
text = text.."<td>"..v.."</td>"
text = text.."<td>"..t.description.."</td>"
text = text.."<td>"
if (type(t.usage) == "table") then
for ku,vu in pairs(t.usage) do
text = text..vu.."<br>"
end
else
text = text..t.usage
end
text = text.."</td>"
text = text.."</tr>"
end
text = text.."</tbody></table>"
return text
end
2015-04-05 15:00:16 +02:00
-- Returns true if is not empty
local function has_usage_data(dict)
2015-02-04 23:03:42 +01:00
if (dict.usage == nil or dict.usage == '') then
return false
end
return true
end
2015-04-05 15:00:16 +02:00
-- Get commands for that plugin
local function plugin_help(name)
2015-04-05 13:57:25 +02:00
local plugin = plugins[name]
if not plugin then return nil end
local text = ""
if (type(plugin.usage) == "table") then
for ku,usage in pairs(plugin.usage) do
text = text..usage..'\n'
2015-01-23 23:49:51 +01:00
end
2015-04-05 13:57:25 +02:00
text = text..'\n'
elseif has_usage_data(plugin) then -- Is not empty
text = text..plugin.usage..'\n\n'
end
return text
end
-- !help command
2015-04-05 15:00:16 +02:00
local function telegram_help()
2015-04-05 13:57:25 +02:00
local text = "Plugin list: \n\n"
-- Plugins names
for name in pairs(plugins) do
text = text..name..'\n'
end
text = text..'\n'..'Write "!help [plugin name]" for more info.'
text = text..'\n'..'Or "!help all" to show all info.'
return text
end
-- !help all command
2015-04-05 15:00:16 +02:00
local function help_all()
2015-04-05 13:57:25 +02:00
local ret = ""
for name in pairs(plugins) do
ret = ret .. plugin_help(name)
2014-11-04 16:09:08 +01:00
end
return ret
end
2015-04-05 15:00:16 +02:00
local function run(msg, matches)
2015-04-05 13:57:25 +02:00
if matches[1] == "!help" then
2015-01-23 23:49:51 +01:00
return telegram_help()
2015-04-05 13:57:25 +02:00
elseif matches[1] == "!help all" then
return help_all()
else
local text = plugin_help(matches[1])
if not text then
text = telegram_help()
end
return text
2015-01-23 23:49:51 +01:00
end
end
2014-11-04 16:09:08 +01:00
return {
2015-03-04 23:51:36 +01:00
description = "Help plugin. Get info from other plugins. ",
usage = {
2015-04-05 13:57:25 +02:00
"!help: Show list of plugins.",
"!help all: Show all commands for every plugin.",
"!help [plugin name]: Commands for that plugin."
2015-03-04 23:51:36 +01:00
},
patterns = {
"^!help$",
2015-04-05 13:57:25 +02:00
"^!help all",
"^!help (.+)"
2015-03-04 23:51:36 +01:00
},
run = run
2015-04-05 15:00:16 +02:00
}
end