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

68 lines
2.0 KiB
Lua
Raw Normal View History

local yandere = {}
function yandere:init(config)
yandere.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('yan', true):t('yandere', true).table
yandere.doc = [[*
]]..config.cmd_pat..[[yan* _<Tags>_: Sucht auf Yandere mit diesen Tags]]
end
yandere.command = 'yan <Tags>'
function yandere:get_post(tag)
local BASE_URL = 'https://yande.re'
local url = BASE_URL..'/post.json?tags='..tag
local b,c = https.request(url)
if c ~= 200 then return nil end
local yan = json.decode(b)
if not yan[1] then return nil end
local i = math.random(#yan)
local url = yan[i].file_url
local resolution = yan[i].jpeg_width..'x'..yan[i].jpeg_height..'px'
local size = ' '..math.ceil(yan[i].file_size/1000)
local source_url = '\nhttps://yande.re/post/show/'..yan[i].id
local text = resolution..size..'kb'..source_url
return url, size, text
end
function yandere: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, yandere.doc, true)
return
end
local tag = string.gsub(input, " ", '_' )
local tag = string.gsub(input, ":", '%%3A')
local tag = string.gsub(input, "+", '%%20')
local url, size, text = yandere:get_post(tag)
print('URL: '..url)
-- don't send GIFs when they're bigger than 20 MB
-- don't send photos when they're bigger than 5 MB
if string.ends(url, ".gif") then
if tonumber(size) > 19900 then
utilities.send_reply(msg, 'Sorry, die GIF ist zu groß.\n'..text)
return
end
else
if tonumber(size) > 4900 then
utilities.send_reply(msg, 'Sorry, das Bild ist zu groß.\n'..text)
return
end
end
if not url then
utilities.send_reply(msg, 'Nobody here but us chickens!')
return
end
2016-08-24 17:18:17 +02:00
utilities.send_typing(msg.chat.id, 'upload_photo')
if string.ends(url, ".gif") then
utilities.send_document(msg.chat.id, url, text, msg.message_id)
else
utilities.send_photo(msg.chat.id, url, text, msg.message_id)
end
end
return yandere