簡體   English   中英

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

[英]Message collector not working - discord.js

這是我嘗試發出采訪命令。 首先,用戶必須輸入!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);
    }
  },
};

嘗試這樣的事情:

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

它只是一個例子,所以它不是最好的代碼。 這是一種可能的方式。

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

無需檢查if (num === 0) ,您可以安全地發送第一條消息,因為該部分代碼僅在用戶鍵入!start時運行。 消息的 rest 是從收集器發出的。

您可以使用dm.channel.createMessageCollector而不是使用new Discord.Collector() ,其中dm是在第一個問題之前發送第一條直接消息后返回的消息。 您還可以將max選項設置為問題的數量,因此一旦用戶回答了所有問題,收集器的end事件就會觸發,因此您不必手動檢查每個答案是否是最后一個問題。

查看下面的工作代碼。

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:我知道這是一個遲到的答案,你可能已經解決了。 我剛剛在我的瀏覽器中打開了這個標簽,我想我會幫你的:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM