Question

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:

http://pastebin.com/Pp6mqucm

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:

http://pastebin.com/dEw0VrH5

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.

Était-ce utile?

La solution

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) ->

Autres conseils

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?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top