3

我正在尝试为 Github 的 Hubot 编写一个脚本,该脚本使用 TooTallNate 的 Node-Spotify-Web 通过 spotify 播放音乐,而且我对 CoffeeScript 有点陌生(Hubot 脚本是用什么编写的)。我在这里写出了第一个命令“播放”:

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

在运行 bin/hubot 时,我收到错误“语法错误,保留字”“功能”,所以我说,好的,并将“功能”更改为“->”,正如另一个 StackOverflow 问题中的建议。使它看起来像:

http://pastebin.com/dEw0VrH5

但仍然得到错误

错误无法加载/home/xbmc/cbot/lisa/scripts/spotify:语法错误:保留字“函数”

是因为依赖吗?我真的被困在这里了。

4

2 回答 2

4

咖啡脚本文档的第一部分之一是如何声明函数。您不只是将单词更改function->. 没那么简单。在 Javascript 函数中是function(args) { body },但在 Coffee Script 中是(args) -> body

但为简洁起见,当你有这个时:

Spotify.login(username, password, function (err, spotify)) {

这对 CoffeeScript 不起作用,因为这不是声明函数的语法。你要:

Spotify.login username, password, (err, spotify) ->
  # function body

和这里一样:

spotify.get(uri, function(err, track){

应该是:

spotify.get uri, (err, track) ->
于 2013-12-22T05:36:49.807 回答
0

CoffeeScript 的函数语法是

(arguments...) ->
    body

并不是

-> (arguments...) {
    body
}

你也有正确的语法:

robot.respond /play (.*)/i, (message) ->
    uri = message.match[1]
    ....

您是否从某个地方复制粘贴了一个片段?

于 2013-12-22T05:28:58.517 回答