【问题标题】:There is an error with async being the problem异步出现错误是问题
【发布时间】:2021-03-28 09:27:48
【问题描述】:

出现异步错误。我正在使用的代码如下。我什至添加了异步消息但仍然有错误?我已经搜索了它,但没有答案。



    let args = message.content.slice(prefix.length).split(" ");
    
    switch(args[0]){
        case 'ban':
        if(!message.member.hasPermission("BAN_MEMBERS")) {
          return message.channel.send("You cannot use this command.")
        }
        member = message.mentions.members.first(); 
        if (!member) {
          message.react("❌");
          return message.channel.send("Missing arguments. -ban @user Reason");
        } 

        reason = args.slice(1).join(" "); 
        if (!reason) reason = "Please provide a reason."; 

        await member
        .ban(reason) 
        message.react("✔️");
        embed = new MessageEmbed()
        .setColor("RED")
        .setTitle("You banned them.")
        .setDescription(`${member} is banned forever. He he`)
        .addFields(
          {name: 'reason', value: reason}
        )
        .setFooter("You banned someone.");
        message.channel.send(embed);
      };
});

【问题讨论】:

  • 我从未使用过 discord.js,但这看起来不像是有效代码 await member.ban(reason)message.react("✔️");.ban()函数后面不应该有分号吗?
  • 您遇到了什么具体错误?将错误消息添加到您的代码中,如果可能,添加触发错误的代码行。这些信息将极大地帮助我们解决您的问题。还可以考虑发布更多代码,包括其中的 client.on("message") 部分。

标签: node.js discord discord.js bots


【解决方案1】:

member.ban() 之后应该有一个分号,最好是一个新行。您没有显示它包含的函数,但它必须是异步函数,因为普通函数不能包含单词await。要创建异步函数,而不是执行 function runCommand() { },只需执行 async function runCommand() { }

这是因为异步函数,正如异步名称所暗示的那样,与其他所有事物异步运行,因此同时运行。一个正常的函数是独立运行的,所以如果你在一个正常的函数中设置一个等待,它会导致整个机器人在等待它时冻结。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 2016-01-13
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多