-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatter.js
130 lines (96 loc) · 3.09 KB
/
chatter.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict'
const request = require('request')
const defineUrl = "http://api.urbandictionary.com/v0/define?term=";
const randomUrl = "http://api.urbandictionary.com/v0/random";
const wordRegExp = /\w{5,}/gi;
const thoughts = [
"Hey guys! Listen to this...",
"I've got one!",
"I spy with my little brown eye...",
"My turn!",
":wink:",
":point_right: :ok_hand:",
"Scraping the bottom of the barrel...",
"Packing fudge...",
"Choking the chicken...",
"Polishing my helmet..."
];
module.exports = (slapp) => {
let searching = false
slapp.command('/urban', (msg) => {
if(msg.body.text == "?") {
msg.respond(`Just type a phrase after *"/urban"*, idiot!`);
return false;
} else if(msg.body.text.length<2) {
msg.respond(`Use both hands and type properly, faggot!`);
return false;
}
let random = false, thinkingPhrase;
if(searching) {
msg.respond("Shut up! I'm busy!");
return false;
} else {
searching = true;
if(msg.body.text.toLowerCase().indexOf('random')>=0) {
random = true;
thinkingPhrase = thoughts[Math.floor(Math.random()*thoughts.length)];
} else {
thinkingPhrase = `Do you guys know what ${msg.body.text} is?...`;
}
}
let thinkingTimeout = setTimeout(()=>{
msg.say(thinkingPhrase);
},500);
request({
url: random?randomUrl:(defineUrl+encodeURIComponent(msg.body.text)),
json: true
}, (error,response,body)=>{
searching = false;
clearTimeout(thinkingTimeout);
if(!error && response.statusCode === 200) {
try {
if(body.list.length<1) {
msg.say(`I don't know what the fuck "${msg.body.text}" is!`);
} else {
var index = Math.floor(Math.random()*body.list.length);
msg.say(`*${body.list[index].word}*: ${body.list[index].definition}`);
}
} catch(e) {
console.log(e.message);
}
}
})
})
slapp.message('.*', (msg) => {
let words = msg.body.event.text.match(wordRegExp),
index;
if(!words) {
return false;
}
index = Math.floor(Math.random()*words.length);
if(Math.random()<0.12&&!searching&&words.length>0) {
searching = true;
request({
url: defineUrl+encodeURIComponent(words[index].toLowerCase()),
json: true
}, (error,response,body)=>{
searching = false;
if(!error && response.statusCode === 200 && body.list.length>0) {
try {
let quoteMiner = /(?:"[^"]*"|^[^"]*$)/gi,
exampleIndex = Math.floor(Math.random()*body.list.length),
quotes = body.list[exampleIndex].example.match(quoteMiner),
quoteIndex = Math.floor(Math.random()*quotes.length);
if(quotes.length>0) {
msg.say(quotes[quoteIndex].replace(/"/g, ""));
} else {
msg.say(body.list[exampleIndex].example);
}
} catch(e) {
console.log(e.message);
}
}
})
}
})
}