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/konachan.lua

41 lines
1.2 KiB
Lua
Raw Normal View History

local konachan = {}
function konachan:init(config)
konachan.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('kc', true):t('konachan', true).table
konachan.doc = [[*
]]..config.cmd_pat..[[kc* _<Tags>_: Sucht auf Konachan mit diesen Tags]]
end
konachan.command = 'kc <Tags>'
function konachan:get_kc(tag)
local url = 'http://konachan.net/post.json?tags='..tag..'%20-rating:explicit'
local res, code = http.request(url)
if code ~= 200 then return nil end
local data = json.decode(res)
if not data[1] then return nil end
local i = math.random(#data)
local link_image = data[i].file_url
return link_image
end
function konachan:action(msg, config)
local input = utilities.input_from_msg(msg)
if not input then
2016-08-24 17:18:17 +02:00
utilities.send_reply(msg, konachan.doc, true)
return
end
local tag = string.gsub(input, " ", '+' )
local url = konachan:get_kc(tag)
if not url then
2016-08-24 17:18:17 +02:00
utilities.send_reply(msg, config.errors.results)
return
end
2016-08-24 17:18:17 +02:00
utilities.send_typing(msg.chat.id, 'upload_photo')
local file = download_to_file(url)
2016-08-24 17:18:17 +02:00
utilities.send_photo(msg.chat.id, file, nil, msg.message_id)
utilities.send_reply(msg, url)
end
return konachan