david
david

Reputation: 192

Discord.js messageCollector won't collect messages

I'm trying to create a discord bot, which can create a thread and after running a command, collect the messages. There is my code:

require('dotenv').config();

const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('start')
        .setDescription('Start the story'),
    async execute(interaction, client) {
        
        ...

        const filter = () => true;

        client.collectors.set(`collector${channel.id}`, channel.createMessageCollector({ filter, time: 30000, max: 1000 }));

        const collector = client.collectors.get(`collector${channel.id}`);

        collector.on('collect', m => {
            console.log(`Collected ${m.content}`);
        });

        collector.on('end', collected => {
            console.log(`Collected ${collected.size} items`);
        });

        ...
    },
};

My first idea was that after the command is ran, the collector will be removed from memory, that's why I gave the client a property, a collection named collectors, but the collect event still won't run and the end event will only run after the thread is deleted, or when the time limit ended, but then it logs "Collected 0 items" even though there were messages sent.

Tried creating it in a function in index.js but that didn't help either.

Update: added () => true as filter, changed nothing...

Upvotes: 2

Views: 475

Answers (1)

david
david

Reputation: 192

I solved the issue by adding GatewayIntentBits.GuildMessages and GatewayIntentBits.MessageContent to the client's intents in index.js, so the line where I create the client object now looks like this:

const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });

Upvotes: 1

Related Questions