-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (76 loc) · 2.75 KB
/
server.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
const Discord = require('discord.js');
const client = new Discord.Client();
const dotenv = require('dotenv')
const https = require('https');
const mongoose = require('mongoose')
const ReplyController = require('./Controller/DiscordReplyController')
const DatabaseController = require('./Controller/DatabaseController')
dotenv.config();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
const messageSplitted = msg.content.replace(" "," ").split(" ")
if (messageSplitted[0] === '!git') {
if(messageSplitted[1] === 'user')
MakeRequest('user','/users/'+messageSplitted[2],msg,messageSplitted[2])
if(messageSplitted[1] === 'repos')
MakeRequest('repos','/users/'+messageSplitted[2]+'/repos',msg,messageSplitted[2],messageSplitted[3]?parseInt(messageSplitted[3]):1)
if(messageSplitted[1] === 'following'||messageSplitted[1]==='followers')
MakeRequest(messageSplitted[1],'/users/'+messageSplitted[2]+'/'+messageSplitted[1],msg,messageSplitted[2],messageSplitted[3]?parseInt(messageSplitted[3]):1)
if(messageSplitted[1] === 'me'){
if(messageSplitted[2] === 'username')
DatabaseController.AddUsername(messageSplitted[3],msg)
else if(messageSplitted[2] === 'resume')
DatabaseController.AddPortfolio(msg.content.trim().split(messageSplitted[2])[1],msg)
else if(messageSplitted[2] === 'repo')
DatabaseController.AddFavRepo(messageSplitted[3],msg)
else
DatabaseController.ShowDetails(msg)
}
}
});
const MakeRequest = (request,path,msg,user,page)=>{
const options = {
host: 'api.github.com',
path: path,
headers: {
"User-Agent" : "Disc"
}
}
https.get(options, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
if(request==='user')
ReplyController.UserDetails(JSON.parse(data),msg)
if(request==='repos'){
let repos = JSON.parse(data)
ReplyController.UserRepos(repos,user,msg,page<Math.ceil(Object.keys(repos).length/2)?page:Math.ceil(Object.keys(repos).length/2))
}
if(request==='following'||request==='followers'){
let followingusers = JSON.parse(data)
ReplyController.UserFollowing(followingusers,user,msg,page<Math.ceil(Object.keys(followingusers).length/2)?page:Math.ceil(Object.keys(followingusers).length/2),request)
}
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
const db = process.env.DATABASE.replace(
"<password>",
process.env.DATABASE_PASSWORD
);
mongoose
.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
})
.then(con => {
console.log("database connection sucessful");
client.login(process.env.Token);
});