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

69 lines
1.6 KiB
Lua
Raw Normal View History

local command = 'pokedex <query>'
local doc = [[```
/pokedex <query>
Returns a Pokedex entry from pokeapi.co.
Alias: /dex
```]]
2015-07-03 00:15:52 +02:00
local triggers = {
'^/pokedex[@'..bot.username..']*',
'^/dex[@'..bot.username..']*'
2015-07-03 00:15:52 +02:00
}
local action = function(msg)
2015-07-03 00:15:52 +02:00
local input = utilities.input(msg.text_lower)
2015-07-03 00:15:52 +02:00
if not input then
if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text
else
sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return
end
2015-07-03 00:15:52 +02:00
end
local url = 'http://pokeapi.co'
2015-07-03 00:15:52 +02: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
sendReply(msg, config.errors.connection)
return
2015-07-03 00:15:52 +02:00
end
local dex_jdat = JSON.decode(dex_jstr)
local desc_url = url .. dex_jdat.descriptions[math.random(#dex_jdat.descriptions)].resource_uri
2015-07-03 00:15:52 +02:00
local desc_jstr, res = HTTP.request(desc_url)
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
2015-07-03 00:15:52 +02:00
end
local desc_jdat = JSON.decode(desc_jstr)
local poke_type
for i,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'
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
sendMessage(msg.chat.id, output, true, nil, true)
2015-07-03 00:15:52 +02:00
end
return {
action = action,
triggers = triggers,
doc = doc,
command = command
}