【问题标题】:How to solve error "SyntaxError: Unexpected token '?'"如何解决错误“SyntaxError: Unexpected token '?'”
【发布时间】:2021-10-11 23:10:02
【问题描述】:

我不确定出了什么问题。我删除了我的代码并下载了它,然后再次上传它,现在我收到了这个错误。

代码:https://replit.com/@hi12167pies/webcord#index.js(点击代码为代码,输出为输出)

错误:

/home/runner/C8AU9ceLyjc/node_modules/discord.js/src/rest/RESTManager.js:32
    const token = this.client.token ?? this.client.accessToken;
                                     ^

SyntaxError: Unexpected token '?'

我不知道出了什么问题,因为它在 node_modules 文件夹中。

如果您在查看时遇到问题,这里是代码:

const http = require("http")
const discord = require("discord.js")
const client = new discord.Client()
const config = require("./config.json")
const fs = require("fs")
// const readLine = require("readline")
// const rl = readLine.createInterface({
//   input: process.stdin,
//   output: process.stdout
// })

let msgs = {
  "873195510251532348": [],
  "873195522633105429": []
}


client.on("ready", () => {
  console.log("ready discord")
})

client.on("message", (message) => {
  if (message.author.bot) return
  if (!config.chats.includes(message.channel.id.toString())) return

  msgs[message.channel.id].push({
    "username": message.author.tag,
    "content": message.content,
    "type": "0"
  })
})

http.createServer((req,res) => {
  const url = req.url.split("?")[0]
  let query = {}
  req.url.slice(req.url.split("").indexOf("?")).slice(1).split("&").forEach((e) => {
    const splited = e.split("=")
    query[splited[0]] = splited[1]
  })

  if (query.q == "messages") {
    let msg = []

    let i = 0
    while (msgs[query.code].length > i) {
      const e = msgs[query.code][msgs[query.code].length - (i+1)]
      msg.push(e)
      i++
    }

    res.write(JSON.stringify(msg))
    res.end()
  } else if (query.q == "post") {
    let name = query.name.split("%20").join(" ")
    let content = query.content.split("%20").join(" ")
    client.channels.cache.get(query.code).send(`**${name}**: ${content}`)
    msgs[query.code].push({
      "username": name,
      "content": content,
      "type": "1"
    })
    res.end()
  } else if (url == "/robot" && query.istrue == "true") {
    res.write("Robot!")
    res.end()
  } else {
    let path
    if (!query.code) {
      path = "./code.html"
    } else {
      if (!config.chats.includes(query.code)) {
        path = "./invaildcode.html"
      } else {
        path = "./chat.html"
      }
    }
    fs.readFile(path, (er, da) => {
      if (er) res.write("Could not get index.html")
      res.write(da)
      res.end()
    })
  }


}).listen(80, (err) => {
  if (err) throw err
  console.log("listening webserver")
})

client.login(process.env.TOKEN)

我知道我的代码现在不好,我正在重写它,但我仍然想知道错误是什么。

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    repl.it 使用 node v12.22.1,但 nullish coalescing operator (??) 相对较新,是在 node v14 中添加的。

    所以要使用 ?? 运算符,您需要在 repl.it 中更新 node

    您可以通过 this repl.it forum post 关注 lukenzy 来完成。

    创建一个文件并将其命名为 .replit 在其中,复制并粘贴以下代码:

    run = """
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh |  bash
    export NVM_DIR=\"$HOME/.nvm\"
    [ -s \"$NVM_DIR/nvm.sh\" ] && \\. \"$NVM_DIR/nvm.sh\"
    [ -s \"$NVM_DIR/bash_completion\" ] && \\.\"$NVM_DIR/bash_completion\"
    nvm install 14
    node index.js
    """
    

    这将安装和使用最新的 Node.js v14 (14.17.4)。
    如果您想使用其他版本,请将 nvm install 14 更改为任何其他版本 号码。
    另外,将节点 index.js 更改为您要运行的文件。

    【讨论】:

    • 这很有道理,但我不明白为什么它曾经有效?
    • @hi12167pies 新语法不在您的代码中,但在discord.js 中,也许您已更新到使用新语法的discord.js 的最新版本?
    • 确实如此。如果您查看discord.js v12 hereRESTManager 的源代码,您会发现它使用了旧的方法(||)而不是无效的合并(??)。 Discord.js v13 使用较新的空值合并和可选链接运算符,这就是该库要求 node.js >=14.x.x 现在运行的原因。
    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2015-04-21
    相关资源
    最近更新 更多