     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}"


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


        if err then return cb "Couldn't update metadata for team #{team}: #{err}"
        log.info "Set metadata key #{key} for team #{team} with existing table"
        return cb null

updateBotMetadata = (bot, team, cb) ->
  db = getDatabase team
  if not db? then return cb "Couldn't get database for team #{team}"

  bot.mbMeta ?= {}
  db.all "SELECT key, value FROM metadata", (err, rows) ->
    if err then return cb "Couldn't get metadata for team #{team}: #{err}"
    for {key, value} in rows
      bot.mbMeta[key] = value
    return cb null


# BOTS --------------------------------------------------------------------------

