forked from oftn-oswg/oftn-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashweb.js
155 lines (132 loc) · 5.85 KB
/
hashweb.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
var http = require("http");
var fs = require("fs");
var request = require("request");
var moment = require("moment");
var userUrl = "http://hashweb.org/api/stats/users/";
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
var karmaUsers = [];
function callStats(user, callback) {
request(userUrl + user, function(error, response, body) {
if (!error && response.statusCode == 200) {
var response = JSON.parse(body)
callback(response);
}
})
}
function addKarma(user, callback) {
request.post({url: 'http://hashweb.org/api/stats/users/'+ user +'/addkarma', form:{ username: user, points: 1 }}, callback)
}
function isAuth(host) {
for (var i=0;i < config.users.length ; i++) {
if (config.users[i].host === host) {
return true
}
}
return false;
}
// this will remove people from the karma list after a period of time
function karmaChecker() {
console.log('KARMA CHECKER RUNNING');
for (i in karmaUsers) {
if ((new Date() - karmaUsers[i]) > 86400000) { // 24 hours
console.log('removing ' + i);
delete karmaUsers[i]; // remove them from the list so they can add karma again
}
}
}
module.exports = {
// Use the Hashweb API to get the last user seen
getLastSeen: function(context, username) {
callStats(username, function(data) {
var msg = "";
days = (data.userNotSeenFor.days) ? data.userNotSeenFor.days + " days " : "";
hours = (data.userNotSeenFor.hours) ? data.userNotSeenFor.hours + " hours " : "";
minutes = (data.userNotSeenFor.minutes) ? data.userNotSeenFor.minutes + " minutes" : "";
msg = msg + data.username + " was last seen in #web " + days + hours + minutes + " ago: <" + data.username + "> " + data.lastSeen.message;
context.channel.send_reply(context.sender, msg);
});
},
// Use the config file to get the list of ops
ops: function(context, username) {
context.channel.send_reply(context.sender, config.ops.join(' '));
},
getFirstSeen: function(context, username) {
callStats(username, function(data) {
var today = moment(),
joinedDate = moment(data.firstSeen.timestamp),
joinedDateString = joinedDate.format("dddd, MMMM Do YYYY");
var msg = data.username + " was first seen here " + joinedDate.from(today) + " (" + joinedDateString + "): ";
msg = msg + "<" + data.username + ">" + " " + data.firstSeen.message;
context.channel.send_reply(context.sender, msg);
});
},
modifyBansObject: function(context, bansText) {
if (isAuth(context.intent.host)) {
bansText = bansText.trim();
id = bansText.match(/^\d+/)[0]
key = bansText.match(/\:(\w*)/)[1]
value = bansText.match(/^\d+\:\w+\s(.+)/)[1]
bansObject = {}
if (key === "reason") {
bansObject.reason = value
}
if (key === "reminderTime") {
bansObject.reminderTime = value
}
request.post("http://hashweb.org/stats/bans/" + id, {form:bansObject}, function(err,httpResponse,body) {
context.channel.send_reply(context.sender, JSON.parse(body).message)
});
} else {
context.channel.send_reply(context.sender, "Oops, looks like you're not authorized!");
}
},
updateBansList: function(context, bansText) {
if (isAuth(context.intent.host)) {
request.post("http://hashweb.org/stats/bans/update", function(err,httpResponse,body) {
context.channel.send_reply(context.sender, JSON.parse(body).message);
});
} else {
context.channel.send_reply(context.sender, "Oops, looks like you're not authorized!");
}
},
giveKarma: function(context, user) {
karmaChecker();
if (context.intent.host in karmaUsers) { // users were changing their names then adding karma again, try using host instead
context.channel.send_reply(context.sender, "Sorry, looks like you've already used your karma allowance for now, try again later");
return
}
if (context.intent.name.toLowerCase() === user.toLowerCase()) {
context.channel.send_reply(context.sender, "You cannot give Karma points to yourself!");
return
}
addKarma(user, function(err, rsp, body) {
var response = JSON.parse(body);
response.statusCode = parseInt(response.statusCode);
if (response.statusCode === 200) {
context.channel.send_reply(context.sender, response.response);
karmaUsers[context.intent.host] = new Date(); // store the time when they added karma, we can use it later
} else if (response.statusCode === 404) {
context.channel.send_reply(context.sender, "oops, that user doesn't seem to exist right now");
} else {
context.channel.send_reply(context.sender, "oops, Karma service is down for now");
}
});
},
karmaHelp: function(context) {
context.channel.send_reply(context.sender, "Every user has Karma points, these can be gained by helping people or having someone give you karma with !giveKarma [user], you can check other user's karma with !karmaLevel [user], or your own with just !karma");
},
karma: function(context, help) {
if (typeof help !== "undefined" && help === "help") {
module.exports.karmaHelp(context)
return
}
callStats(context.intent.name, function(data) {
context.channel.send_reply(context.sender, "Your karma level is: " + data.karma);
});
},
karmaLevel: function(context, user) {
callStats(user, function(data) {
context.channel.send_reply(context.sender, user + "'s karma level is: " + data.karma);
});
}
}