This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from whitman/uptime
Add uptime.coffee
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Outputs bot uptime | ||
|
||
module.exports = (robot) -> | ||
|
||
start = new Date().getTime() | ||
|
||
robot.respond /uptime/i, (msg) -> | ||
uptimeMe msg, start, (uptime) -> | ||
msg.send uptime | ||
|
||
numPlural = (num) -> | ||
if num != 1 then 's' else '' | ||
|
||
uptimeMe = (msg, start, cb) -> | ||
now = new Date().getTime() | ||
uptime_seconds = Math.floor((now - start) / 1000) | ||
intervals = {} | ||
intervals.day = Math.floor(uptime_seconds / 86400) | ||
intervals.hour = Math.floor((uptime_seconds % 86400) / 3600) | ||
intervals.minute = Math.floor(((uptime_seconds % 86400) % 3600) / 60) | ||
intervals.second = ((uptime_seconds % 86400) % 3600) % 60 | ||
|
||
elements = [] | ||
for own interval, value of intervals | ||
if value > 0 | ||
elements.push value + ' ' + interval + numPlural(value) | ||
|
||
last | ||
if elements.length > 1 | ||
last = elements.pop() | ||
response = elements.join ', ' | ||
response += ' and ' + last if last | ||
else | ||
response = elements.join ', ' | ||
|
||
cb 'I\'ve been sentient for ' + response | ||
|