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-2/otouto/plugins/pokedex.lua

72 lines
2.1 KiB
Lua
Raw Normal View History

local pokedex = {}
local HTTP = require('socket.http')
local JSON = require('dkjson')
2016-06-07 06:31:34 +02:00
local bindings = require('otouto.bindings')
local utilities = require('otouto.utilities')
pokedex.command = 'pokedex <query>'
function pokedex:init(config)
2016-08-14 04:46:18 +02:00
pokedex.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('pokedex', true):t('dex', true).table
pokedex.doc = config.cmd_pat .. [[pokedex <query>
Returns a Pokedex entry from pokeapi.co.
2016-08-14 04:26:44 +02:00
Queries must be a number of the name of a Pokémon.
Alias: ]] .. config.cmd_pat .. 'dex'
end
2015-07-03 00:15:52 +02:00
2016-05-27 02:26:30 +02:00
function pokedex:action(msg, config)
2015-07-03 00:15:52 +02:00
2016-08-14 04:46:18 +02:00
local input = utilities.input_from_msg(msg)
if not input then
utilities.send_reply(msg, pokedex.doc, 'html')
2016-08-14 04:46:18 +02:00
return
end
2015-07-03 00:15:52 +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 url = 'http://pokeapi.co'
2015-07-03 00:15:52 +02:00
2016-08-14 04:46:18 +02:00
local dex_url = url .. '/api/v1/pokemon/' .. input
local dex_jstr, res = HTTP.request(dex_url)
if res ~= 200 then
utilities.send_reply(msg, config.errors.connection)
2016-08-14 04:46:18 +02:00
return
end
2015-07-03 00:15:52 +02:00
2016-08-14 04:46:18 +02:00
local dex_jdat = JSON.decode(dex_jstr)
2015-07-03 00:15:52 +02:00
2016-08-14 04:46:18 +02:00
if not dex_jdat.descriptions or not dex_jdat.descriptions[1] then
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 desc_url = url .. dex_jdat.descriptions[math.random(#dex_jdat.descriptions)].resource_uri
local desc_jstr, _ = HTTP.request(desc_url)
if res ~= 200 then
utilities.send_reply(msg, config.errors.connection)
2016-08-14 04:46:18 +02:00
return
end
2015-07-03 00:15:52 +02:00
2016-08-14 04:46:18 +02:00
local desc_jdat = JSON.decode(desc_jstr)
2015-07-03 00:15:52 +02:00
2016-08-14 04:46:18 +02:00
local poke_type
for _,v in ipairs(dex_jdat.types) do
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'
2015-07-03 00:15:52 +02:00
2016-08-14 04:46:18 +02: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
utilities.send_message(msg.chat.id, output, true, nil, true)
2015-07-03 00:15:52 +02:00
end
return pokedex