-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSilverBot.js
316 lines (285 loc) · 12.8 KB
/
SilverBot.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
var Discord = require("discord.js");
var bot = new Discord.Client();
var prefix = "~";
var request = require("request")
var authinfo = require("./authinfo.json");
var unirest = require("unirest");
bot.on("message", msg => {
if(msg.author.bot) return;
if(!msg.content.startsWith(prefix)) return;
if(msg.channel.type === "text"){
console.log("group message sent in " + msg.channel.guild.name + "\n\"" + msg.content + "\"\n");
}else if(msg.channel.type === "dm"){
console.log("private message sent in " + msg.author.username + "\n\"" + msg.content + "\"\n");
}else{
return;
}
mes = msg.content.substring(1).trim().toLowerCase();
if(mes.startsWith("weather")){
var city = mes.substring(7).trim();
request({
url: "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&APPID=" + authinfo.keys.weather,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
var tempk = body.main.temp;
var tempc = Math.max( Math.round((tempk - 273.15) * 10) / 10, 2.8 ).toFixed(2);
var tempf = Math.max( Math.round((tempc * 9/5 + 32) * 10) / 10, 2.8 ).toFixed(2);
tempc * 9/5 + 32;
msg.channel.sendMessage("Weather in " + city + ":\n" +
body.weather[0].description + "\n" +
"temperature: " + tempf + "˚F/" + tempc + "˚C"); // Print the json response
}else{
msg.channel.sendMessage("Something went wrong. You may have typed the city name wrong or are sending too many requests.");
console.log("error: " + error + "\n response.statusCode: " + response.statusCode);
}
});
}
if(mes.startsWith("gif")){
topic = mes.substring(3).trim();
request({
url: "http://api.giphy.com/v1/gifs/search?q=" + topic + "&api_key=" + authinfo.keys.giphy,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200 && body.data.length !== 0) {
var randInt = -Math.floor(Math.random() * (0 - body.data.length + 1));
msg.channel.sendMessage(body.data[randInt].images.original.url);
}else if(body.data.length === 0){
msg.channel.sendMessage("No gifs found");
}else{
msg.channel.sendMessage("Something went wrong.");
console.log("error: " + error + "\n response.statusCode: " + response.statusCode);
}
});
}
if(mes.startsWith("shorten")){
var longURL = msg.content.substring(8).trim();
if(!longURL.startsWith("http")){
longURL = "http://" + longURL;
}
unirest.get("http://api.bit.ly/v3/shorten?format=json&login=" + authinfo.logins.bitly + "&apiKey=" + authinfo.keys.bitly + "&longUrl=" + longURL)
.header("Accept", "application/json")
.end(function (result) {
if(result.body.status_txt === "INVALID_URI"){
msg.sendMessage("The URL you entered is invalid.");
}else{
msg.channel.sendMessage(result.body.data.url);
}
});
}
if(mes.startsWith("expand")){
var shortURL = msg.content.substring(7).trim();
if(!shortURL.startsWith("http")){
shortURL = "http://" + shortURL;
}
unirest.get("http://api.bit.ly/v3/expand?format=json&login=" + authinfo.logins.bitly + "&apiKey=" + authinfo.keys.bitly + "&shortUrl=" + shortURL)
.header("Accept", "application/json")
.end(function (result) {
if(result.body.data.expand[0].hasOwnProperty("error")){
msg.channel.sendMessage("The URL you entered is invalid. Make sure it starts with bit.ly, or j.mp")
}else{
msg.channel.sendMessage(result.body.data.expand[0].long_url);
}
});
}
if(mes.startsWith("echo")){
msg.channel.sendMessage(msg.content.substring(5).trim());
}
if(mes.startsWith("urban")){
topic = mes.substring(5).trim();
unirest.get("https://mashape-community-urban-dictionary.p.mashape.com/define?term=" + topic)
.header("X-Mashape-Key", authinfo.keys.mashape)
.header("Accept", "text/plain")
.end(function (result) {
if(result.body.list.length !== 0){
var index = -Math.floor(Math.random() * (0 - result.body.list.length + 1));
msg.channel.sendMessage("**" + result.body.list[index].word + "** by " + result.body.list[index].author + "\n\n" +
result.body.list[index].definition + "\n\n" +
"*" + result.body.list[index].example + "*\n\n" +
":thumbsup:" + result.body.list[index].thumbs_up +
" : " + ":thumbsdown:" + result.body.list[index].thumbs_down);
}else{
msg.channel.sendMessage("No results found :(");
}
});
}
if(mes.startsWith("movie")){
var titleAndYear = mes.substring(5).trim();
var params = titleAndYear.split("|")
for (var i = params.length - 1; i >= 0; i--) {
params[i] - params[i].length;
};
unirest.get("http://www.omdbapi.com/?t=" + params[0] + "&y=" + params[1] + "&apikey=" + authinfo.keys.omdb)
.end(function(result){
if(result.body.Response === "True"){
var poster;
if(result.body.Poster === "N/A"){
poster = "";
}else{
poster = result.body.Poster;
}
const embed = new Discord.RichEmbed()
.setTitle("**" + result.body.Title + "** (" + result.body.Year + ")")
.setAuthor('Silver Bot', bot.user.avatarURL)
.setColor(0x00AE86)
.setDescription(result.body.Plot)
.setFooter('Use ~help for help', bot.user.avatarURL)
.setImage(poster)
.setTimestamp()
.setURL('http://www.imdb.com/title/' + result.body.imdbID)
.addField('Director', result.body.Director, true)
.addField('Ratings', "Metascore: " + result.body.Metascore + "\nIMDB: " + result.body.imdbRating, true)
.addField('Writer(s)', result.body.Writer, true)
.addField('Cast', result.body.Actors, true)
msg.channel.sendEmbed(
embed,
{ disableEveryone: true }
);
}else{
msg.channel.sendMessage("Movie not found :(");
}
});
}
if(mes.startsWith("meme")){
if(mes.substring(4).trim() === "list"){
msg.channel.sendMessage("List of background meme images:\n"
+ "```one does not simply\n"
+ "batman slapping robin\n"
+ "the most interesting \n"
+ "man in the world\n"
+ "ancient aliens\n"
+ "futurama fry\n"
+ "first world problems\n"
+ "bad luck brian\n"
+ "doge\n"
+ "grumpy cat\n"
+ "y u no```");
}else{
var params = mes.substring(4).split("|");
if(params.length == 3){
for (var i = params.length - 1; i >= 0; i--) {
params[i] = params[i].trim();
};
var img = params[0];
var templateID
var matchingID = true;
switch(img){
case "one does not simply":
templateID = 61579;
break;
case "batman slapping robin":
templateID = 438680;
break;
case "the most interesting man in the world":
templateID = 61532;
break;
case "ancient aliens":
templateID = 101470;
break;
case "futurama fry":
templateID = 61520;
break;
case "first world problems":
templateID = 61539;
break;
case "bad luck brian":
templateID = 61585;
break;
case "doge":
templateID = 8072285;
break;
case "grumpy cat":
templateID = 405658;
break;
case "y u no":
templateID = 61527;
break;
default:
matchingID = false;
break;
}
if(matchingID){
var formData = {
template_id : templateID,
username : authinfo.logins.imgflip.username,
password : authinfo.logins.imgflip.password,
text0 : params[1],
text1 : params[2]
};
request.post("https://api.imgflip.com/caption_image", {
form : formData
}, function(error, response, body) {
var meme = JSON.parse(body);
if (!error && response.statusCode == 200) {
msg.channel.sendMessage(meme.data.url);
}else{
msg.channel.sendMessage("Something went wrong.")
}
});
}else{
msg.channel.sendMessage("Image not found. Type ```~meme list``` to see a list of available images.");
}
}else{
msg.channel.sendMessage("Incorrect amount of parameters. Make sure to format it as\n```~meme <image> | <top text> | <bottom text>```");
}
}
}
if(mes === "fat"){
msg.channel.sendMessage("esdesign");
}
if(mes === "uptime"){
msg.channel.sendMessage(bot.uptime / 1000 + "s");
}
if(mes === "rtd"){
msg.channel.sendMessage(msg.author.username + " rolled a " + Math.floor(Math.random() * (6 - 1 + 1) + 1));
}
if(mes === "help"){
msg.channel.sendMessage("```SilverBot was created by arocks124#9318\n"
+ "to use, type ~ <command>\n" +
+ "Commands:\n"
+ "fat\n"
+ "uptime\n"
+ "rtd\n"
+ "weather (~weather <city>)\n"
+ "gif (~gif <topic>)\n"
+ "urban (~urban <topic>)\n"
+ "echo (~echo <phrase>)\n"
+ "shorten (~shorten <link>)\n"
+ "expand (~expand <bit.ly or j.mp link>)\n"
+ "movie (~movie <title> | <year (optional)>)\n"
+ "meme (~meme <image> | <top text> | <bottom text>) (type ~meme list for list of available memes)\n"
+ "\n"
+ "Thanks for using SilverBot!```");
}
if(mes === "git"){
msg.channel.sendMessage("GitHub: https://github.com/awarren124/Silver-Bot");
}
if(mes.startsWith("info")){
for (var i = 0; i <msg.mentions.users.array().length; i++) {
user = msg.mentions.users.array()[i];
console.log(user.createdAt);
year = user.createdAt.toString().substring(0, 4);
month = user.createdAt.toString().substring(5, 7);
day = user.createdAt.toString().substring(8, 10);
const embed = new Discord.RichEmbed()
.setTitle(user.username + "#" + user.discriminator)
.setThumbnail(user.avatarURL)
.setColor(0x00AE86)
.addField('Created on: ' + month + "/" + day + "/" + year)
.setFooter('Use ~help for help', bot.user.avatarURL)
.setTimestamp()
msg.channel.sendEmbed(
embed,
{ disableEveryone: true }
);
}
}
});
bot.on("guildMemberAdd", (member) => {
member.guild.defaultChannel.sendMessage(`"${member.user.username}" has joined this server`);
});
bot.on('ready', () => {
console.log('I am ready!');
bot.user.setGame("Type ~help for info");
});
bot.login(authinfo.keys.discord);