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

43 lines
893 B
Lua
Raw Normal View History

2015-07-03 00:15:52 +02:00
local PLUGIN = {}
PLUGIN.doc = [[
2015-07-08 09:38:04 +02:00
/ud <term>
2015-07-03 00:15:52 +02:00
Returns the first definition for a given term from Urban Dictionary.
]]
PLUGIN.triggers = {
2015-07-08 09:38:04 +02:00
'^/ud',
'^/urbandictionary'
2015-07-03 00:15:52 +02:00
}
function PLUGIN.action(msg)
local input = get_input(msg.text)
if not input then
return send_msg(msg, PLUGIN.doc)
end
local url = 'http://api.urbandictionary.com/v0/define?term=' .. URL.escape(input)
local jstr, res = HTTP.request(url)
if res ~= 200 then
return send_msg(msg, config.locale.errors.connection)
2015-07-03 00:15:52 +02:00
end
local jdat = JSON.decode(jstr)
if jdat.result_type == "no_results" then
return send_msg(msg, config.locale.errors.results)
2015-07-03 00:15:52 +02:00
end
message = '"' .. jdat.list[1].word .. '"\n' .. trim_string(jdat.list[1].definition)
if string.len(jdat.list[1].example) > 0 then
message = message .. '\n\nExample:\n' .. trim_string(jdat.list[1].example)
end
send_msg(msg, message)
end
return PLUGIN