0
votes

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.

1
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.Thomas J.
Thank you thomas but its working now thanks to JaykeLuis Maximus

1 Answers

1
votes

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.