T. K.

asked • 03/23/21

Discord.js random message after x amount of time (Code Correction)

I need to have my discord bot send a random message every 1.5 hours (1 second here for testing) and so far I used the following code but it did not work:


'use strict';


const { Client, MessageEmbed } = require('discord.js');


const client = new Client();



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


var facts = ['Zebra', 'Elephant', 'Goose', 'Lion', 'Turtle', 'Mongoose']
setInterval(function() {
var fact = Math.floor(Math.random() * facts.length)
console.log(facts[fact])
}, 1000) // Changed to 1s for testing


client.on('message', message => {

if (message.content != '101') {
message.channel.send(facts[fact]);
}
});

client.login(process.env.DISCORD_TOKEN);

I really would appreciate it if you helped me out and sorry because I am just a beginner to node.js, e.t.c.

Thanks!

1 Expert Answer

By:

Ben S. answered • 05/11/21

Tutor
5 (3)

Full stack developer specializing in React and Rails

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.