Skip to content

How to make an discord js bot to send an random message(i make an list and he sends it) at some time in an exact text channel from discord

This is how an comand worksenter image description here

And this is my main.js enter image description here

an little help please?i really need it and i would apreciatte much if you would help me

Answer

First of all you need to find the channel ID. Best to go into the Discord app and right click the channel and select “Copy ID”. It should look something like this: 845346073543326453

Now to send something to that specific channel you have to do this:

const channel = client.channels.cache.get(845346073543326453);
channel.send("hello!")

For the random messages you just create an array and randomly pick one:

const random = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1) + min);
}
let randomMsg = [`Howdy`, `Howdily doodily`, `Zoinks`]
channel.send(quotes[random(0, quotes.length - 1)])

To send it at a specific time there’s many methods. I recommend using the cron package and reference this post: How can I send a message every day at a specific hour?

But if you just want a quick and really low effort way you could just use setInterval() and set the delay to an hour. So we end up with something like this:

const channel = client.channels.cache.get(845346073543326453);
const randomMsg = [`Howdy`, `Howdily doodily`, `Zoinks`]
const random = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

const sendRandomMsg = () => {
  var d = new Date();
  var n = d.getHours();
  if (n === 12) {
    channel.send(randomMsg[random(0, quotes.length - 1)])
  }
}

setInterval(function(){ sendRandomMsg() }, 3600000);

You can add more functions into the if, in case you have more functions to run at specific times.