-
Notifications
You must be signed in to change notification settings - Fork 3
/
ud.jsm
85 lines (75 loc) · 2.07 KB
/
ud.jsm
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// -*- Mode: JavaScript; tab-width: 4 -*- vim:tabstop=4 syntax=javascript:
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*global module: false */
module.version = 1.4;
module.BASE_URL = "http://api.urbandictionary.com/v0/";
module.DEFINE_BASE_URL = module.BASE_URL + "define?term=";
module.defToText = function defToText(def, field) {
var text = def[field || "definition"].trim().replace(/\[([^\[\]]+)\]/g, "$1").replace("\r\n", "\n", "g");
if (text.length > 350) {
text = text.split("\n")[0];
if (text.length > 350) {
// guys pls
var l = text.lastIndexOf(". ", 350);
if (l != -1) {
// I give up...
l = 350;
}
text = text.slice(0, l);
}
text += " ...";
}
return "{0} {1}".format(text, def.permalink);
};
module.cmd_ud = module.cmd_urban =
function cmd_ud(e) {
var term = e.args;
if (!term) {
e.reply(this.cmd_ud.help);
return true;
}
var data;
try {
// WHY URBAN DICTIONARY? WHY!?!?!?!?!??!?!?
data = JSON.parse(e.bot.readURI(this.DEFINE_BASE_URL + encodeURIComponent(term)));
} catch (ex) {}
if (!data) {
e.reply("Urban Dictionary returned no data.");
return true;
}
var def0 = data.list[0];
if (def0) {
e.send(this.defToText(def0));
} else {
e.reply("No results.");
}
return true;
};
module.cmd_ud.help = "Search Urban Dictionary. Usage: ud <term>";
module.cmd_urbanex =
function cmd_urbanex(e) {
var term = e.args;
if (!term) {
e.reply(this.cmd_ud.help);
return true;
}
var data;
try {
// seriously guys, fix the goddamn API server.
data = JSON.parse(e.bot.readURI(this.DEFINE_BASE_URL + encodeURIComponent(term)));
} catch (ex) {}
if (!data) {
e.reply("Urban Dictionary returned no data.");
return true;
}
var def0 = data.list[0];
if (def0) {
e.send(this.defToText(def0, "example"));
} else {
e.reply("No results.");
}
return true;
};
module.cmd_urbanex.help = "Get the top Urban Dictionary definition's example for a word. Usage: urbanex <term>";