【问题标题】:Message collector not working - discord.js [duplicate]消息收集器不起作用-discord.js [重复]
【发布时间】:2021-05-12 06:29:03
【问题描述】:

这是我做采访命令的尝试。首先,用户必须输入!start 才能让机器人开始对 20 个问题的采访。当用户调用此命令时,机器人会询问第一个问题,然后用户必须回答任何问题才能让机器人继续回答第二个问题,依此类推。

收集器应该在给出答案后发送一个问题,但它根本不起作用。

代码:

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);
    }
  },
};

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    试试这样的:

    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
        }
    });

    这只是一个例子,所以它不是最好的代码。这是一种可能的方式。

    【讨论】:

      【解决方案2】:

      首先,您可以在模块的execute 方法中移动答案。

      您可以安全地发送第一条消息,而不是检查if (num === 0),因为该部分代码仅在用户键入!start 时运行。其余消息由收集器发送。

      您可以使用dm.channel.createMessageCollector 代替new Discord.Collector(),其中dm 是您在第一个问题之前发送第一条直接消息后返回的消息。您还可以将max 选项设置为问题的数量,因此一旦用户回答了所有问题,收集器的end event 就会触发,因此您不必手动检查每个答案是否是最后一个问题。

      查看下面的工作代码。

      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:我知道这是一个迟到的答案,你可能已经解决了。我刚刚在我的浏览器中发现了这个标签页,我想我会帮你的:)

      【讨论】:

        猜你喜欢
        • 2021-03-31
        • 2020-12-30
        • 1970-01-01
        • 2022-01-21
        • 2021-04-20
        • 2020-10-25
        • 2019-07-30
        • 2022-11-17
        • 2022-01-12
        相关资源
        最近更新 更多