简体   繁体   English

消息收集器不工作 - discord.js

[英]Message collector not working - discord.js

This is my attempt of making an interview command.这是我尝试发出采访命令。 First the user has to type !start in order for the bot to start an interview of 20 questions.首先,用户必须输入!start才能让机器人开始面试 20 个问题。 When the user calls this command the bot asks the first question then the user has to answer any question in order for the bot to continue to the 2nd question and so on.当用户调用此命令时,机器人会询问第一个问题,然后用户必须回答任何问题才能让机器人继续回答第二个问题,依此类推。

The collector is supposed to send a question after an answer is given but it's not working at all.收集器应该在给出答案后发送一个问题,但它根本不起作用。

Code:代码:

const Discord = require('discord.js');
const client = new Discord.Client();
const serverInfo = require('../info.json');
const role = serverInfo.wlRole; // The id of the whit-listed role
const ch = serverIngo.wlChannel; // the id of the white-listed channel for the command to execute
const questions = [
  '1',
  '2',
  '3',
  '4',
  '5',
  '6',
  '7',
  '8',
  '9',
  '10',
  '11',
  '12',
  '13',
  '14',
  '15',
  '16',
  '17',
  '18',
  '19',
  '20',
];

const answers = [];
let num = 0;

module.exports = {
  name: 'start',
  description: 'starts the conversation between the user and the bot',
  usage: '!start',
  execute(message, args) {
    try {
      message.delete({
        timeout: 1000
      });
      if (message.channel.id !== ch) return;

      const dude = message.guild.members.cache.get(message.author.id);

      if (!dude.roles.cache.has(role)) return;

      message.author.send('just hang on a sec:clock3: ');

      const filter = (m) => m.author.id === message.author.id;
      if (num === 0) message.author.send(questions[num]);
      // the collector is supposed to send a question after an answer is given but it's not working

      const collector = new Discord.Collector(message.author.dmChannel, filter);
      collector.on('collect', (msg) => {
        answers.push(msg.content);
        message.author.send(questions[num]);
        console.log(answers);
        num++;
      });
    } catch (error) {
      console.log(error);
    }
  },
};

Try something like this:尝试这样的事情:

 const collector = message.channel.createMessageCollector(filter, { time: 15000 }); var questioncount = 0; var authorID = message.author.id; collector.on('collect', m => { if(authorID.= message.author;id) return; questioncount++. if(questioncount == 1){ // DO STUFF 1 } //..; if(questioncount == 20){ // DO STUFF 20 } });

Its just an example so its not the best code.它只是一个例子,所以它不是最好的代码。 Its tho a possible way.这是一种可能的方式。

First of all, you can move the answers inside the module's execute method.首先,您可以在模块的execute方法中移动答案。

Instead of checking if (num === 0) , you can safely send the first message as that part of the code only runs when a user types !start .无需检查if (num === 0) ,您可以安全地发送第一条消息,因为该部分代码仅在用户键入!start时运行。 The rest of the messages are sent from the collector.消息的 rest 是从收集器发出的。

Instead of using new Discord.Collector() , you could use dm.channel.createMessageCollector where dm is the returned message after you sent out the first direct message before the first question.您可以使用dm.channel.createMessageCollector而不是使用new Discord.Collector() ,其中dm是在第一个问题之前发送第一条直接消息后返回的消息。 You can also set the max option to the number of questions, so once the user answered all of them, the collector's end event fires so you don't have to manually check for every answer if it was the last question.您还可以将max选项设置为问题的数量,因此一旦用户回答了所有问题,收集器的end事件就会触发,因此您不必手动检查每个答案是否是最后一个问题。

Check out the working code below.查看下面的工作代码。

const Discord = require('discord.js');
const client = new Discord.Client();
const serverInfo = require('../info.json');
const role = serverInfo.wlRole; // The id of the whit-listed role
const ch = serverIngo.wlChannel; // the id of the white-listed channel for the command to execute

module.exports = {
  name: 'start',
  description: 'starts the conversation between the user and the bot',
  usage: '!start',
  async execute(message, args) {
    const questions = [
      '1',
      '2',
      '3',
      '4',
      '5',
      '6',
      '7',
      '8',
      '9',
    ];

    const answers = [];
    let current = 0;

    try {
      message.delete({ timeout: 1000 });
      if (message.channel.id !== ch) return;

      const dude = message.guild.members.cache.get(message.author.id);

      if (!dude.roles.cache.has(role)) return;

      message.author.send('just hang on a sec:clock3:');

      const filter = (m) => m.author.id === message.author.id;
      const dm = await message.author.send(questions[current++]);

      const collector = dm.channel.createMessageCollector(filter, {
        max: questions.length,
      });

      collector.on('collect', (msg) => {
        const currentQuestion = questions[current++];

        answers.push(msg.content);
        console.log({ answers });

        if (currentQuestion) {
          message.author.send(currentQuestion);
        }
      });

      collector.on('end', (collected, reason) => {
        if (reason === 'limit') {
          message.author.send(
            `Cheers ${message.author}, that's all for today.`,
          );
        }
      });
    } catch (error) {
      console.log(error);
    }
  },
};

在此处输入图像描述

PS: I know it's a late answer, and you've probably already solved it. PS:我知道这是一个迟到的答案,你可能已经解决了。 I've just found this tab open in my browser and thought I would help you out:)我刚刚在我的浏览器中打开了这个标签,我想我会帮你的:)

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

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