【问题标题】:How to stop MessageCollector with custom message如何使用自定义消息停止 MessageCollector
【发布时间】:2022-01-21 09:17:37
【问题描述】:

我有从用户收集消息的斜杠命令。用户输入游戏中的昵称,bot 应返回embeded 2 队。我设法过滤和收集昵称,并调用计算函数。如果我输入最大数量的昵称,这将起作用,但现在我想添加功能,我将通过发送消息“停止”手动停止收集。现在我不知道在哪里添加条件 if(m.content==="stop") {collector.stop();}

const { SlashCommandBuilder } = require("@discordjs/builders");
const { getSummonerProfile } = require("./../functions/summonerData");
const { MessageEmbed } = require("discord.js");

let arrayOfSummoners = [];
let arrayOfNicknames = [];

async function checkIfSummonerExist(m) {
  const test = await getSummonerProfile(m.content);
    
  if (test && m.author.bot == false && !arrayOfNicknames.includes(m.content)) {
    m.react("????");
    return true;
  } else if (test == false && m.author.bot == false) {
    m.react("????");
    return false;
  }
  if (arrayOfNicknames.includes(m.content)) {
    m.react("????");
  }
}
module.exports = {
  data: new SlashCommandBuilder()
    .setName("custom")
    .setDescription("Enter the name of the user."),
    
  async execute(interaction) {

    await interaction.deferReply();

     interaction.editReply("Insert users");

      const exampleEmbed = new MessageEmbed()
        .setColor("#0099ff")
        .setTimestamp();

    // `m` is a message object that will be passed through the filter function
    const filter = (m) => checkIfSummonerExist(m);
    const collector = interaction.channel.createMessageCollector({
      filter,
      max: 4,
    });
    if(m.content==="stop")
    {
      collector.stop();
    }
    collector.on("collect", (m) => {
      arrayOfNicknames.push(m.content);
      exampleEmbed.addFields({
        name: "Regular field title",
        value: m.content, inline:true
      });
    });
    collector.on("end", (collected) => { 
        interaction.followUp({ embeds: [exampleEmbed] });
    });      
  },
};

如果我在filter fuction (checkIfSummonerExist(m) 中添加此条件,我会收到错误collector is not defined,如果在execute 中调用它,就像上面的示例一样,我会收到错误m is not defined

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    我会将if 语句放在collector.on 回调中。像这样的:

    collector.on("collect", m => {
      if(m.content === "stop") return collector.stop()
      // Other code
    })
    

    【讨论】:

    • 那我还要加过滤器让“stop”通过?
    • 是的,您需要更改过滤器@Lube
    猜你喜欢
    • 1970-01-01
    • 2019-01-14
    • 2011-11-24
    • 1970-01-01
    • 2015-09-22
    • 2019-09-23
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多