简体   繁体   中英

when i launch my bot discord i have an error and i can't find how to fix it

hello i'm trying to code my first bot and especially a command for it to give its ping and that of the API but when i launch the bot i have this error and i don't know how to fix it

        waiting = await message.channel.send("Je calcule le ping...").catch(console.error);
                  ^^^^^

SyntaxError: await is only valid in async function

currently the code is this:

const Discord = require ("discord.js");

module.exports = {
    name: "ping",
    execute(bot, message, args){
        message.delete().catch(console.error);

        waiting = await message.channel.send("Je calcule le ping...").catch(console.error);

        let embed = new Discord.MessageEmbed()
        .setAuthor("Latence du bot & de l'api discord.js", bot.user.avatarURL)
        .setColor("RANDOM")
        .setTitle("pong !")
        .addField("**CactusBot :**", "> `" + `${bot.ws.ping}` + "ms`", true)
        .addField("**API :**", "> `" + Math.round(bot.ping) + "ms`", true)
        .setTimestamp(message.createdAt)
        .setFooter("Nuptay | demandé par @" + message.author.tag, bot.user.avatarURL)
        waiting.edit(pingEmbed).catch(console.error);
        message.channel.send(embed);
    }
}

(yes I am French)

thank you for your answers

As the error states you are using await inside a non-async function.

Write a separate async function for your code and export that function:

const execute = async (bot, message, args) => {
  // your code with await
}

module.exports = {
  name: "ping",
  execute,
}

Check the discord guide

More information about async/await

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