2016-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-04-11 06:04:47 +02:00
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
local help = {}
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
function help:init(config)
|
|
|
|
help.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('help', true):t('h', true).table
|
2016-08-14 04:26:44 +02:00
|
|
|
help.command = 'help [command]'
|
2016-07-25 11:03:35 +02:00
|
|
|
help.doc = config.cmd_pat .. 'help [command] \nReturns usage information for a given command.'
|
2016-04-14 05:48:20 +02:00
|
|
|
end
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
function help:action(msg, config)
|
2016-04-08 23:12:02 +02:00
|
|
|
local input = utilities.input(msg.text_lower)
|
2016-07-25 11:03:35 +02:00
|
|
|
if input then
|
2016-08-14 04:26:44 +02:00
|
|
|
if not help.help_word then
|
|
|
|
for _, plugin in ipairs(self.plugins) do
|
|
|
|
if plugin.command and plugin.doc and not plugin.help_word then
|
|
|
|
plugin.help_word = utilities.get_word(plugin.command, 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-25 11:03:35 +02:00
|
|
|
for _,plugin in ipairs(self.plugins) do
|
|
|
|
if plugin.help_word == input:gsub('^/', '') then
|
2016-08-14 04:26:44 +02:00
|
|
|
local output = '*Help for* _' .. plugin.help_word .. '_*:*\n' .. plugin.doc
|
2016-07-25 11:03:35 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, output, true, nil, true)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
utilities.send_reply(self, msg, 'Sorry, there is no help for that command.')
|
|
|
|
else
|
2016-08-14 04:26:44 +02:00
|
|
|
-- Generate the help message on first run.
|
|
|
|
if not help.text then
|
|
|
|
local commandlist = {}
|
|
|
|
for _, plugin in ipairs(self.plugins) do
|
|
|
|
if plugin.command then
|
|
|
|
table.insert(commandlist, plugin.command)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(commandlist)
|
|
|
|
help.text = '*Available commands:*\n• ' .. config.cmd_pat .. table.concat(commandlist, '\n• '..config.cmd_pat) .. '\nArguments: <required> [optional]'
|
|
|
|
help.text = help.text:gsub('%[', '\\[')
|
|
|
|
end
|
2016-07-25 11:03:35 +02:00
|
|
|
-- Attempt to send the help message via PM.
|
|
|
|
-- If msg is from a group, tell the group whether the PM was successful.
|
2016-08-14 04:26:44 +02:00
|
|
|
local res = utilities.send_message(self, msg.from.id, help.text, true, nil, true)
|
2016-01-08 14:44:37 +01:00
|
|
|
if not res then
|
2016-06-15 14:35:39 +02:00
|
|
|
utilities.send_reply(self, msg, 'Please [message me privately](http://telegram.me/' .. self.info.username .. '?start=help) for a list of commands.', true)
|
2016-01-08 14:44:37 +01:00
|
|
|
elseif msg.chat.type ~= 'private' then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, 'I have sent you the requested information in a private message.')
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2016-01-08 14:44:37 +01:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return help
|