繁体   English   中英

Discord.js 'await 仅在异步函数中有效',即使它是异步 function?

[英]Discord.js 'await is only valid in an async function' even though it is an async function?

我正在编写一个机器人来制作票务系统,并且我试图让机器人对消息做出反应,但这并不是因为我得到的错误await is only valid in an async function 我知道这意味着什么,我感到困惑的部分是它是一个异步 function:我知道这一点,因为在函数/事件的早期,有一个await语句。 这是代码:

client.on("message", async (message) => {

if (message.author.bot) return;

const filter = (m) => m.author.id === message.author.id;

if (message.content === "-ticket") {

let channel = message.author.dmChannel;
if (!channel) channel = await message.author.createDM();

let embed = new Discord.MessageEmbed();
embed.setTitle('Open a Ticket')
embed.setDescription('Thank you for reaching out to us. If you have a question, please state it below so I can connect you to a member of our support team. If you a reporting a user, please describe your report in detail below.')
embed.setColor('AQUA')

message.author.send(embed);
channel
  .awaitMessages(filter, {max: 1, time: 1000 * 300, errors: ['time'] })
  .then((collected) => {

    const msg = collected.first();
    message.author.send(`
     >>> ✅ Thank you for reaching out to us! I have created a case for 
    your inquiry with out support team. Expect a reply soon!
    ❓ Your question: ${msg}
    `);

    let claimEmbed = new Discord.MessageEmbed();
    claimEmbed.setTitle('New Ticket')
    claimEmbed.setDescription(`       
    New ticket created by ${message.author.tag}: ${msg}

    React with ✅ to claim!
    `)
    claimEmbed.setColor('AQUA')
    claimEmbed.setTimestamp()

    try{

      let claimChannel = client.channels.cache.find(channel => channel.name === 'general');
      claimChannel.send(claimEmbed);

      await claimMessage.react("✅");

    } catch (err) {
      throw (err);
    }

  })
  .catch((err) => console.log(err));
  }
})

收集消息时,有一个箭头 function 缺少async关键字:

client.on('message', async (message) => {
  if (message.author.bot) return;

  const filter = (m) => m.author.id === message.author.id;

  if (message.content === '-ticket') {
    let channel = message.author.dmChannel;
    if (!channel) channel = await message.author.createDM();

    let embed = new Discord.MessageEmbed();
    embed.setTitle('Open a Ticket');
    embed.setDescription(
      'Thank you for reaching out to us. If you have a question, please state it below so I can connect you to a member of our support team. If you a reporting a user, please describe your report in detail below.',
    );
    embed.setColor('AQUA');

    message.author.send(embed);
    channel
      .awaitMessages(filter, { max: 1, time: 1000 * 300, errors: ['time'] })
      // it should be async
      .then(async (collected) => {
        const msg = collected.first();
        message.author.send(`
     >>> ✅ Thank you for reaching out to us! I have created a case for
    your inquiry with out support team. Expect a reply soon!
    ❓ Your question: ${msg}
    `);

        let claimEmbed = new Discord.MessageEmbed();
        claimEmbed.setTitle('New Ticket');
        claimEmbed.setDescription(`
    New ticket created by ${message.author.tag}: ${msg}

    React with ✅ to claim!
    `);
        claimEmbed.setColor('AQUA');
        claimEmbed.setTimestamp();

        try {
          let claimChannel = client.channels.cache.find(
            (channel) => channel.name === 'general',
          );
          claimChannel.send(claimEmbed);

          await claimMessage.react('✅');
        } catch (err) {
          throw err;
        }
      })
      .catch((err) => console.log(err));
  }
});

确保此代码是否属于 promise 回调,以确保回调 function 也是异步的。 如果您不提供完整代码,我们将无法再提供帮助。

暂无
暂无

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

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