forked from oftn-oswg/oftn-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashweb.js
More file actions
91 lines (78 loc) · 3.31 KB
/
hashweb.js
File metadata and controls
91 lines (78 loc) · 3.31 KB
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
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'));
function callStats(user, callback) {
request(userUrl + user, function(error, response, body) {
if (!error && response.statusCode == 200) {
var response = JSON.parse(body)
callback(response);
}
})
}
function isAuth(host) {
for (var i=0;i < config.users.length ; i++) {
if (config.users[i].host === host) {
return true
}
}
return false;
}
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!");
}
}
}