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/plugins/urban_dictionary.lua

41 lines
1.1 KiB
Lua
Raw Normal View History

2015-04-19 21:18:06 +02:00
function getUrbanDictionary(text)
local topic = string.match(text, "/ud (.+)") or string.match(text, "http://([A-Za-z0-9-_-]+).urbanup.com/")
2015-04-19 21:18:06 +02:00
topic = url_encode(topic)
b = http.request("http://api.urbandictionary.com/v0/define?term=" .. topic)
res = json:decode(b)
local definition = nil
if #res.list > 0 then
definition = res.list[1].word..": "..res.list[1].definition.."\n".. res.list[1].permalink
else
definition = nil
end
return definition
end
function url_encode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w %-%_%.%~])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
function run(msg, matches)
local text = getUrbanDictionary(msg.text)
if (text == nil) then
2015-04-20 18:52:25 +02:00
return "Nichts gefunden!"
2015-04-19 21:18:06 +02:00
else
return text
end
end
return {
2015-04-20 18:52:25 +02:00
description = "Zeigt eine Urban Dictionary Definition",
2015-04-28 17:49:11 +02:00
usage = {"/ud [Begriff]"},
patterns = {"^/ud (.*)$",
"^http://([A-Za-z0-9-_-]+).urbanup.com/"
},
2015-04-19 21:18:06 +02:00
run = run
}