【发布时间】: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