Search code examples
javascriptnode.jsdiscord.js

DiscordJS Collector don´t Collect Message


My DiscordJS Interaction Message Collector doesn´t collect a Message. After the time ends, the bot only tells me "0 Items Collected"

const { EmbedBuilder, MessageReaction } = require("discord.js");
const { registerFont, createCanvas, loadImage } = require('canvas');
const scale = 20;
const canvas = createCanvas(171*scale,138*scale);
const fs = require('fs');
const sql = require('./../self-modules/sql.js');

const filter = m => m.content.includes('discord');

exports.run = async (client, interaction, dbcon) => {
    interaction.reply({content: "test", fetchReply: true}).then( (message) =>{
        const filter = (reaction, user) => {
            return reaction.emoji.name === '👍' && user.id === message.author.id;
        };
        
        const collector = message.createReactionCollector({ filter, time: 15000 });
        
        collector.on('collect', (reaction, user) => {
            console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
        });
        
        collector.on('end', collected => {
            console.log(`Collected ${collected.size} items`);
        });
    });
}

Output (nothing else):

Collected 0 items

Here is my code, can somebody help?

my goal is, that after a user executed the Slash-Command, the Slash ask a question to the user and than a answer is required.

Thanks

#################################################################

I also tried this:

const { EmbedBuilder, MessageReaction } = require("discord.js");
const { registerFont, createCanvas, loadImage } = require('canvas');
const scale = 20;
const canvas = createCanvas(171*scale,138*scale);
const fs = require('fs');
const sql = require('./../self-modules/sql.js');

const filter = m => m.content.includes('discord');

exports.run = async (client, interaction, dbcon) => {
        interaction.reply({content: "test", fetchReply: true}).then( (message) =>{
            interaction.channel.awaitMessages({ max: 1, time: 30000, errors: ['time'] })
                .then(collected => {
                    interaction.followUp(`${collected.first().author} got the correct answer!`);
                })
                .catch(collected => {
                    interaction.followUp('Looks like nobody got the answer this time.');
                });
        });
}

Same result


Solution

  • As I mentioned in my comment above, using user.id === message.author.id in your filter will let you collect reactions made by the bot only as message.author is your bot. If you want to accept reactions from the user who used the command, you can compare user.id to the interaction.user.id.

    If it still doesn't work like that, you'll need to check the intents you enabled. You will need Guilds, GuildMessages, and GuildMessageReactions to collect reactions:

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

    The following should work as expected:

    exports.run = async (client, interaction, dbcon) => {
      interaction.reply({ content: 'test', fetchReply: true }).then((message) => {
        const filter = (reaction, user) =>
          reaction.emoji.name === '👍' && user.id === interaction.user.id;
    
        const collector = message.createReactionCollector({
          filter,
          time: 15000,
        });
    
        collector.on('collect', (reaction, user) => {
          console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
        });
    
        collector.on('end', (collected) => {
          console.log(`Collected ${collected.size} items`);
        });
      });
    };