0

I'm trying to get my discord bot to randomly pick messages from a json file and send it every 5*1000 seconds the message interval is working fine earlier.But its sending the same message everytime. Where can i be going wrong?

here is the app.js

const Discord = require('discord.js');
const factDB = require("./replies.json");
const PREFIX = '!';
const client = new Discord.Client();
const fetch = require("node-fetch");


client.once('ready',() =>{
console.log('online!');
});


client.on('message', msg => {
            if (msg.content === '?finit') {
                const REP = require("./replies.json");
                const item = REP[Math.floor(Math.random() * REP.length)];
                setInterval(() =>{
                msg.channel.send(item.facts);
            }, 5 * 1000);
            }
        });


client.login('token')

here is the json file

[
    {
        "facts": "1"
       
    },

    {
        "facts": "2"
    
    },
    {
        "facts": "3"
        
    },
    {
        "facts": "4"
    
    },
    {
        "facts": "5"
        
    }
]

Thank you for reading.

2
  • provide a little bit more of a content to test it trough. Like a screenshot of discord messages with time intervals and etc. You time might have a too big of a gap. Commented Oct 20, 2020 at 9:20
  • 1
    Thank you thomas but its working now thanks to Jayke Commented Oct 20, 2020 at 9:23

1 Answer 1

1

Your random response is generated only once when the command is being executed. You need to move the following line in the setInterval function:

const item = REP[Math.floor(Math.random() * REP.length)];

setInterval(() => {
    const item = REP[Math.floor(Math.random() * REP.length)];
    msg.channel.send(item.facts);
}, 5 * 1000);

This will pick a random response every 5 * 1000 ms.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.