简体   繁体   English

当我启动我的机器人 discord 时出现错误,我找不到解决方法

[英]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你好,我正在尝试编写我的第一个机器人,尤其是一个命令,让它给出它的 ping 和 API 但是当我启动机器人时,我遇到了这个错误,我不知道如何修复它

        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.正如错误所述,您在非异步 function 中使用await

Write a separate async function for your code and export that function:为您的代码编写一个单独的async function 并导出该 function:

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

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

Check the discord guide 检查 discord 指南

More information about async/await 有关异步/等待的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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