2016-04-11 06:04:47 +02:00
|
|
|
local urbandictionary = {}
|
|
|
|
|
|
|
|
urbandictionary.command = 'urbandictionary <query>'
|
2016-05-27 05:28:44 +02:00
|
|
|
|
|
|
|
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 = [[```
|
|
|
|
]]..config.cmd_pat..[[urbandictionary <query>
|
2016-01-08 14:44:37 +01:00
|
|
|
Returns a definition from Urban Dictionary.
|
2016-05-27 05:28:44 +02:00
|
|
|
Aliases: ]]..config.cmd_pat..[[ud, ]]..config.cmd_pat..[[urban
|
2016-01-08 14:44:37 +01:00
|
|
|
```]]
|
2016-04-11 06:04:47 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function urbandictionary:action(msg, config)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
local input = utilities.input(msg.text)
|
2015-07-03 00:15:52 +02:00
|
|
|
if not input then
|
2015-11-25 03:22:04 +01:00
|
|
|
if msg.reply_to_message and msg.reply_to_message.text then
|
|
|
|
input = msg.reply_to_message.text
|
2015-07-29 00:13:46 +02:00
|
|
|
else
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, urbandictionary.doc, true, msg.message_id, true)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-07-29 00:13:46 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local url = 'http://api.urbandictionary.com/v0/define?term=' .. URL.escape(input)
|
|
|
|
|
2016-08-01 21:07:27 +02:00
|
|
|
local jstr, res = http.request(url)
|
2015-07-03 00:15:52 +02:00
|
|
|
if res ~= 200 then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2016-08-01 21:07:27 +02:00
|
|
|
local jdat = json.decode(jstr)
|
2015-07-03 00:15:52 +02:00
|
|
|
if jdat.result_type == "no_results" then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2016-08-04 13:44:56 +02:00
|
|
|
local output = '<b>' .. jdat.list[1].word .. '</b>\n' .. utilities.trim(jdat.list[1].definition)
|
2015-07-03 00:15:52 +02:00
|
|
|
if string.len(jdat.list[1].example) > 0 then
|
2016-08-04 13:44:56 +02:00
|
|
|
output = output .. '<i>\n' .. utilities.trim(jdat.list[1].example) .. '</i>'
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2016-08-04 13:44:56 +02:00
|
|
|
|
2016-01-08 14:44:37 +01:00
|
|
|
output = output:gsub('%[', ''):gsub('%]', '')
|
|
|
|
|
2016-08-04 13:44:56 +02:00
|
|
|
utilities.send_reply(self, msg, output, 'HTML')
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-08-04 13:44:56 +02:00
|
|
|
return urbandictionary
|