簡體   English   中英

SyntaxError: await 僅在我的代碼中的異步 function 中有效

[英]SyntaxError: await is only valid in async function in my code

我的代碼有問題,錯誤是SyntaxError: await is only valid in async function.

const Discord = require("discord.js")

module.exports = {
  name: "ping",
  description: "Display ping latency.",

  run(msg, args) {
    let waiting = await message.channel.send("Calculation...").catch(console.error)

    let embed = new Discord.MessageEmbed()

      .setTitle("Bot & Discord.js API latency", bot.user.avatarURL)
      .setColor(0xb348ff)
      .setField("BOT :", "> '" + '${waiting.createdTimestamp - message.createdTimestamp}' + "ms'", true)
      .setField("API :", "'" + Math.round(bot.ping) + "ms'", true)
      .setTimestamp(message.createdAt)
      .setTimestamp()

    waiting.edit(embed).catch(console.error)
  }
}

錯誤說明了一切,您只能在異步( async )function 中使用await

const Discord = require("discord.js")

module.exports = {
  name: "ping",
  description: "Display ping latency.",

  async run(msg, args) {
    let waiting = await message.channel.send("Calculation...").catch(console.error)

    let embed = new Discord.MessageEmbed()

      .setTitle("Bot & Discord.js API latency", bot.user.avatarURL)
      .setColor(0xb348ff)
      .setField("BOT :", "> '" + '${waiting.createdTimestamp - message.createdTimestamp}' + "ms'", true)
      .setField("API :", "'" + Math.round(bot.ping) + "ms'", true)
      .setTimestamp(message.createdAt)
      .setTimestamp()

    waiting.edit(embed).catch(console.error)
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM