Discord.js检测用户何时输入

问题描述

是否可以检测到用户是否开始/停止输入? 我使用discord.js Webpack版本12,但似乎找不到任何解决方案?

解决方法

您可以使用typingStart()事件,该事件将在用户开始输入时触发。

client.on('typingStart',(channel,user) => {
  console.log(`${user.username} is typing in ${channel.name}`)
};

还有一种User.typingIn()方法,该方法将检查用户是否在指定频道中键入内容,并返回boolean

if (<user>.typingIn(<channel>)) 
  console.log(`${<user>.username} in typing in ${<channel>.name}`);

(您也可以使用typingDurationIn()typingSinceIn()方法)


最后,您可以查看TextChannel的{​​{3}}属性,以检测是否有人在该频道中键入内容。

if (<channel>.typing)
  console.log(`Somebody is typing in ${channel.name}`);

(您也可以使用TextChannel.typing属性来查看许多人们在给定频道中的输入方式)

,

我遇到了同样的问题,我使用 JavaScript 中内置的 setTimeout() 函数解决了它。这是我的代码片段:

client.on("typingStart",function(channel,user){
    //What you want to execute while someone is typing...
    console.log('User is typing!')
    setTimeout(() => {
        //What you want to execute when someone stops typing...
        console.log('User is no longer typing')
    },5000)
})

您可以根据需要更改值 5000,它代表在某人停止输入后代码等待的时间(以毫秒为单位)。我认为 5000 很好,但这取决于您。