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

117 lines
2.7 KiB
Lua
Raw Normal View History

2015-04-05 15:00:16 +02:00
do
2015-08-01 13:27:29 +02:00
function pairsByKeys(t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
2015-02-04 23:03:42 +01:00
end
2015-04-05 13:57:25 +02:00
2015-08-01 13:27:29 +02:00
-- Returns true if is not empty
local function has_usage_data(dict)
if (dict.usage == nil or dict.usage == '') then
return false
2015-01-23 23:49:51 +01:00
end
2015-08-01 13:27:29 +02:00
return true
2015-04-05 13:57:25 +02:00
end
2015-08-01 13:27:29 +02:00
-- Get commands for that plugin
local function plugin_help(name,number)
local plugin = ""
if number then
local i = 0
for name in pairsByKeys(plugins) do
if plugins[name].hide then
name = nil
else
i = i + 1
if i == tonumber(number) then
plugin = plugins[name]
end
end
end
else
plugin = plugins[name]
2015-11-12 17:42:03 +01:00
if not plugin then return 'Dieses Plugin existiert nicht.' end
2015-08-01 13:27:29 +02:00
end
2015-04-05 13:57:25 +02:00
2015-08-01 13:27:29 +02:00
local text = ""
if (type(plugin.usage) == "table") then
for ku,usage in pairs(plugin.usage) do
text = text..usage..'\n'
end
text = text..'\n'
elseif has_usage_data(plugin) then -- Is not empty
text = text..plugin.usage..'\n\n'
end
return text
2014-11-04 16:09:08 +01:00
end
2015-08-01 13:27:29 +02:00
-- !help command
local function telegram_help()
local i = 0
local text = "Liste aller Plugins\n\n"
-- Plugins names
for name in pairsByKeys(plugins) do
if plugins[name].hide then
name = nil
else
i = i + 1
text = text..i..'. '..name..'\n'
end
2015-04-05 13:57:25 +02:00
end
2015-08-01 13:27:29 +02:00
text = text..'\n'..i.." Plugins sind zurzeit aktiviert"
text = text..'\n'..'Benutze "/hilfe [Plugin Nummer]" fuer mehr Informationen'
text = text..'\n'..'Fragen und/oder Anregungen: @Akamaru'
2015-04-05 13:57:25 +02:00
return text
2015-08-01 13:27:29 +02:00
end
-- !help all command
local function help_all()
local ret = ""
for name in pairsByKeys(plugins) do
if plugins[name].hide then
name = nil
else
ret = ret .. plugin_help(name)
end
end
return ret
end
local function run(msg, matches)
if matches[1] == "#hilfe" then
2015-08-01 13:27:29 +02:00
return telegram_help()
elseif matches[1] == "#hilfe all" then
2015-08-01 13:27:29 +02:00
return help_all()
else
local text = ""
if tonumber(matches[1]) then
text = plugin_help(nil,matches[1])
else
text = plugin_help(matches[1])
end
if not text then
text = telegram_help()
end
return text
end
end
2015-01-23 23:49:51 +01:00
2015-08-01 13:27:29 +02:00
return {
description = "Zeigt die Plugins und Befehle",
usage = {"#hilfe","#hilfe all","#hilfe [Plugin]"},
patterns = {"^#hilfe$","^#hilfe all","^#hilfe (.+)"},
2015-08-01 13:27:29 +02:00
run = run
}
2015-04-05 15:00:16 +02:00
2015-08-01 13:27:29 +02:00
end