Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow keys to contain "is" and "?" #403

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 41 additions & 31 deletions scripts/remember.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,53 @@ module.exports = (robot) ->
Object.keys(memories()).filter (key) -> searchRegex.test(key)

robot.respond /(?:what is|rem(?:ember)?)\s+(.*)/i, (msg) ->
msg.finish()
words = msg.match[1].trim()
if match = words.match /(.*?)(\s+is\s+([\s\S]*))$/i
msg.finish()
key = match[1].toLowerCase()
value = match[3]
currently = memories()[key]
if currently
msg.send "But #{key} is already #{currently}. Forget #{key} first."

# First check for a search expression.
if match = words.match /\|\s*(grep\s+)?(.*)$/i
searchPattern = match[2]
matchingKeys = findSimilarMemories(searchPattern)
if matchingKeys.length > 0
msg.send "I remember:\n#{matchingKeys.join(', ')}"
else
memories()[key] = value
msg.send "OK, I'll remember #{key}."
else if match = words.match /([^?]+)\??/i
msg.finish()
msg.send "I don't remember anything matching `#{searchPattern}`"
return

key = match[1].toLowerCase()
value = memories()[key]
# Next, attempt to interpret `words` as an existing key. This also strips
# off the last "?" character.
if match = words.match /(.+?)\??$/i
stripped_key = match[1].toLowerCase()
value = memories()[stripped_key]

if value
memoriesByRecollection()[key] ?= 0
memoriesByRecollection()[key]++
else
if match = words.match /\|\s*(grep\s+)?(.*)$/i
searchPattern = match[2]
matchingKeys = findSimilarMemories(searchPattern)
if matchingKeys.length > 0
value = "I remember:\n#{matchingKeys.join(', ')}"
else
value = "I don't remember anything matching `#{searchPattern}`"
memoriesByRecollection()[stripped_key] ?= 0
memoriesByRecollection()[stripped_key]++
msg.send value
return

# Next, attempt to interpret `words` as a "foo is bar" expression in order
# to store a memory.
if match = words.match /^(.*)is(.*)$/i
key = match[1].trim().toLowerCase()
value = match[2].trim()
if key and value
currently = memories()[key]
if currently
msg.send "But #{key} is already #{currently}. Forget #{key} first."
else
matchingKeys = findSimilarMemories(key)
if matchingKeys.length > 0
keys = matchingKeys.join(', ')
value = "I don't remember `#{key}`. Did you mean:\n#{keys}"
else
value = "I don't remember anything matching `#{key}`"

msg.send value
memories()[key] = value
msg.send "OK, I'll remember #{key}."
return

# If none of the previous actions succeeded, search existing memories for
# similar keys.
matchingKeys = findSimilarMemories(stripped_key)
if matchingKeys.length > 0
keys = matchingKeys.join(', ')
msg.send "I don't remember `#{stripped_key}`. Did you mean:\n#{keys}"
else
msg.send "I don't remember anything matching `#{stripped_key}`"

robot.respond /forget\s+(.*)/i, (msg) ->
key = msg.match[1].toLowerCase()
Expand Down