2015-11-25 03:22:04 +01:00
|
|
|
local doc = [[
|
2015-07-08 09:38:04 +02:00
|
|
|
/reddit [r/subreddit | query]
|
2015-11-25 03:22:04 +01:00
|
|
|
Returns the four (if group) or eight (if private message) top posts for the given subreddit or query, or from the frontpage.
|
2015-07-03 00:15:52 +02:00
|
|
|
]]
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local triggers = {
|
2016-01-08 04:30:12 +01:00
|
|
|
'^/reddit[@'..bot.username..']*',
|
|
|
|
'^/r[@'..bot.username..']*$',
|
|
|
|
'^/r[@'..bot.username..']* ',
|
2015-11-25 03:22:04 +01:00
|
|
|
'^/r/'
|
2015-07-03 00:15:52 +02:00
|
|
|
}
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local action = function(msg)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
msg.text_lower = msg.text_lower:gsub('/r/', '/r r/')
|
|
|
|
local input = msg.text_lower:input()
|
|
|
|
local url
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local limit = 4
|
|
|
|
if msg.chat.id == msg.from.id then
|
|
|
|
limit = 8
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
if input then
|
|
|
|
if input:match('^r/') then
|
|
|
|
url = 'http://www.reddit.com/' .. input .. '/.json?limit=' .. limit
|
2015-07-03 00:15:52 +02:00
|
|
|
else
|
2015-11-25 03:22:04 +01:00
|
|
|
url = 'http://www.reddit.com/search.json?q=' .. input .. '&limit=' .. limit
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
else
|
2015-11-25 03:22:04 +01:00
|
|
|
url = 'http://www.reddit.com/.json?limit=' .. limit
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local jstr, res = HTTP.request(url)
|
|
|
|
if res ~= 200 then
|
|
|
|
sendReply(msg, config.errors.connection)
|
|
|
|
return
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
if #jdat.data.children == 0 then
|
|
|
|
sendReply(msg, config.errors.results)
|
|
|
|
return
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local message = ''
|
|
|
|
for i,v in ipairs(jdat.data.children) do
|
|
|
|
if v.data.over_18 then
|
2015-07-03 00:15:52 +02:00
|
|
|
message = message .. '[NSFW] '
|
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
local long_url = '\n'
|
|
|
|
if not v.data.is_self then
|
|
|
|
long_url = '\n' .. v.data.url .. '\n'
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
local short_url = '[redd.it/' .. v.data.id .. '] '
|
|
|
|
message = message .. short_url .. v.data.title .. long_url
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
sendReply(msg, message)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
return {
|
|
|
|
action = action,
|
|
|
|
triggers = triggers,
|
|
|
|
doc = doc
|
|
|
|
}
|