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/otouto/plugins/urbandictionary.lua
topkecleon 9f760114bd otouto 3.14
All help messages and many other things moved from markdown to html.
Eventually, I'd like only things made from user input to use markdown.

cats.lua, rmspic.lua, and dilbert.lua moved to sendPhoto with URL.
xkcd.lua and apod.lua not moved to retain formatting.

Probably a lot of other stuff that I forget about. I should commit more often.
2016-10-04 10:07:15 -04:00

49 lines
1.6 KiB
Lua

local urbandictionary = {}
local HTTP = require('socket.http')
local URL = require('socket.url')
local JSON = require('dkjson')
local utilities = require('otouto.utilities')
urbandictionary.command = 'urbandictionary <query>'
urbandictionary.base_url = 'http://api.urbandictionary.com/v0/define?term='
function urbandictionary:init(config)
urbandictionary.triggers = utilities.triggers(self.info.username, config.cmd_pat)
:t('urbandictionary', true):t('ud', true):t('urban', true).table
urbandictionary.doc = [[/urbandictionary <query>
Returns a definition from Urban Dictionary.
Aliases: /ud, /urban]]
urbandictionary.doc = urbandictionary.doc:gsub('/', config.cmd_pat)
end
function urbandictionary:action(msg, config)
local input = utilities.input_from_msg(msg)
if not input then
utilities.send_reply(msg, urbandictionary.doc, 'html')
return
end
local url = urbandictionary.base_url .. URL.escape(input)
local jstr, code = HTTP.request(url)
if code ~= 200 then
utilities.send_reply(msg, config.errors.connection)
return
end
local data = JSON.decode(jstr)
local output
if data.result_type == 'no_results' then
output = config.errors.results
else
output = string.format('<b>%s</b>\n\n%s\n\n<i>%s</i>',
utilities.html_escape(data.list[1].word),
utilities.trim(utilities.html_escape(data.list[1].definition)),
utilities.trim(utilities.html_escape(data.list[1].example or ''))
)
end
utilities.send_reply(msg, output, 'html')
end
return urbandictionary