Having trouble retrieving the client in my discord.js command file

Here is the code for my reaction role:


module.exports = {
  name: "rr",
  desc: "create a reaction role --> <",
  async run(message, client) {
    const jsEmoji = "🍏";
    const pythonEmoji = "🍍";

    let embedMessage = await message.channel.send("React to get a role");
    embedMessage.react(jsEmoji);
    embedMessage.react(pythonEmoji);

    client.on("messageReactionAdd", (user, reaction) => {
      console.log("User reacted to message");
    });
  },
};

And here is the setup in my bot file:

const intents = new Discord.Intents(32767);

I created my client instance below:

const client = new Discord.Client({ intents: intents });

const fs = require("fs");
const commandFolders = fs.readdirSync("./Commands");
client.commands = new Discord.Collection();

commandFolders.forEach((folder) => {
  const commandFiles = fs
    .readdirSync(`./Commands/${folder}`)
    .filter((file) => file.endsWith(".js"));
  commandFiles.forEach((file) => {
    const command = require(`./Commands/${folder}/${file}`);
    client.commands.set(command.name, command);
  });
});

client.on("messageCreate", (message) => {
  if (message.author.bot || !message.content.startsWith(config.prefix)) return;

  const [command, ...args] = message.content
    .substring(config.prefix.length)
    .split(/\s+/);

I attempted to access the client and pass it into the run function but encountered an issue.


  client.commands.forEach((cmd) =>
    command === cmd.name
      ? client.commands.get(cmd.name).run(message, args, client)
      : null
  );
});

client.login(config.token);

However, I encountered an error:

    client.on("messageReactionAdd", (user, reaction) => {
           ^
TypeError: client.on is not a function

Does anyone have suggestions on another method to access my client?

Answer №1

Your points are invalid regarding the command file.

The recommended syntax is to utilize async run(client, message, args)

Below is a complete code snippet for your reference:

module.exports = {
  name: "rr",
  desc: "create a reaction role -> <",
  async run(client, message, args) {
    const jsEmoji = "🍏";
    const pythonEmoji = "🍍";

    let embedMessage = await message.channel.send("react for a role");
    embedMessage.react(jsEmoji);
    embedMessage.react(pythonEmoji);

    client.on("messageReactionAdd", (user, reaction) => {
      console.log("he reacted");
    });
  },
};

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 is the process of changing a number to the double data type in JavaScript or TypeScript?

Within a single input field, users can enter various numbers such as 12, 12.1, 12.30, and 12.34. The challenge is to pass this value in a service call where only the value can be sent as a number but with two decimal points. let a = input //a will be a ty ...

How to change a string from utf-8 to iso-8859-1 using Javascript

Although it may seem unpleasant, it is essential. I am facing an issue with a HTML form on my website that uses utf-8 charset but is sent to a server operating with iso-8859-1 charset. The problem arises when the server fails to interpret characters commo ...

Web Audio API functions are encountering a playback issue on iOS 13.3, despite working smoothly on previous iOS versions

I have been developing an audio visualizer using the web audio API. It was functioning smoothly on PC, however, after upgrading to iOS 13.3, it no longer operates on Apple mobile devices. The root cause of this issue remains a mystery to me. The problem s ...

Personalize the color scheme for your timeline paper

I am interested in customizing this specific example of a personalized timeline: import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; import Timeline from '@material-ui/lab/Timeline'; import Timeli ...

Error encountered during Next.js project build: Entry point specified for implicit type library 'build'

While working on my nextjs project, I encountered the following error: Type error: Cannot find type definition file for 'build'. The file is in the program because: Entry point for implicit type library 'build' Can someone guide ...

We are hosting an event focused on DOM text selection outside of Input or TextArea elements

I need help finding a Javascript event that triggers when a user highlights paragraph text with their mouse on a web page. Once the text is highlighted, I want to access it using window.getSelection(). Just to clarify, I am not looking for ways to capture ...

The Angular Universal error arises due to a ReferenceError which indicates that the MouseEvent is not

I am encountering an error while trying to utilize Angular Universal for server-side rendering with the command npm run build:ssr && npm run serve:ssr. This is being done in Angular8. /home/xyz/projects/my-app/dist/server/main.js:139925 Object(tslib__WEB ...

Using AJAX to dynamically update content via HTTP requests

Why do I keep seeing "loading..." instead of the content from data.php? xmlhttp = new XMLHttpRequest(); function fetchData () { xmlhttp.onreadystatechange = function () { if(xmlhttp.readyState = 4 && xmlhttp.status == 20 ...

What is the best way to split an AJAX response into different variables and effectively retrieve each one of them?

When using AJAX to fetch data from a database and display it in a text box, most examples found online only show how to display the AJAX response in one text box. But what if we need to separate multiple PHP variables retrieved from the AJAX response and d ...

JavaScript can be used to target and remove parent <tr> elements that contain empty <td> elements with a specific class

I am facing a challenge with a large HTML table that is populated by variables set in JavaScript from my database response. When the database returns an empty string, I want to remove the entire parent row using JavaScript. Although I have attempted to us ...

Steps for displaying the contents of a file from Firebase storage on a React component

I uploaded a file to Firebase storage, and I want my app to be able to access and display the contents of that file within my class component div. Currently, when I try to access the file, it just opens in a new tab instead. class ScanResult extends Comp ...

React Native: Implementing scroll-based header animations in ScrollView

Currently, I am working on implementing an animated header with ScrollView on the screen. In this implementation, I need to set my view based on the Y position of the ScrollView during scrolling. This is how it's done: const onScroll = ({ nativeEvent: ...

What is the process for modifying information within a text document?

What I am trying to achieve is a ticker with two buttons that can increment or decrement the value by one each time they are clicked. In addition, I want this value to be synced with a number stored in a text file. For instance, if both the counter and t ...

Mastering Tooltip Placement Using CSS or JavaScript

I have been working on creating a CSS-only tooltip for a web application and so far I have managed to create some useful tooltips with different classes: tooltip-up tooltip-down tooltip-left tooltip-right The distinguishing factors between them are t ...

Tips for troubleshooting an Express application launched by nodemon through a Gulpfile in WebStorm 10

I have a unique Express application that is powered by a Gulpfile configuration. gulpfile.js 'use strict'; var gulp = require('gulp'); var sass = require('gulp-sass'); var prefix = require('gulp-autoprefixer'); va ...

Conceal a particular object from view by selecting it from the menu

I want to hide a specific element when the burger button is clicked. (See CaptureElementNoDisplay.PNG for the element to hide) When I click the burger button again to close the menu, I want the hidden item to be displayed once more. I've successfull ...

Press the Text and Alter Text with React

I'm having an issue with the onClick event using React hooks. My goal is to have the text change to a different one when the user clicks, and then revert back to the original text on another click. For example, clicking "Change First" should switch it ...

Executing function with ng-click in AngularJS only on the second click of the button

Currently, I am studying AngularJS and working on an exercise that involves using the ng-click function. Strangely, I am only able to view the result after clicking upload for the second time. I am trying to display my json content but it's not workin ...

"Capture live video using the reverse camera with the HTML5-QR Code

Recently, I started utilizing a JavaScript library called html5-qrcode (https://github.com/mebjas/html5-qrcode) for scanning QR Codes directly from my browser. The performance of this library is exceptional - it's fast and seamless! https://i.sstatic ...

The flickering problem with HTML5 DOM

I created a HTML5 game using canvas and DOM elements, but I'm facing an issue with flickering DOM elements while playing. The problem seems to be more prevalent in mobile browsers, particularly Chrome. My game includes a full screen canvas with vario ...