forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request github#80 from lukesmith/locations-script
A decision making script. Ask hubot to remember locations
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Decides where you should go. | ||
# | ||
# These commands are grabbed from comment blocks at the top of each file. | ||
# | ||
# remember <location> as a <group> location - Remembers the location for the group. | ||
# forget <location> as a <group> location - Forgets the location from the group. | ||
# forget all locations for <group> - Forgets all the locations for the group. | ||
# where can we go for <group>? - Returns a list of places that exist for the group. | ||
# where should we go for <group>? - Returns a randomly selected location for the group. | ||
|
||
class Locations | ||
constructor: (@robot) -> | ||
@robot.brain.data.locations = {} | ||
|
||
add: (groupname, name) -> | ||
if @robot.brain.data.locations[groupname] is undefined | ||
@robot.brain.data.locations[groupname] = [] | ||
|
||
for location in @robot.brain.data.locations[groupname] | ||
if location.toLowerCase() is name.toLowerCase() | ||
return | ||
|
||
@robot.brain.data.locations[groupname].push name | ||
|
||
remove: (groupname, name) -> | ||
group = @robot.brain.data.locations[groupname] or [] | ||
@robot.brain.data.locations[groupname] = (location for location in group when location.toLowerCase() isnt name.toLowerCase()) | ||
|
||
removeAll: (groupname) -> | ||
delete @robot.brain.data.locations[groupname] | ||
|
||
group: (name) -> | ||
return @robot.brain.data.locations[name] or [] | ||
|
||
|
||
module.exports = (robot) -> | ||
locations = new Locations robot | ||
|
||
robot.respond /remember (.*) as a (.*) location/i, (msg) -> | ||
locationname = msg.match[1] | ||
locationgroup = msg.match[2] | ||
locations.add locationgroup, locationname | ||
|
||
if locationname.toLowerCase() is "nandos" | ||
msg.send "Nom peri peri. My fav." | ||
|
||
robot.respond /forget (.*) as a (.*) location/i, (msg) -> | ||
locationname = msg.match[1] | ||
locationgroup = msg.match[2] | ||
locations.remove locationgroup, locationname | ||
|
||
robot.respond /forget all locations for (.*)/i, (msg) -> | ||
locationgroup = msg.match[1] | ||
locations.removeAll locationgroup | ||
|
||
robot.respond /where can we go for (.*)\?$/i, (msg) -> | ||
locationgroup = msg.match[1] | ||
grouplocations = locations.group(locationgroup) | ||
|
||
if grouplocations.length > 0 | ||
for location in grouplocations | ||
msg.send location | ||
else | ||
msg.send "I don't know anywhere to go for #{locationgroup}" | ||
|
||
robot.respond /where (should|shall) we go for (.*)\?$/i, (msg) -> | ||
locationgroup = msg.match[2] | ||
grouplocations = locations.group(locationgroup) | ||
|
||
if grouplocations.length is 0 | ||
msg.send "I dont know anywhere to go for #{locationgroup}" | ||
else | ||
location = msg.random grouplocations | ||
|
||
msg.send "I think you should goto #{location}" |