Bots 如何在Discord.js bot中创建动态聊天?

Bots 如何在Discord.js bot中创建动态聊天?,bots,discord,discord.js,Bots,Discord,Discord.js,我指的是像React for Roles bot one这样的聊天:当您执行命令rr!reactionrole为了创建一个新的反应角色,它要求您输入一个频道,在您回答完一条全新的消息后,编辑机器人的消息,您必须在另一条新消息中回答新问题。这怎么可能?我希望我的解释是正确的。多亏了从某种意义上说,实现这一点的方法是为bot读取消息的每个通道保持一个“状态”。其想法是,每当BOT从某个频道获得新消息时,它首先检查它是否已经在会话的中间。根据此状态的值和输入,bot将执行相应的操作 var state

我指的是像React for Roles bot one这样的聊天:当您执行命令
rr!reactionrole
为了创建一个新的反应角色,它要求您输入一个频道,在您回答完一条全新的消息后,编辑机器人的消息,您必须在另一条新消息中回答新问题。这怎么可能?我希望我的解释是正确的。多亏了

从某种意义上说,实现这一点的方法是为bot读取消息的每个通道保持一个“状态”。其想法是,每当BOT从某个频道获得新消息时,它首先检查它是否已经在会话的中间。根据此状态的值和输入,bot将执行相应的操作

var state = 0;

//on rr!reactionrole
{
switch(state) {
 case 0: //start the conversation and change the state to 1; break;
 case 1: //continue the conversation, edit the message, set state to 2; break;
 //other cases
}
}

Discord.js有一个方法可用于此目的。一旦您将其设置为
文本频道
,它将根据和收集消息

也就是说,要获得答案,但要无缝地编辑原始问题消息,只需在存储的消息ID方法上使用

例如:

const questions = ['What role?', 'What message?', 'What emoji?'];

const question = await message.channel.send(questions[0]); // store the question message object to a constant to be used later

const filter = msg => msg.author.id === message.author.id; // creates the filter where it will only look for messages sent by the message author
const collector = message.channel.createMessageCollector(filter, { time: 60 * 1000 }); // creates a message collector with a time limit of 60 seconds - upon that, it'll emit the 'end' event

const answers = []; // basic way to store to the answers, for this example

collector.on('collect', msg => { // when the collector finds a new message
  answers.push(msg.content);
  questions.shift();
  if (questions.length <= 0) return collector.stop('done'); // sends a string so we know the collector is done with the answers
  question.edit(questions[0]).catch(error => { // catch the error if the question message was deleted - or you could create a new question message
    console.error(error);
    collector.stop();
  });
});

collector.on('end', (collected, reason) => {
  if (reason && reason === 'stop') {
    // when the user has answered every question, do some magic
  }
  message.channel.send('You did not answer all the questions in time or the original message was deleted!');
});
const questions=[“什么角色”、“什么消息”、“什么表情符号”];
const question=wait message.channel.send(问题[0]);//将问题消息对象存储到一个常量中,以便以后使用
const filter=msg=>msg.author.id==message.author.id;//创建筛选器,在其中仅查找由消息作者发送的消息
const collector=message.channel.createMessageCollector(过滤器,{time:60*1000});//创建一个时间限制为60秒的消息采集器-在此之后,它将发出“end”事件
常量answers=[];//存储答案的基本方法,例如本例
collector.on('collect',msg=>{//当收集器发现新消息时
答案。推送(msg.content);
问题。shift();
if(questions.length{//如果问题消息被删除,则捕获错误-或者您可以创建新的问题消息
控制台错误(error);
收集器。停止();
});
});
收集器.on('结束',(已收集,原因)=>{
如果(原因和原因=='stop'){
//当用户回答完所有问题后,做一些魔术
}
message.channel.send('您没有及时回答所有问题,或者原始邮件已被删除!');
});
注意:我还没有测试过这个,它的制作也不是很好,但是你应该能够根据自己的使用来调整它。我建议这样做,这将解释更多关于异步收集器和更多有趣的东西