Home > other >  How do I make the message collector only be run by the person who executed the command?
How do I make the message collector only be run by the person who executed the command?

Time:08-21

I have this bot, and the bot itself uses a collector in order to collect data from user input. However, whenever someone runs this command to input their own data, someone else can come in and type in anything before the other user can send their message, and the bot will collect that one instead.

Here's an example:

User1: !collect
Bot: What do you want to collect?
User2: A
User1: B
Bot: Collected (A)

If User 2 sent a message before User 1 while the collector is running, then the bot will collect what User 2 has inputted instead of User 1, who is the person that ran the command.

So I've tried handling this by modifying the filter and adding in a logical AND:

const filter = m => m.content.toString() && m.author.id; 
const collector = message.channel.createMessageCollector({
  filter,
  time: 10000,
});

Apparently, this doesn't seem to help and the bot will continue to collect what User 2 has sent instead of User 1 during the collection process.

Or doing the same thing above but I used m.content.match() instead of .toString().

Another thing I tried doing is an odd way to handle it by creating a role with the user ID, making the bot give the role then if the person has that role the collector continues, if not, it just ignores it before at the end of the collection it'll delete the role. That didn't work either.

I was considering making a set to store user ID but that would just result in the same thing as making a new role for it right?

So how do I make the collector only collect the message sent by the person who typed the command?

Rest of code:

 collector.on("collect", async m => {
      if(m.content.toString() == '') {
        message.channel.send("No");
        collector.stop();
      }
  if(!fs.existsSync(`./guild/${message.guild.id   '<@'   message.member.id   '>'}/${args.join(" ")}guild.txt`)) {
    message.channel.send("No info found")
    await fs.writeFile(`./guild/${message.guild.id   '<@'   message.member.id   '>'}/${args.join(" ")}guild.txt`, m.content.toString(), 'utf8', function (err) {
         if (err) throw err;
         });
         collector.stop();
       }
      let guildName = m.content.toString();
      
      if(fs.existsSync(`./guild/${message.guild.id   '<@'   message.member.id   '>'}/${args.join(" ")}guild.txt`)) {
        await fs.writeFile(`./guild/${message.guild.id   '<@'   message.member.id   '>'}/${args.join(" ")}guild.txt`, guildName, 'utf8', function (err) {
         if (err) throw err;
         });
         message.channel.send("Information edited");
         collector.stop();
       }
     }); //Closes start collector

Bot Version: V13

CodePudding user response:

m.content.toString() && m.author.id only checks if there is some content and if there is an author ID. This filter will always return true.

You want to check if the collected message's author is the same as the author of the command. If the message you receive in your event listener is message, the following should work:

const filter = m => m.author.id === message.author.id; 

I'm not sure why you use m.content.toString() though.

  • Related