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 9070a44c8f Improved triggers for many plugins.
Redone README to match the website.
Bot now supports deep linking / start payloads.
lastfm.lua now notifies a user when his default username is saved.
antisquig is now part of moderation.lua.
2016-01-07 22:30:12 -05:00

68 lines
1.4 KiB
Lua
Executable File

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