Discord.JS |在 DM 中等待反应

时间:2021-03-17 12:48:44

标签: discord.js

我不希望机器人加入我的服务器并在用户的聊天或 dm 中做广告,所以我想我会让新用户对消息做出反应,然后我的机器人会在 dm 中向他们发送问题,如果他们回答正确,他应该给他们经过验证的角色。机器人可以发送消息,但之后会出现错误。

(node:12356) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'type' of undefined

我不知道如何解决这个问题,因为在 Docs 中存在 channel.type。

这是代码

client.on('messageReactionAdd', async (reaction, user) =>{
    const filter = m => true

    if (reaction.message.partials) await reaction.message.fetch();
    if (reaction.partials) await reaction.fetch();

    if (user.bot) return;
    if (!reaction.message.guild) return;

    if (reaction.message.id === '814035246399488001'){
        if (reaction.emoji.name === '✅'){
            user.send("What is the subtrahend of 156 - 123?")
            .catch(()=>{console.log('couldnt send a DM')})
            if ( channel.type === 'dm' )
            user.dmChannel.awaitMessages(filter, {
                max:1,
                time:30000,
                error:["time"]
            })
            .then(collected => {
                if (collected.first().content === "33"){
                    reaction.message.guild.members.cache.get(user.id).roles.add('779523805492019254')         
                    if(err => {
                        console.log(err)
                    });
                }else{
                    user.send("this is not the correct answer, you will not get verified.")
                    return;
                }
            })
        }
    }
});

1 个答案:

答案 0 :(得分:0)

我在一些不和谐的服务器上询问并找到了解决方案:

if (reaction.emoji.name === '✅'){
            let msg = await user.send("What is the subtrahend of 156 - 123?")
            .catch(()=>{console.log('couldnt send a DM')})
            if (msg.channel.type == `dm`)

我认为发生的事情是,机器人现在知道 msg.channel.type 是他之前发送消息的频道,是的,我很高兴现在它可以工作了 :3 也谢谢你 NullDev 你的评论领导解决方案也<3