Home > Software engineering >  My bot is collecting it's own message (infinite loop) and spamming an specific line
My bot is collecting it's own message (infinite loop) and spamming an specific line

Time:03-28

the bot spams the (`You Found ${earnings} Coins!`); and I think it's collecting it's own messages (infinite loop) I asked the problem from some of my friends and they said the same: "the bot is collecting it's own messages, remove the collector". but I don't know how can I replace the collector.

            const profileModel = require("../models/profileSchema");

            module.exports = {
                name: "search",
                aliases: [],
                permissions: [],
                description: "Search for some coin!",
                async execute(message, args, cmd, client, Discord, profileData) {
            
                    const locations = [
                        "car",
                        "bathroom",
                        "park",
                        "truck",
                        "pocket",
                        "computer"
                    ];
            
                    const chosenLocations = locations.sort(() => Math.random() - Math.random()).slice(0, 3);
            
                    const filter = ({ author, content }) => message.author == author && chosenLocations.some((location) => location.toLowerCase() == content.toLowerCase());
            
                    const collector = message.channel.createMessageCollector(filter, { max: 1});
            
                    const earnings = Math.floor(Math.random() * (1000 - 100   1))   100;
            
            
                    collector.on('collect', async (m) => {
                        if(message.author.bot) return;
                        message.channel.send(`You Found ${earnings} Coins!`);
            
                        await profileModel.findOneAndUpdate(
                            {
                                userID: message.author.id,
                            },
                            {
                                $inc: {
                                    PDMcoins: earnings,
                                },
                            }
                        );
                    });
            
            
            
                    message.channel.send(`<@${message.author.id}> which location would you like to search?\n type it in this channel\n \`${chosenLocations.join('` `')}\``);
                }
            }

CodePudding user response:

In discord.js v13 all Collector related classes and methods (e.g. .createMessageCollector() or .awaitMessages()) take a single object parameter which also includes the filter. So, your collector should look like this:

const collector = message.channel.createMessageCollector({ filter, max: 1 });
  • Related