Skip to content

Instantly share code, notes, and snippets.

@tjhanley
Created January 26, 2015 17:47
Show Gist options
  • Save tjhanley/e8002980a9efb2b63d1a to your computer and use it in GitHub Desktop.
Save tjhanley/e8002980a9efb2b63d1a to your computer and use it in GitHub Desktop.
Shuffle Users
# Description:
# Generates a random list of users in the room
#
# Commands:
# hubot djs - gathers a list of users and shuffles them
#
# Author:
# tjhanley
module.exports = (robot) ->
robot.respond /djs/i, (msg) ->
djs = []
for key,userObj of robot.adapter.client.users
if userObj.presence is "active" and (userObj.name isnt "slackbot" and userObj.name isnt "walkerbot" and userObj.name isnt 'bot')
djs.push userObj.name
msg.send shuffle(djs).toString()
shuffle = (array) ->
currentIndex = array.length
temporaryValue = undefined
randomIndex = undefined
# While there remain elements to shuffle...
while 0 isnt currentIndex
# Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
# And swap it with the current element.
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment