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
topkecleon 60570e90f3 added cron jobs for plugins
added example plugin with documentation
added liberbot-compliant flood control
	see Liberbot Support for details on getting compliant
added Kickass Torrent plugin
various bugfixes
all files seem to have been marked changed due to a shift in platform
	I will do a clean clone and testing to ensure there is no issue.
2015-08-28 23:15:01 -07:00

85 lines
1.7 KiB
Lua
Executable File

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, config.locale.errors.connection)
end
jdat = JSON.decode(jstr)
if #jdat.data.children == 0 then
return send_msg(msg, config.locale.errors.results)
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, config.locale.errors.connection)
end
jdat = JSON.decode(jstr)
if #jdat.data.children == 0 then
return send_msg(msg, config.locale.errors.results)
end
end
else
url = 'https://www.reddit.com/.json'
local jstr, res = HTTP.request(url)
if res ~= 200 then
return send_msg(msg, config.locale.errors.connection)
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