Guide on creating a Discord bot using discord.js to send a random message from a predefined list at a specific time within a designated text channel on Discord

Here is an illustration of how a command functions https://i.sstatic.net/53fqU.png

Also, here is my main.js file https://i.sstatic.net/jKLPR.png

I need some assistance with this. Your help would be greatly appreciated.

Answer №1

First step is to locate the channel ID. Open the Discord app, right click on the channel, and choose "Copy ID". The ID will look something like this: 845346073543326453

Next, to send a message to that particular channel, use the following code:

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

For sending random messages, create an array and pick one randomly:

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 schedule messages at specific times, there are several methods. You can consider using the cron package, or refer to this post: How can I send a message every day at a specific hour?

If you prefer a quick and simple approach, you can use setInterval() and set the delay to one hour. Here's an example:

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 include more functions within the if statement if you have additional tasks to perform at specific times.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What exactly do Dependencies mean in Angular?

As a beginner in Angular, the concept of dependencies has come up several times during my learning process. Despite my efforts to search for a clear definition of Dependencies in Angular on Google, I have been unsuccessful. Could someone please provide a ...

Developing JSON with the use of jQuery

My latest project involves creating an application that converts HTML tables to JSON format. The code is functioning properly, but I've encountered an issue when the td or th elements contain inner components like span or other child elements. While I ...

In JavaScript, eliminate all characters in a string after the second hyphen using only one line of code

Is it possible to extract the language from a string before the 2nd occurrence of a dash (-) using a single line of Javascript? For example: en-AU-Option-A would become en-AU ...

Guide on dynamically loading email contacts options in selectize.js

I have been working with selectize.js to create an email contacts feature. However, I am facing an issue where the mail list retrieved from the database is displaying as undefined in selectize. Strangely, when I manually enter the return value, everything ...

How can Swipe support help you slide a menu back in?

For implementing swipe support on my landing page carousel, I included jquery.mobile.custom.min.js. However, I am facing a challenge with adding swipe support to "close" the menu. Essentially, swiping left should have the same effect as clicking the butto ...

Exploring the Dynamic Routing Features of Next.js

After diving into Next.js, I found the learning curve to be manageable since React was already familiar territory for me. Currently, my focus is on implementing dynamic routing in my application - Starting with my live/index.js file - (Where I utilize ge ...

excluding a library when creating a package

Hey everyone, I'm relatively new to npm, react, and webpack and I have a question. When using npm, how can you avoid including a library in the production package file? For instance, I'm working on a small react component that will be placed on a ...

form_not_submitting_ajax

In the app I'm working on, I have a form that is defined in the following manner: = form_with model: project, remote: true, method: :put do |f| = f.select :selected_draw, options_for_select(project.draws.pluck(:number, :id), draw.id), {}, class: &a ...

Unlimited Caching: A Guide to Storing HTTP Responses permanently

Is there a way to send an HTTP response that will be cached by any client indefinitely, so that the browser can retrieve it from the local file system without making any new HTTP requests when needed? Just imagine using this for versioned client code in a ...

Updating the handler function for AutoComplete with Checkbox in Material UI using React JS

I am looking to include an <AutoComplete /> field in my form. The options for this field are fetched through a get request, and here is the result displayed in the console. [ { "uid": "c34bb0ed-9f63-4803-8639-a42c7e2a8fb0&q ...

Where exactly is the destination for the package being installed by running "npm install -g <package>"?

After utilizing npm install -g, I found myself wondering where the packages were actually installed. Upon running npm install, it seems that the packages are installed in the current directory under node_modules. I comprehend the usage of the -g flag fro ...

Stopping form submission on a jQuery form

I am in the process of implementing a password control feature on a login form using jQuery and ajax. This is the current script I have: $(document).ready(function() { $("#login-form").submit(function(e) { var csrftoken = getCookie('csr ...

What are the steps for setting up React on a browser?

Recently, I decided to dive into the world of React framework and experiment with building a web app using Tailwind CSS. After following these steps successfully: npx create-react-app my-project cd my-project npm install -D tailwindcss@npm:@tailwindcss/pos ...

Filter out specific fields from an object when populating in MongoDB using the aggregate method

Is there a way to use the populate() function in MongoDB to exclude specific fields like email and address, and only retrieve the name? For example: const results = await Seller.aggregate(aggregatePipeline).exec(); const sellers = await Seller.populate(re ...

Is it possible to dictate a custom sequence of operations in THREE.js?

Check out this question on Stack Overflow regarding Threejs Transform Matrix ordering: Threejs Transform Matrix ordering I'm currently working on depicting the orbits of planets and I've encountered some difficulties when trying to change the in ...

What is the recommended value for the auto-incremented ID field when using the POST method in Node.js?

Currently, I am utilizing the mysql package for NodeJs in order to Post Data to a MySqlDB. Below is an example of my code: app.post('/countries', (req, res) => { const country = req.body.country; const city = req.body.city; const t ...

Unable to click a button on HTML file

In my current project, there is a piece of code responsible for checking if the user is logged in or not. If the user hasn't logged in yet, they are redirected to the login page. Once the user logs in successfully, they should be able to upload conten ...

Is Python being used to track NBA.com stats?

Does anyone have any advice on how to extract NBA.com "tracking" stats using a python script and display them in a simple table? I'm particularly struggling with this specific section of stats on the site. Accessing stats directly from NBA.com can be ...

Issues with closures in JavaScript

Struggling to grasp closure with 3 levels of scopes. https://jsfiddle.net/Ar2zee/wLy8rkyL/1/ How can I retrieve the value of parameter "g" within the level3 function? var a = 10; function level1(b) { var c = 1; function level2(f) { var d = 2 ...

Why is my Node.js Connect middleware Limit feature not functioning as expected?

Having an issue with server: var connect = require('connect'); var http = require('http'); var app = connect() .use(connect.limit('32kb')) .use(connect.urlencoded()) .use(connect.json()) .use(function(req, res){ console. ...