Skip to content
Snippets Groups Projects
Commit 8ab21427 authored by Rodrigo Nascimento's avatar Rodrigo Nascimento
Browse files

Init hubot script loading

parent 0fe159f5
No related branches found
No related tags found
No related merge requests found
......@@ -149,38 +149,44 @@ class RocketBotReceiver
class HubotScripts
constructor: (robot) ->
# load all scripts in scripts/
scriptPath = path.resolve __dirname, 'scripts'
for file in fs.readdirSync(scriptPath)
continue unless /\.(coffee|js)$/.test(file)
robot.loadFile scriptPath, file
# load all scripts from hubot-scripts
scriptPath = path.resolve __dirname, 'node_modules', 'hubot-scripts', 'src', 'scripts'
scripts = require './hubot-scripts.json'
robot.loadHubotScripts scriptPath, scripts
robot.parseHelp path.join scriptPath, 'meme_captain.coffee'
# load all hubot-* modules from package.json
packageJson = require './package.json'
pkgs = (pkg for own pkg, version of packageJson.dependencies when !/^(coffee-script|hubot-scripts|hubot-help)$/.test(pkg))
pkgs.forEach (p) -> (require p)(robot)
# A special hack for hubot-help: ensure it replies via pm
privRobot = Object.create robot
privRobot.respond = (regex, cb) ->
robot.respond regex, (resp) ->
resp.message.private = true
cb(resp)
(require 'hubot-help')(privRobot)
# A special hack for meme_captain: change its "respond" invocations to "hear" so that it memes everywhere.
memecaptain = require './node_modules/hubot-scripts/src/scripts/meme_captain'
memecaptain
respond: (regex, cb) ->
robot.hear regex, (msg) ->
cb(msg) if msg.envelope.room is 'general/0' or /^\s*[@]?(rocket)?bot\b/i.test(msg.message.text)
hello = Npm.require 'hubot-scripts/src/scripts/hello.coffee'
hello robot
console.log Npm.require 'hubot-scripts/src/scripts'
# # load all scripts in scripts/
# console.log path.resolve '.'
# scriptPath = path.resolve __dirname, 'scripts'
# console.log scriptPath
# for file in fs.readdirSync(scriptPath)
# continue unless /\.(coffee|js)$/.test(file)
# robot.loadFile scriptPath, file
# return
# # load all scripts from hubot-scripts
# scriptPath = path.resolve __dirname, 'node_modules', 'hubot-scripts', 'src', 'scripts'
# scripts = require './hubot-scripts.json'
# robot.loadHubotScripts scriptPath, scripts
# robot.parseHelp path.join scriptPath, 'meme_captain.coffee'
# # load all hubot-* modules from package.json
# packageJson = require './package.json'
# pkgs = (pkg for own pkg, version of packageJson.dependencies when !/^(coffee-script|hubot-scripts|hubot-help)$/.test(pkg))
# pkgs.forEach (p) -> (require p)(robot)
# # A special hack for hubot-help: ensure it replies via pm
# privRobot = Object.create robot
# privRobot.respond = (regex, cb) ->
# robot.respond regex, (resp) ->
# resp.message.private = true
# cb(resp)
# (require 'hubot-help')(privRobot)
# # A special hack for meme_captain: change its "respond" invocations to "hear" so that it memes everywhere.
# memecaptain = require './node_modules/hubot-scripts/src/scripts/meme_captain'
# memecaptain
# respond: (regex, cb) ->
# robot.hear regex, (msg) ->
# cb(msg) if msg.envelope.room is 'general/0' or /^\s*[@]?(rocket)?bot\b/i.test(msg.message.text)
sendHelper = Meteor.bindEnvironment (robot, envelope, strings, map) ->
while strings.length > 0
......@@ -197,7 +203,7 @@ sendHelper = Meteor.bindEnvironment (robot, envelope, strings, map) ->
RocketBot = new Robot null, null, false, 'rocketbot'
RocketBot.alias = 'bot'
RocketBot.adapter = new RocketChatAdapter RocketBot
# HubotScripts(RocketBot)
HubotScripts(RocketBot)
RocketBot.hear /test/i, (res) ->
res.send "Test? TESTING? WE DON'T NEED NO TEST, EVERYTHING WORKS!"
......
RocketChat.addUserToRoom = (userId, data) ->
fromId = userId
# console.log '[methods] addUserToRoom -> '.green, 'fromId:', fromId, 'data:', data
room = ChatRoom.findOne data.rid
# if room.username isnt Meteor.user().username and room.t is 'c'
if room.u.username isnt Meteor.users.findOne(userId).username and room.t is 'c'
throw new Meteor.Error 403, '[methods] addUserToRoom -> Not allowed'
# verify if user is already in room
if room.usernames.indexOf(data.username) isnt -1
return
now = new Date()
update =
$push:
usernames:
$each: [data.username]
$sort: 1
newUser = Meteor.users.findOne username: data.username
ChatRoom.update data.rid, update
ChatSubscription.insert
rid: data.rid
ts: now
name: room.name
t: room.t
open: true
alert: true
unread: 1
u:
_id: newUser._id
username: data.username
ChatMessage.insert
rid: data.rid
ts: now
t: 'au'
msg: newUser.name
u:
_id: fromId
username: Meteor.users.findOne(userId).username
return true
Meteor.methods
addUserToRoom: (data) ->
RocketChat.addUserToRoom Meteor.userId(), data
fromId = Meteor.userId()
# console.log '[methods] addUserToRoom -> '.green, 'fromId:', fromId, 'data:', data
room = ChatRoom.findOne data.rid
# if room.username isnt Meteor.user().username and room.t is 'c'
if room.u.username isnt Meteor.user().username and room.t is 'c'
throw new Meteor.Error 403, '[methods] addUserToRoom -> Not allowed'
# verify if user is already in room
if room.usernames.indexOf(data.username) isnt -1
return
now = new Date()
update =
$push:
usernames:
$each: [data.username]
$sort: 1
newUser = Meteor.users.findOne username: data.username
ChatRoom.update data.rid, update
ChatSubscription.insert
rid: data.rid
ts: now
name: room.name
t: room.t
open: true
alert: true
unread: 1
u:
_id: newUser._id
username: data.username
ChatMessage.insert
rid: data.rid
ts: now
t: 'au'
msg: newUser.name
u:
_id: fromId
username: Meteor.user().username
return true
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment