Skip to content
This repository
Josh Nichols June 08, 2012
file 50 lines (40 sloc) 1.081 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
# Description:
# None
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot uptime - Outputs bot uptime
#
# Author:
# whitman

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)

  if elements.length > 1
    last = elements.pop()
    response = elements.join ', '
    response += ' and ' + last
  else
    response = elements.join ', '

  cb 'I\'ve been sentient for ' + response

Something went wrong with that request. Please try again.