0

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

adiga
  • 34,372
  • 9
  • 61
  • 83
  • Sorry, forgot to add the result. – Nick Raston Jun 02 '19 at 05:06
  • Possible duplicate of [Getting a random value from a JavaScript array](https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array) – Mike Doe Jun 02 '19 at 05:10
  • This is what I got when I had a working simple message/reply bot. C:\Users\morning\Documents\Bot\truthortruth>node bot.js . Logged in as ToT#2092! – Nick Raston Jun 02 '19 at 05:11
  • What I got after C:\Users\morning\Documents\Bot\truthortruth>node bot.js C:\Users\morning\Documents\Bot\truthortruth\bot.js:21 function() ^ SyntaxError: Unexpected token ( [90m at Module._compile (internal/modules/cjs/loader.js:718:23)[39m [90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)[39m [90m at Module.load (internal/modules/cjs/loader.js:641:32)[39m [90m at Function.Module._load (internal/modules/cjs/loader.js:556:12)[39m – Nick Raston Jun 02 '19 at 05:15
  • [90m at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)[39m [90m at internal/main/run_main_module.js:17:11[39m These results being the cmd deployment for the bot. – Nick Raston Jun 02 '19 at 05:16

1 Answers1

0

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.

Agoose Banwatti
  • 410
  • 2
  • 13
  • Thank you. I was wondering also which part it uses to mention the person who uses the ?q to get their question. The answer you gave does exactly what I want it to do, I don't know which part adds the mention of the person. – Nick Raston Jun 03 '19 at 02:14