Created
June 6, 2014 15:05
-
-
Save elliotttf/634e066355900a96c04c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Bot factoid. | |
# | |
# facts - List all the stored definitions. | |
# <subject> is <definition> - Define a word or subject. | |
# forget <subject> - Remove a definition. | |
u = require 'underscore' | |
util = require 'util' | |
module.exports = (robot) -> | |
# 75% chance that robochef will respond to a definition mention. | |
probability = 75 | |
# Flag to avoid responding randomly. | |
quiet = false | |
# Event listener for brain loaded. | |
robot.brain.on 'loaded', => | |
robot.brain.data.factoids ||= {} | |
## | |
# List the factoids. | |
## | |
robot.respond /(definitions|facts)\??/i, (msg) -> | |
if !u.size robot.brain.data.factoids | |
msg.send "I don't know nothin." | |
return | |
count = 0 | |
bundle = [] | |
u.each robot.brain.data.factoids, (definition, subject) -> | |
bundle.push util.format '%s %s %s', subject, definition.verb, definition.definition | |
if bundle.length > 1 and bundle.legnth % 10 is 0 | |
str = bundle.join '\n' | |
msg.send str | |
bundle = [] | |
if bundle.length | |
str = bundle.join '\n' | |
msg.send str | |
## | |
# Save a new factoid. | |
## | |
robot.respond /(.*?)\s+(is|are)\s+(.*)$/i, (msg) -> | |
# Bail if the bot is being addressed in a different way | |
if /^(\?all|here|how|it|something|that|this|what|when|where|which|who|why)$/i.exec(msg.match[1]) | |
return | |
# Bail if this is an empty key. | |
if msg.match[1].length == 0 | |
return | |
# Else save the definition. | |
robot.brain.data.factoids[msg.match[1].toLowerCase()] = | |
verb: msg.match[2] | |
definition: msg.match[3] | |
msg.send util.format 'Saved %s.', msg.match[1] | |
## | |
# Delete a factoid | |
## | |
robot.respond /forget( about)? (.*)$/i, (msg) -> | |
if typeof robot.brain.data.factoids[msg.match[2].toLowerCase()] isnt 'undefined' | |
delete robot.brain.data.factoids[msg.match[2].toLowerCase()] | |
msg.send util.format "I've forgotten about %s.", msg.match[2] | |
else | |
msg.send util.format "I don't know anything about %s.", msg.match[2] | |
## | |
# Helper respond function. | |
## | |
respond = (subject, msg) -> | |
key = subject.toLowerCase() | |
if key.length && typeof robot.brain.data.factoids[key] isnt 'undefined' | |
msg.send util.format '%s %s %s', subject, robot.brain.data.factoids[key].verb, robot.brain.data.factoids[key].definition | |
## | |
# Respond if the factoid was mentioned. | |
## | |
robot.hear /(.+)[!\?]+$/i, (msg) -> | |
respond msg.match[1], msg | |
## | |
# Respond if we were asked about the factoid. | |
## | |
robot.respond /(.+)[!\?]+$/i, (msg) -> | |
respond msg.match[1], msg | |
## | |
# Randomly respond to a known definition | |
## | |
robot.hear /.+[^!\?]+$/i, (msg) -> | |
# Bail if we've been told to shut up, meanies! | |
if quiet | |
return | |
# Bail if we would be too annoying by responding. | |
if (Math.random() * (100 - 1) + 1) > probability | |
return | |
# Respond to any word that matches. | |
responded = false | |
u.each robot.brain.data.factoids, (definition, subject) -> | |
if subject.length == 0 | |
return | |
# Dial back the annoyance factor. | |
if responded | |
return | |
re = new RegExp subject, 'i' | |
if msg.match[0].match re | |
msg.send util.format '%s %s %s', subject, definition.verb, definition.definition | |
responded = true | |
## | |
# Make the bot quiet for a timeout | |
## | |
robot.respond /(shut up|be quiet)/i, (msg) -> | |
msg.send 'http://cdn.memegenerator.net/instances/400x/20447835.jpg' | |
quiet = true | |
# Stay quiet for one hour. | |
setTimeout () -> | |
quiet = false | |
, 3600000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment