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

53 lines
1.6 KiB
Lua

local pokedex = {}
pokedex.command = 'pokedex <Name (englisch/ID>'
function pokedex:init(config)
pokedex.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('pokedex', true):t('dex', true):t('pkmn', true).table
pokedex.doc = [[*
]]..config.cmd_pat..[[pokedex* _<Name oder ID>_: Sucht Pokémon im Pokedex (nur englisch)
]]
end
function pokedex:get_pkmn(query)
local url = "http://pokeapi.co/api/v1/pokemon/" .. query .. "/"
local res, code = http.request(url)
if code ~= 200 then return nil end
local pokemon = json.decode(res)
if not pokemon then return nil end
local text = '<b>Pokédex ID:</b> '..pokemon.pkdx_id
..'\n<b>Name:</b> '..pokemon.name
..'\n<b>Typ:</b> '..pokemon.types[1].name
..'\n<b>Gewicht:</b> '..pokemon.height..' kg'
..'\n<b>Größe:</b> '..pokemon.weight..' cm'
..'\n<b>Geschwindigkeit:</b> '..pokemon.speed
if not pokemon.species:isempty() then
text = text..'\n<b>Spezies:</b> '..pokemon.species
end
return text, pokemon.pkdx_id
end
function pokedex:action(msg, config)
local query = utilities.input_from_msg(msg)
if not query then
utilities.send_reply(msg, pokedex.doc, true)
return
end
utilities.send_typing(msg.chat.id, 'typing')
local text, id = pokedex:get_pkmn(query)
local image = 'http://pokeapi.co/media/img/'..id..'.png'
if not text then
utilities.send_reply(msg, config.errors.result)
return
end
utilities.send_typing(msg.chat.id, 'upload_photo')
utilities.send_photo(msg.chat.id, image, nil, msg.message_id)
utilities.send_reply(msg, text, 'HTML')
end
return pokedex