2016-08-14 04:26:44 +02:00
|
|
|
-- Based on a plugin by matthewhesketh.
|
|
|
|
|
|
|
|
local HTTP = require('socket.http')
|
|
|
|
local JSON = require('dkjson')
|
|
|
|
local bindings = require('otouto.bindings')
|
|
|
|
local utilities = require('otouto.utilities')
|
|
|
|
|
|
|
|
local starwars = {}
|
|
|
|
|
|
|
|
function starwars:init(config)
|
2016-08-14 04:46:18 +02:00
|
|
|
starwars.triggers = utilities.triggers(self.info.username, config.cmd_pat)
|
|
|
|
:t('starwars', true):t('sw', true).table
|
|
|
|
starwars.doc = config.cmd_pat .. [[starwars <query>
|
2016-08-14 04:26:44 +02:00
|
|
|
Returns the opening crawl from the specified Star Wars film.
|
|
|
|
Alias: ]] .. config.cmd_pat .. 'sw'
|
2016-08-14 04:46:18 +02:00
|
|
|
starwars.command = 'starwars <query>'
|
|
|
|
starwars.base_url = 'http://swapi.co/api/films/'
|
2016-08-14 04:26:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local films_by_number = {
|
2016-08-14 04:46:18 +02:00
|
|
|
['phantom menace'] = 4,
|
|
|
|
['attack of the clones'] = 5,
|
|
|
|
['revenge of the sith'] = 6,
|
|
|
|
['new hope'] = 1,
|
|
|
|
['empire strikes back'] = 2,
|
|
|
|
['return of the jedi'] = 3,
|
|
|
|
['force awakens'] = 7
|
2016-08-14 04:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
local corrected_numbers = {
|
2016-08-14 04:46:18 +02:00
|
|
|
4,
|
|
|
|
5,
|
|
|
|
6,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
7
|
2016-08-14 04:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function starwars:action(msg, config)
|
2016-08-14 04:46:18 +02:00
|
|
|
local input = utilities.input_from_msg(msg)
|
|
|
|
if not input then
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_reply(msg, starwars.doc, true)
|
2016-08-14 04:46:18 +02:00
|
|
|
return
|
|
|
|
end
|
2016-08-14 04:26:44 +02:00
|
|
|
|
2016-08-23 06:16:32 +02:00
|
|
|
bindings.sendChatAction{ chat_id = msg.chat.id, action = 'typing' }
|
2016-08-14 04:26:44 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local film
|
|
|
|
if tonumber(input) then
|
|
|
|
input = tonumber(input)
|
|
|
|
film = corrected_numbers[input] or input
|
|
|
|
else
|
|
|
|
for title, number in pairs(films_by_number) do
|
|
|
|
if string.match(input, title) then
|
|
|
|
film = number
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-14 04:26:44 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
if not film then
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_reply(msg, config.errors.results)
|
2016-08-14 04:46:18 +02:00
|
|
|
return
|
|
|
|
end
|
2016-08-14 04:26:44 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local url = starwars.base_url .. film
|
|
|
|
local jstr, code = HTTP.request(url)
|
|
|
|
if code ~= 200 then
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_reply(msg, config.errors.connection)
|
2016-08-14 04:46:18 +02:00
|
|
|
return
|
|
|
|
end
|
2016-08-14 04:26:44 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local output = '*' .. JSON.decode(jstr).opening_crawl .. '*'
|
2016-08-23 06:16:32 +02:00
|
|
|
utilities.send_message(msg.chat.id, output, true, nil, true)
|
2016-08-14 04:26:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return starwars
|