Skip to content

Instantly share code, notes, and snippets.

@paul-bjorkstrand
Last active April 26, 2017 03:20
Show Gist options
  • Save paul-bjorkstrand/b99dea15ceef285403e00b962af8c818 to your computer and use it in GitHub Desktop.
Save paul-bjorkstrand/b99dea15ceef285403e00b962af8c818 to your computer and use it in GitHub Desktop.
module = module ? {}
module.exports = (robot) ->
class Factoid
constructor: (@x, @v, @y) ->
robot.logger.info("making factoid [#{@x}, #{@v}, #{@y}]")
@y = if Array.isArray(@y) then @y else [@y]
respond: (robot, message) ->
robot.logger.info("responding to [#{@x}, #{@v}, #{@y}]")
switch @v
when "reply" then @reply(robot, message)
when "'s" then @possessive(robot, message)
when "action" then @action(robot, message)
else @verb(robot, message)
reply: (robot, res) ->
robot.logger.info("reply: [#{@x}, #{@v}, #{@y}]")
robot.send(response(robot, res, @someY()))
possessive: (robot, message) ->
robot.logger.info("possessive: [#{@x}, #{@v}, #{@y}]")
robot.send(response(robot, res, "#{@x}#{@v} #{@someY()}"))
action: (robot, message) ->
robot.logger.info("action: [#{@x}, #{@v}, #{@y}]")
robot.emote(response(robot, res, @someY()))
verb: (robot, message) ->
robot.logger.info("verb: [#{@x}, #{@v}, #{@y}]")
robot.send(response(robot, res, "#{@x} #{@v} #{@someY()}"))
someY = () ->
if @y.length == 1
@y[0]
else
@y[Math.floor(Math.random() * @y.length)]
response = (robot, res, phrase) ->
phrase
class Hubucket
constructor: (robot) ->
@data = getData(robot)
@data.factoids ?= {}
addFactoid: (robot, x, v, y) ->
robot.logger.info("adding factoid [#{x}, #{v}, #{y}]")
factoid = @data.factoids[x] ? {}
verbiage = factoid[v] ? {}
phrases = verbiage.phrases ? []
phrases.push(y)
factoid[v] = verbiage
factoids[x] = factoid
data.factoids = factoids
setData(robot, @data)
getFactoid: (x, v) ->
factoid = @data.factoids[x]
verbiage = factoid[v]
if verbiage?
new Factoid(x, v, y)
else
null
getData = (robot) ->
robot.brain.get("hubucketData") ? {}
setData = (robot, data) ->
robot.brain.set("hubucketData", data)
robot.hubucket = new Hubucket(robot)
robot.respond /(.*)\s*<(.*)>\s+(.*)/, (res) ->
robot.logger.info(res.message)
addFactoid(robot, res.match[1], res.match[2], res.match[3])
res.send "Okay, #{res.message.user.name}"
robot.listen (
# matcher
(message) ->
if /.{3,}/.test(message)
robot.logger.info(message)
robot.hubucket.getFactoid(robot, message)
else
null
# responder
(res) ->
res.match.respond(robot)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment