     else
       who = robot.brain.userForName who
 
-    userBadges = badges[who.id] ?= []
-
     if badgeName in badgeList
-      userBadges.push ":#{badgeName}:"
+      badges.add who, badgeName
       if who.name == msg.envelope.user.name
         msg.reply "congrats on earning the :#{badgeName}: badge!"
       else

    else
      who = robot.brain.userForName who

    userBadges = badges[who.id] = []
    you = if who? and who.name == msg.envelope.user.name then true else false
    whowhat = "#{if you then 'You have' else who.name + ' has'}"


]

module.exports = (robot) ->
  badges =
    add: (user, badgeName) ->
      userBadges = robot.brain.data.ingressBadges[user.id] ?= []
      userBadges.push ":#{badgeName}:"
    forUser: (user) ->
      robot.brain.data.ingressBadges[user.id] ?= []

  sayBadges = (a) ->
    badgeReq = for kind, amt of a
      Array(amt+1).join ":#{kind}:"

  robot.brain.on 'loaded', ->
    robot.brain.data.ingressBadges ?= {}

  robot.respond /AP\s+(?:to|(?:un)?til)\s+L?(\d{1,2})/i, (msg) ->
    [lv, lvl] = [msg.match[1], levels[msg.match[1]]]
    if lvl.badges?
      badgeReq = sayBadges lvl.badges
    msg.reply "You need #{lvl.ap} AP#{if badgeReq? then ' ' + badgeReq.join ' ' else ''}
 to reach L#{lv}#{if lv > 15 then ' (hang in there!)' else ''}"

  robot.respond /AP all/i, (msg) ->
    lvls = for lv, lvl of levels
