Skip to content
Advertisement

Discord.js v13 Bot doesn’t collect messages in DMChannel

I’m trying to make a captcha verification system for my discord bot. So when someone join a server the bot will DM the user a captcha and the user have to solve the captcha in a given time period to get a specific role in the server but my bot doesn’t seem to collect the user’s message. I’ve tried different ways to fix this problem. Here are some ways that I’ve tried :

1. Using createMessageCollector()

        const filter = (message) => {
            if (message.author.id !== member.id) return;
            if (message.content === captcha.text) return true;
            else member.send("Wrong captcha")
        }
        const msg = await member.send({files : [captchaAttachment], embeds: [captchaEmbed], content: captcha.text })

        const collector = msg.channel.createMessageCollector({ filter, max: 1, time: 10000, errors: ["time"]})

        collector.on('collect', () => {
            member.roles.add("1009431897694294017");
            member.send("You have been verified")
        })

        collector.on('end', async () => {
            await member.send("you have not been verified and have kicked from the server")
            member.kick()
        })

2. Using awaitMessages() with try…catch

        const filter = (message) => {
            if (message.author.id !== member.id) return;
            if (message.content === captcha.text) return true;
            else member.send("Wrong captcha")
        }
        const msg = await member.send({files : [captchaAttachment], embeds: [captchaEmbed], content: captcha.text })
        try {
            const response = await msg.channel.awaitMessages({filter: filter, max: 1, time: 10000, errors: ["time"]})
            if (response) {
                member.roles.add("1009431897694294017")
                member.send("You have been verified")
            }
        }
        catch(e) {
            await member.send("you have not been verified and have kicked from the server")
            member.kick()
        }

3. Using awaitMessages() with Promise

        const filter = (message) => {
            if (message.author.id !== member.id) return;
            if (message.content === captcha.text) return true;
            else member.send("Wrong captcha")
        }
        await member.send({files : [captchaAttachment], embeds: [captchaEmbed], content: captcha.text })
            .then((msg) => {
                msg.channel.awaitMessages({ filter, max: 1, time: 10000, errors: ["time"] })
                    .then(() => {
                        member.roles.add("1009431897694294017");
                        member.send("You have been verified")
                    })
                    .catch(async () => {
                        await member.send("you have not been verified and have kicked from the server")
                        member.kick()
                    })
            })

All of the snippets above have the same result as shown in this picture : Captcha Bot error

As you can see the user have already solved the captcha but the bot didn’t collect the message so it threw an error.

I’ve also tried removing the filter because I thought that’s the source of the problem but that didn’t seem to resolve it.

I also noticed that I only got this problem in DMChannel. I’ve tried collecting messages in TextChannel via Interaction and the Message Collectors worked perfectly fine.

Advertisement

Answer

I have finally resolved the issue. Turned out it had something to do with the Discord Intents that I was using. Here is the intents that I was using :

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS] });

Then I realized that I actually need to add the DIRECT_MESSAGES Intent. So the right Client instance should be like this :

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.DIRECT_MESSAGES] });
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement