我希望制作一个随机问题机器人来解决问题,但我不知道出了什么问题

时间:2019-06-02 05:05:23

标签: javascript discord

我希望制作一个不和谐的机器人,提出比我们更好的问题。目的是我可以定期更新问题。我已经解决了托管我的机器人的问题,并让它加入了测试服务器。但是我想不出一种方法来让它调用列表中的随机问题。我所能做的就是在输入?q后使它以设定的单词回复。

此外,很抱歉,如果发布的内容太多,我只是加入了StackOverflow。

我尝试使用随机数生成器,然后将其结果用作变量,然后将该变量用作问题。就是1-100之间的随机数,如果随机数是#,则msg.question。我至少知道每个问题都需要有数字,所以如果结果是数字50,那么将显示与50关联的问题。

var magic8Ball = {};
magic8Ball.listofquestions = ["It is certain.", "It is decidedly so.", "Without a doubt.", "Yes, definitely.", "You may rely on it.", "As I see it, yes.", "Most likely.", "Outlook good.", "Yes.", "Signs point to yes.", "Reply hazy, try again.", "Ask again later.", "Better not tell you now.", "Cannot predict now.", "Concentrate and ask again.", "Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful."];

magic8Ball.getAnswer = function(question) {
  var randomNumber = Math.random();
  var randomAnswer = Math.floor(randomNumber * this.listofquestions.length);
  var answer = this.listofquestions[randomAnswer];

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === '?q') {
    // msg.reply('pong');
    function() {
      magic8Ball.getAnswer(question);
    };
  }
});

client.login(auth.token);

我希望它显示随机问题,但在部署机器人时我的cmd中出现此错误

1 个答案:

答案 0 :(得分:0)

我已经修改并编辑了您的代码。

listofquestions = ["It is certain.", "It is decidedly so.", "Without a doubt.", "Yes, definitely.", "You may rely on it.", "As I see it, yes.", "Most likely.", "Outlook good.", "Yes.", "Signs point to yes.", "Reply hazy, try again.", "Ask again later.", "Better not tell you now.", "Cannot predict now.", "Concentrate and ask again.", "Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful."];

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === '?q') {
     msg.reply(listofquestions[Math.floor(Math.random() * listofquestions.length)]);

  }

client.login(auth.token);

如果您需要任何其他帮助或任何疑问的澄清,我会根据您的需求修改答案。