简体   繁体   中英

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 works在此处输入图像描述

And this is my main.js 在此处输入图像描述

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM