简体   繁体   English

如何使消息收集器仅由执行命令的人运行?

[英]How do I make the message collector only be run by the person who executed the command?

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.如果在收集器运行时用户 2用户 1之前发送了一条消息,那么机器人将收集用户 2输入的内容,而不是运行命令的用户 1。

So I've tried handling this by modifying the filter and adding in a logical AND :所以我尝试通过修改过滤器并添加逻辑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.显然,这似乎没有帮助,机器人将在收集过程中继续收集用户 2而不是用户 1发送的内容。

Or doing the same thing above but I used m.content.match() instead of .toString() .或者在上面做同样的事情,但我使用m.content.match()而不是.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.我尝试做的另一件事是通过使用用户 ID 创建一个角色来处理它它将删除角色的集合。 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?我正在考虑设置一组来存储用户 ID,但这只会导致与为其设置新角色相同的结果,对吗?

So how do I make the collector only collect the message sent by the person who typed the command?那么如何让收集器收集输入命令的人发送的消息呢?

Rest of code: Rest 代码:

 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机器人版本: V13

m.content.toString() && m.author.id only checks if there is some content and if there is an author ID. m.content.toString() && m.author.id仅检查是否有某些content以及是否有作者 ID。 This filter will always return true .此过滤器将始终返回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:如果您在事件侦听器中收到的消息是message ,则以下内容应该有效:

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

I'm not sure why you use m.content.toString() though.我不确定你为什么使用m.content.toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM