简体   繁体   中英

I wish to make a random question bot for discord, but I can't figure out what's wrong

I wish to make a discord bot that asks better questions than the one we have. The intent is that I can update the questions regularly. I have worked out hosting my bot and have had it join a test server. But I can't figure out a way to have it call on a random question from a list. All I can do is make it reply with a set word after inputting ?q.

Also, sorry if this is too much to post, I only just joined StackOverflow.

I have tried using a random number generator then having it's result be used to be used as a variable, that variable then being the question. Ie. random number between 1-100, if random number is # then msg.question. I know at least that I would need to have numbers for each question, so if it were the number 50 that were the result then the question that 50 is associated with will be displayed.

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

I wanted it to show random questions but I get this error in my cmd when I deploy the bot

I have revised and edited your code.

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

if you need any other help or clarification on anything feel free to ask and I will edit my answer to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM