-
Notifications
You must be signed in to change notification settings - Fork 27
/
urban_dictionary.js
54 lines (43 loc) · 1.43 KB
/
urban_dictionary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var request = require("request");
var UrbanDictionaryPlugin = function (data) {
var that = this;
that.bot = data.bot;
that.help = {
"/urban word": "Get the urban dictionary definition of a word"
};
function getNthDefinition(word, n) {
request.get({
url: "https://api.urbandictionary.com/v0/define?term=" + word,
json: true
}, function (err, resp) {
if (err) return that.bot.emit("error", err);
var str = "No data.";
if (resp.body && resp.body.list && resp.body.list[n]) {
str = word + ': ' + resp.body.list[n].definition + "\n";
str += "Example: " + resp.body.list[n].example;
}
else {
str = "There aren't enough definitions."
}
that.bot.emit("do:commandResponseExpandable", str, {
htmlMessage: str.split("\n").join("<br/><br/>")
});
});
}
function getDefinition(word) {
return getNthDefinition(word, 0);
}
that.commands = {
"/urban": function (input, user) {
if (input) {
getDefinition(input);
}
else {
response = 'Use "/urban word" to get a definition.';
that.bot.emit("do:commandResponsePM", response, [user]);
}
}
};
return that;
};
module.exports = UrbanDictionaryPlugin;