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/reddit.lua
2015-07-02 18:15:52 -04:00

85 lines
1.7 KiB
Lua

local PLUGIN = {}
PLUGIN.doc = [[
!reddit [r/subreddit | query]
This command returns top results for a given query or subreddit. NSFW posts are marked as such.
]]
PLUGIN.triggers = {
'^!reddit',
'^!r$',
'^!r '
}
function PLUGIN.action(msg)
local input = get_input(msg.text)
local jdat = {}
local message = ''
if input then
if string.match(input, '^r/') then
local url = 'http://www.reddit.com/' .. first_word(input) .. '/.json'
local jstr, res = HTTP.request(url)
if res ~= 200 then
return send_msg(msg, 'Connection error.')
end
jdat = JSON.decode(jstr)
if #jdat.data.children == 0 then
return send_msg(msg, 'Subreddit not found.')
end
else
local url = 'http://www.reddit.com/search.json?q=' .. URL.escape(input)
local jstr, res = HTTP.request(url)
if res ~= 200 then
return send_msg(msg, 'Connection error.')
end
jdat = JSON.decode(jstr)
if #jdat.data.children == 0 then
return send_msg(msg, 'No results found.')
end
end
else
url = 'https://www.reddit.com/.json'
local jstr, res = HTTP.request(url)
if res ~= 200 then
return send_msg(msg, 'Connection error.')
end
jdat = JSON.decode(jstr)
end
local limit = 4
if #jdat.data.children < limit then
limit = #jdat.data.children
end
for i = 1, limit do
if jdat.data.children[i].data.over_18 then
message = message .. '[NSFW] '
end
url = '\n'
if not jdat.data.children[i].data.is_self then
url = '\n' .. jdat.data.children[i].data.url .. '\n'
end
local short_url = '[redd.it/' .. jdat.data.children[i].data.id .. '] '
message = message .. short_url .. jdat.data.children[i].data.title .. url
end
send_msg(msg, message)
end
return PLUGIN