forked from upnextfm/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlast_seen.js
48 lines (42 loc) · 1.33 KB
/
last_seen.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
/**
* Script: Last Seen
* Version: 1.0
* Author: headzoo
*
* Creates a /seen command which displays the last time a user was seen in
* the lobby. For example "/seen headzoo".
*/
(function() {
if ($channel.name != "lobby") {
return;
}
var seen = {};
var regex = new RegExp('^/seen\\s+([^\\s]+)');
// Save the data to the database every 30 seconds.
setInterval(function() {
$store.database.set("last_seen", seen);
}, 30000);
$store.database.get("last_seen", {}, function(err, data) {
if (typeof data !== "object" || data == null) {
data = {};
}
seen = data;
});
$api.on("receive", function(e, data) {
seen[data.username.toLowerCase()] = Date.now();
var matches = data.msg_clean.match(regex);
if (matches !== null) {
var username = matches[1].toLowerCase();
if (seen[username] !== undefined) {
var date = new Date(seen[username]);
$api.send(sprintf(
"[#FFFFFF]%s was last seen on %s[/#]",
matches[1],
date.toString()
));
} else {
$api.send("[#FFFFFF]" + matches[1] + " has not been seen in a while.[/#]");
}
}
});
})();