[英]How do I wait for a message to be sent in a channel with DiscordJS?
我正在制作一个应用程序机器人,它将具有多项选择和短/长响应的选项。 响应系统使用直接消息,需要等待消息发送才能继续,但 MessageChannel#awaitMessages 对我不起作用。 这是我的代码(两个独立的部分):
// borrowed from another stackoverflow question
const waitUntil = (condition, checkInterval = 100) => {
return new Promise(resolve => {
let interval = setInterval(() => {
if (!condition()) return;
clearInterval(interval);
resolve();
}, checkInterval)
})
}
let shouldStop = false
var content = 1
var contentArrived = false
const filter = (m) => m.author.id == userId // userId is the ID of the user in DMs
interaction.channel.awaitMessages({ filter, max: 1, maxProcessed: 1, idle: 10_000, errors: ['idle'] })
.then(collected => {
console.log(`${collected.size} is pain and suffering`)
content = collected.first().content
contentArrived = true
})
.catch(() => {
message.channel.send('You did not respond to the question so your application was canceled.')
shouldStop = true
currentlyApplying.set(userId, null)
})
await waitUntil(() => {
return contentArrived || shouldStop
}, 500)
我用文档和其他 StackOverflow 问题中的示例尝试使用 awaitMessages,但没有一个有效。 他们都卡住了,没有检测到任何消息,即使过滤器设置为检测所有非机器人消息。 然后它们都会抛出错误并被.catch()
捕获。
解决方案是在创建客户端时包含部分message
并启用DirectMessages
Intent。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.