简体   繁体   中英

SyntaxError: await is only valid in async function in my code

I have problem with my code, the error is 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)
  }
}

The error says it all, you can only use await inside of an asynchronous ( async ) function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM