2016-04-11 06:04:47 +02:00
|
|
|
local pokedex = {}
|
|
|
|
|
|
|
|
local HTTP = require('socket.http')
|
2016-04-15 21:07:23 +02:00
|
|
|
local JSON = require('dkjson')
|
2016-04-11 06:04:47 +02:00
|
|
|
local bindings = require('bindings')
|
|
|
|
local utilities = require('utilities')
|
|
|
|
|
|
|
|
pokedex.command = 'pokedex <query>'
|
|
|
|
pokedex.doc = [[```
|
2016-01-08 14:44:37 +01:00
|
|
|
/pokedex <query>
|
|
|
|
Returns a Pokedex entry from pokeapi.co.
|
|
|
|
Alias: /dex
|
|
|
|
```]]
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
function pokedex:init()
|
|
|
|
pokedex.triggers = utilities.triggers(self.info.username):t('pokedex', true):t('dex', true).table
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
function pokedex:action(msg)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
bindings.sendChatAction(self, { chat_id = msg.chat.id, action = 'typing' } )
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
local input = utilities.input(msg.text_lower)
|
2015-07-03 00:15:52 +02:00
|
|
|
if not input then
|
2015-11-25 03:22:04 +01:00
|
|
|
if msg.reply_to_message and msg.reply_to_message.text then
|
|
|
|
input = msg.reply_to_message.text
|
|
|
|
else
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, pokedex.doc, true, msg.message_id, true)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local url = 'http://pokeapi.co'
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local dex_url = url .. '/api/v1/pokemon/' .. input
|
2015-07-03 00:15:52 +02:00
|
|
|
local dex_jstr, res = HTTP.request(dex_url)
|
|
|
|
if res ~= 200 then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, self.config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local dex_jdat = JSON.decode(dex_jstr)
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local desc_url = url .. dex_jdat.descriptions[math.random(#dex_jdat.descriptions)].resource_uri
|
2016-04-11 06:04:47 +02:00
|
|
|
local desc_jstr, _ = HTTP.request(desc_url)
|
2015-07-03 00:15:52 +02:00
|
|
|
if res ~= 200 then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, self.config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local desc_jdat = JSON.decode(desc_jstr)
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local poke_type
|
2016-04-11 06:04:47 +02:00
|
|
|
for _,v in ipairs(dex_jdat.types) do
|
2015-07-03 00:15:52 +02:00
|
|
|
local type_name = v.name:gsub("^%l", string.upper)
|
|
|
|
if not poke_type then
|
|
|
|
poke_type = type_name
|
|
|
|
else
|
|
|
|
poke_type = poke_type .. ' / ' .. type_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
poke_type = poke_type .. ' type'
|
|
|
|
|
2016-01-08 14:44:37 +01:00
|
|
|
local output = '*' .. dex_jdat.name .. '*\n#' .. dex_jdat.national_id .. ' | ' .. poke_type .. '\n_' .. desc_jdat.description:gsub('POKMON', 'Pokémon'):gsub('Pokmon', 'Pokémon') .. '_'
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-01-08 14:44:37 +01:00
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, output, true, nil, true)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return pokedex
|