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

51 lines
1.5 KiB
Lua
Raw Normal View History

local urbandictionary = {}
local HTTP = require('socket.http')
local URL = require('socket.url')
local JSON = require('dkjson')
2016-06-07 06:31:34 +02:00
local utilities = require('otouto.utilities')
urbandictionary.command = 'urbandictionary <query>'
2016-08-14 04:26:44 +02:00
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
2016-08-14 04:26:44 +02:00
urbandictionary.doc = [[
/urbandictionary <query>
Returns a definition from Urban Dictionary.
2016-08-14 04:26:44 +02:00
Aliases: /ud, /urban
]]
urbandictionary.doc = urbandictionary.doc:gsub('/', config.cmd_pat)
end
2015-07-03 00:15:52 +02:00
2016-05-27 02:26:30 +02:00
function urbandictionary:action(msg, config)
2016-08-14 04:26:44 +02:00
local input = utilities.input_from_msg(msg)
2015-07-03 00:15:52 +02:00
if not input then
2016-08-14 04:26:44 +02:00
utilities.send_reply(self, msg, urbandictionary.doc, true)
return
2015-07-03 00:15:52 +02:00
end
2016-08-14 04:26:44 +02:00
local url = urbandictionary.base_url .. URL.escape(input)
local jstr, code = HTTP.request(url)
if code ~= 200 then
utilities.send_reply(self, msg, config.errors.connection)
return
2015-07-03 00:15:52 +02:00
end
2016-08-14 04:26:44 +02:00
local data = JSON.decode(jstr)
local output
if data.result_type == 'no_results' then
output = config.errors.results
else
output = string.format('*%s*\n\n%s\n\n_%s_',
data.list[1].word:gsub('*', '*\\**'),
utilities.trim(utilities.md_escape(data.list[1].definition)),
utilities.trim((data.list[1].example or '')):gsub('_', '_\\__')
)
2015-07-03 00:15:52 +02:00
end
2016-08-14 04:26:44 +02:00
utilities.send_reply(self, msg, output, true)
2015-07-03 00:15:52 +02:00
end
return urbandictionary