     if err
       log.error "Couldn't get token for #{team}: #{err}"
       return
+
     bot = bots[team] = controller.spawn(token: token)
-    bot.startRTM()
+    updateBotMetadata bot, team, (err) ->
+      if err
+        log.error "Couldn't update bot metadata: #{err}"
+        return
+      bot.startRTM()
 
 for file in fs.readdirSync process.env.DATA_DIR
   log.info "Found db file for #{file}"

      #log.error "Failed to reply: #{err}"
#for i in ['message_received', 'mention', 'direct_message', 'direct_mention', 'ambient']
  #controller.on i, handler(i)

handleMessage = (bot, channel, isDirect, message) ->
  team = bot.identifyTeam()
  bot.reply {
    channel: channel
  }, {
    text: ":star: channel=#{channel} isDirect=#{isDirect} message=#{message}"
  }

controller.on 'direct_mention', (bot, msg) ->
  return if msg.user is bot.identity?.id


# LOGIC -------------------------------------------------------------------------

handleMessage = (bot, channel, isDirect, message) ->
  team = bot.identifyTeam()

  bot.reply {
    channel: channel
  }, {
    text: ":star: channel=#{channel} isDirect=#{isDirect} message=#{message}"
  }

