【问题标题】:when i launch my bot discord i have an error and i can't find how to fix it当我启动我的机器人不和谐时,我有一个错误,我找不到如何解决它
【发布时间】:2021-04-16 06:20:58
【问题描述】:

你好,我正在尝试编写我的第一个机器人,特别是一个命令,让它提供它的 ping 和 API 的 ping,但是当我启动机器人时,我遇到了这个错误,我不知道如何修复它

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

SyntaxError: await is only valid in async function

目前的代码是这样的:

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

(是的,我是法国人)

谢谢你的回答

【问题讨论】:

标签: javascript node.js discord discord.js


【解决方案1】:

由于错误状态,您在非异步函数中使用 await

为您的代码编写一个单独的 async 函数并导出该函数:

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

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

Check the discord guide

More information about async/await

【讨论】:

  • 感谢您的快速回复,现在我遇到了这个错误:`execute async (bot, message, args) { ^^^^^ SyntaxError: Unexpected token 'async'`
  • 哦,我已经根据这个更新了我的答案。
  • 好的,谢谢,后来我遇到了错误,但我设法纠正了它们,你知道如何让它给我 API 的 ping,因为现在它告诉我“NaNms”
  • NaN 的意思是“不是数字”,先试着记录下你需要的信息,看看里面有什么。
  • 嗯,我该怎么做?
猜你喜欢
  • 2021-04-27
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 2023-01-05
  • 2021-07-09
  • 2020-09-17
  • 2021-03-28
  • 1970-01-01
相关资源
最近更新 更多