【问题标题】:Discord.js 'await is only valid in an async function' even though it is an async function?Discord.js“等待仅在异步函数中有效”,即使它是异步函数?
【发布时间】:2022-01-25 07:19:55
【问题描述】:

我正在编写一个机器人来制作票务系统,并且我试图让机器人对消息做出反应,但这并不是因为我收到了错误 await is only valid in an async 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));
  }
})

【问题讨论】:

  • 可以在消息事件中看到完整的代码吗?
  • 也许这是该方法中的调用之一,而不是他等待调用本身?检查行号
  • try/catch 的词法范围也必须是异步的

标签: javascript discord.js


【解决方案1】:

当您收集消息时,有一个箭头函数缺少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));
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多