Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #56 from whitman/uptime
Browse files Browse the repository at this point in the history
Add uptime.coffee
  • Loading branch information
atmos committed Oct 30, 2011
2 parents 9bf9ef4 + 2710ad8 commit 98073c0
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/scripts/uptime.coffee
@@ -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

0 comments on commit 98073c0

Please sign in to comment.