Reputation: 63
I'm trying to code a script for Github's Hubot that uses TooTallNate's Node-Spotify-Web to play music through spotify, and I'm somewhat new to CoffeeScript (What Hubot scripts are written in). I wrote out the first command "Play" here:
lame = require('lame')
Speaker = require('speaker')
Spotify = require('spotify-web')
username = "INSERTUSERNAMEHERE"
password = "INSERTPASSWORDHERE"
robot.respond /play (.*)/i, (message) ->
uri = message.match[1]
Spotify.login(username, password, function (err, spotify)) {
if (err) throw err;
console.log('Playing: %s - %s', track.artist[0].name, track.name)
}
spotify.get(uri, function(err, track){
if err throw err;
message.send("Playing:" + track.artist[0].name, track.name)
})
Upon running bin/hubot I got the Error "Syntax Error, Reserved word "function" so I said, ok and changed 'function' to '->' as recommend in another StackOverflow question. Making it so it appeared as:
But still get the error
ERROR Unable to load /home/xbmc/cbot/lisa/scripts/spotify: SyntaxError: reserved word "function"
Is it because of the dependencies? I'm really stuck here.
Upvotes: 4
Views: 7258
Reputation: 186994
One of the very first sections of the coffee script documentation is how to declare functions. You don't just change the word function
to ->
. It's not that simple. In Javascript functions are function(args) { body }
, but in Coffee Script it's (args) -> body
But for brevity, when you have this:
Spotify.login(username, password, function (err, spotify)) {
That's not going to work CoffeeScript, because that's not the syntax for declaring functions. You want:
Spotify.login username, password, (err, spotify) ->
# function body
And the same here:
spotify.get(uri, function(err, track){
Which should be:
spotify.get uri, (err, track) ->
Upvotes: 4
Reputation: 198314
CoffeeScript's function syntax is
(arguments...) ->
body
and not
-> (arguments...) {
body
}
You have the correct syntax too:
robot.respond /play (.*)/i, (message) ->
uri = message.match[1]
....
Did you copy-paste a snippet from somewhere?
Upvotes: 0