diff --git a/README.md b/README.md index d537a2e..964d2d8 100644 --- a/README.md +++ b/README.md @@ -32,13 +32,24 @@ as a comma separated list of usernames. ## Commands - hubot team +1 # add me to the team - hubot team -1 # remove me from the team - hubot team add (me|) # add me or to team - hubot team remove (me|) # remove me or from team - hubot team (list|show) # list the people in the team - hubot team (new|empty|clear) # clear team list - hubot team count # list the current size of the team + hubot create team # create team called + hubot (delete|remove) team # delete team called + hubot list teams # list all existing teams + hubot () team +1 # add me to the team + hubot () team -1 # remove me from the team + hubot () team add (me|) # add me or to team + hubot () team remove (me|) # remove me or from team + hubot () team (list|show) # list the people in the team + hubot () team (empty|clear) # clear team list + hubot () team count # list the current size of the team + +All commands that have the `` in parantheses can ommit it. For example: + + hubot team +1 + +would work just fine, adding the current user to the default team. Note: when +adding and removing users without the `` label, those users are +included in a team that does not show up when running `hubot list teams`. ## Contributing diff --git a/src/team.coffee b/src/team.coffee index 11b007c..54dc957 100644 --- a/src/team.coffee +++ b/src/team.coffee @@ -8,21 +8,23 @@ # HUBOT_TEAM_ADMIN - A comma separate list of user names # # Commands: -# hubot create team - add me to the team +# hubot create team - create team called +# hubot (delete|remove) team - delete team called # hubot list teams - list all existing teams -# hubot team +1 - add me to the team -# hubot team -1 - remove me from the team -# hubot team add (me|) - add me or to team -# hubot team remove (me|) - remove me or from team -# hubot team count - list the current size of the team -# hubot team (list|show) - list the people in the team -# hubot team (new|empty|clear) - clear team list +# hubot () team +1 - add me to the team +# hubot () team -1 - remove me from the team +# hubot () team add (me|) - add me or to team +# hubot () team remove (me|) - remove me or from team +# hubot () team count - list the current size of the team +# hubot () team (list|show) - list the people in the team +# hubot () team (empty|clear) - clear team list # # Author: # mihai config = - admin_list: process.env.HUBOT_TEAM_ADMIN + admin_list: process.env.HUBOT_TEAM_ADMIN, + default_team_label: '__default__' module.exports = (robot) -> robot.brain.data.teams ||= {} @@ -39,11 +41,20 @@ module.exports = (robot) -> return false unless teamExists(team_name, msg) robot.brain.data.teams[team_name].length + teamLabel = (team_name) -> + label = team_name unless team_name is config.default_team_label + message = if label then "`#{label}` team" else "team" + return message + addUserToTeam = (user, team_name, msg) -> - return unless teamExists(team_name, msg) + if not team_name + robot.brain.data['teams'][config.default_team_label] ||= [] + team_name = config.default_team_label + else + return unless teamExists(team_name, msg) if user in robot.brain.data.teams[team_name] - msg.send "#{user} already in the #{team_name}" + msg.send "#{user} already in the #{teamLabel(team_name)}" else count = teamSize(team_name, msg) if count > 0 @@ -53,104 +64,154 @@ module.exports = (robot) -> robot.brain.data.teams[team_name].push(user) - message = "#{user} added to the team" + message = "#{user} added to the #{teamLabel(team_name)}" message += countMessage if countMessage msg.send message addTeam = (team_name, msg) -> if robot.brain.data.teams[team_name] - msg.send "#{team_name} already exists" + msg.send "#{teamLabel(team_name)} already exists" else robot.brain.data['teams'][team_name] = [] - msg.send "#{team_name} team created!" - - listTeams = (msg) -> - team_count = Object.keys(robot.brain.data.teams).length - - if team_count > 0 - message = "Team (#{team_count} total):\n" - for team of robot.brain.data.teams - message += "#{team}\n" - for user in robot.brain.data.teams[team] - message += "- #{user}\n" - msg.send message - - else - msg.send "Oh noes, we gots no teams!" + msg.send "#{teamLabel(team_name)} created, add some people to it" removeUserFromTeam = (user, team_name, msg) -> + team_name = config.default_team_label unless team_name return unless teamExists(team_name, msg) if user not in robot.brain.data.teams[team_name] - msg.send "#{user} already out of the team" + msg.send "#{user} already out of the #{teamLabel(team_name)}" else user_index = robot.brain.data.teams[team_name].indexOf(user) robot.brain.data.teams[team_name].splice(user_index, 1) count = teamSize(team_name, msg) countMessage = ", " + count + " remaining" if count > 0 - message = "#{user} removed from #{team_name}" + message = "#{user} removed from the #{teamLabel(team_name)}" message += countMessage if countMessage msg.send message teamExists = (team_name, msg) -> + return true if team_name is config.default_team_label + if robot.brain.data.teams[team_name] true else - msg.send "#{team_name} does not exist, buddy." + msg.send "#{teamLabel(team_name)} does not exist" false + ## + ## hubot create team - create team called + ## robot.respond /create (\S*) team ?.*/i, (msg) -> team_name = msg.match[1] addTeam(team_name, msg) + ## + ## hubot (delete|remove) team - delete team called + ## + robot.respond /(delete|remove) (\S*) team ?.*/i, (msg) -> + team_name = msg.match[2] + return unless teamExists(team_name, msg) + + if msg.message.user.name in admins + unless team_name is config.default_team_label + delete robot.brain.data.teams[team_name] + msg.send "Team `#{team_name}` removed" + else + msg.reply "Sorry, only admins can remove teams" + + ## + ## hubot list teams - list all existing teams + ## robot.respond /list teams ?.*/i, (msg) -> - listTeams(msg) + team_count = Object.keys(robot.brain.data.teams).length + team_count = team_count - 1 if robot.brain.data.teams[config.default_team_label] + + if team_count > 0 + message = "Teams:\n" + + for team of robot.brain.data.teams + continue if team is config.default_team_label + size = teamSize(team) + if size > 0 + message += "`#{team}` (#{size} total)\n" + for user in robot.brain.data.teams[team] + message += "- #{user}\n" + message += "\n" + else + message += "`#{team}` (empty)\n" + + else + message = "No team was created so far" + + msg.send message - robot.respond /(\S*) team add (\S*) ?.*/i, (msg) -> + ## + ## hubot team add (me|) - add me or to team + ## + robot.respond /(\S*)? team add (\S*) ?.*/i, (msg) -> team_name = msg.match[1] user = msg.match[2] if user.toLocaleLowerCase() == "me" user = msg.message.user.name addUserToTeam(user, team_name, msg) - robot.respond /(\S*) team \+1 ?.*/i, (msg) -> + ## + ## hubot team +1 - add me to the team + ## + robot.respond /(\S*)? team \+1 ?.*/i, (msg) -> team_name = msg.match[1] addUserToTeam(msg.message.user.name, team_name, msg) - robot.respond /(\S*) team remove (\S*) ?.*/i, (msg) -> + ## + ## hubot team remove (me|) - remove me or from team + ## + robot.respond /(\S*)? team remove (\S*) ?.*/i, (msg) -> team_name = msg.match[1] user = msg.match[2] if user.toLocaleLowerCase() == "me" user = msg.message.user.name removeUserFromTeam(user, team_name, msg) - robot.respond /(\S*) team -1/i, (msg) -> + ## + ## hubot team -1 - remove me from the team + ## + robot.respond /(\S*)? team -1/i, (msg) -> team_name = msg.match[1] removeUserFromTeam(msg.message.user.name, team_name, msg) - robot.respond /(\S*) team count$/i, (msg) -> - team_name = msg.match[1] + ## + ## hubot team count - list the current size of the team + ## + robot.respond /(\S*)? team count$/i, (msg) -> + team_name = msg.match[1] || config.default_team_label return unless teamExists(team_name) msg.send "#{teamSize(team_name, msg)} people are currently in the team" - robot.respond /(\S*) team (list|show)$/i, (msg) -> - team_name = msg.match[1] + ## + ## hubot team (list|show) - list the people in the team + ## + robot.respond /(\S*)? team (list|show)$/i, (msg) -> + team_name = msg.match[1] || config.default_team_label return unless teamExists(team_name) count = teamSize(team_name, msg) if count == 0 - msg.send "There is no one in the team currently" + msg.send "There is no one in the #{teamLabel(team_name)} currently" else position = 0 - message = "Team (#{count} total):\n" + message = "#{teamLabel(team_name)} (#{count} total):\n" for user in robot.brain.data.teams[team_name] position += 1 message += "#{position}. #{user}\n" msg.send message - robot.respond /(\S*) team (new|clear|empty)$/i, (msg) -> - team_name = msg.match[1] + ## + ## hubot team (empty|clear) - clear team list + ## + robot.respond /(\S*)? team (clear|empty)$/i, (msg) -> + team_name = msg.match[1] || config.default_team_label if msg.message.user.name in admins robot.brain.data.teams[team_name] = [] - msg.send "Team list cleared" + msg.send "#{teamLabel(team_name)} list cleared" else msg.reply "Sorry, only admins can clear the team members list"