What is the best way to extract a message as an object from a JSON data in discord.js?

Currently in the process of developing a discord bot with an election feature for my server's moderator. All the necessary election data is being saved in an external JSON file to ensure that if the bot crashes during an election, it can seamlessly resume from where it left off upon restarting without any user input. To achieve this, I am utilizing the npm package edit-json-file to edit and read my JSON.

Encountering an issue when retrieving a message object from the JSON in order to create a reaction collector, resulting in the following error:

TypeError: electionData.get("message").createReactionCollector is not a function

The message object stored in my JSON appears as follows:

{
  "message": {
    "channelID": "678347518907777062",
    "deleted": false,
    "id": "750965454297628754",
    "type": "DEFAULT",
    "content": "@everyone",
    "authorID": "729286679822860309",
    "pinned": false,
    "tts": false,
    "system": false,
    "embeds": [
      {
        "type": "rich",
        "title": "Election",
        "description": "It's time to vote! \n\nšŸ‡¦ for <@247283454440374274> \nšŸ‡§ for <@235088799074484224> \n\n__*Warning:*__ *You* ***can't*** *remove or change your vote later.*",
        "color": 15046144,
        "timestamp": 1599114577345,
        "fields": [],
        "thumbnail": null,
        "image": null,
        "video": null,
        "author": null,
        "provider": null,
        "files": []
      }
    ],
    "attachments": [],
    "createdTimestamp": 1599114516568,
    "editedTimestamp": null,
    "webhookID": null,
    "applicationID": null,
    "activity": null,
    "flags": 0,
    "reference": null,
    "guildID": "591947002066108424",
    "cleanContent": "@everyone"
  }
}

Attempted to cache the message by its id without success. How can I properly retrieve the message as an object from the JSON?

Answer ā„–1

When you parse JSON, it simply returns a basic object with filled-in properties. Attempting to use

electionData.get("message")
as if it were a class will not work.

To resolve this, you can either create a new class instance using your JSON data, or relocate the function outside of the class.

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

Dropzone user interface package experiencing issues with displaying image previews

I recently installed the dropzone-ui package and noticed that while file items load and file icon previews show up when I drop files on the dropzone, image previews are not displaying. Could it be an issue with my implementation? Here's a snippet of ...

Retrieving information from Graph API query using C#

Currently, I am utilizing the Graph API on Azure Active Directory to gather group membership details. After executing a query, I receive a JSON string containing information about the groups that the user is part of. While I can analyze and de-serialize th ...

When the return false statement is included, the form fails to submit and instead refreshes on the current page

I recently discussed an issue regarding triggering a dialog box before confirming a submit action. Unfortunately, after implementing this, the document no longer submits when expected. Instead, it just remains on the same page with the same settings. I h ...

Performing a password-protected JavaScript Ajax request that includes special characters

Within my JavaScript page, I have implemented an Ajax call shown in the code snippet below. The PHP page resides within a corporate intranet that demands domain authentication (without basic auth). To extract the username (u) and password (p), I am using j ...

Extract the href value from an element and append it to the src attribute of an image

How can I extract the href link from the .image1 div class? <div class="image1"> <a href="/images/productA.jpg"> </a> </div> Then, how do I insert it into the image src shown below? <ul class="example"> <li class ...

Testing Vue Component with Cypress revealed that the function getActivePinia was triggered without an active Pinia instance

Below are the code snippets I'm working with: Test.vue <template> <button v-show="store.common == 'hi'" @click="func"> hello world </button> </template> <script setup> import {mainS ...

What methods and applications are available for utilizing the AbortController feature within Next.js?

My search application provides real-time suggestions as users type in the search box. I utilize 'fetch' to retrieve these suggestions from an API with each character input by the user. However, there is a challenge when users quickly complete the ...

Extracting information from an external website in the form of XML data

Trying to retrieve data from an XML feed on a remote website , I encountered issues parsing the XML content and kept receiving errors. Surprisingly, fetching JSON data from the same source at worked perfectly. The code snippet used for XML parsing is as f ...

Understanding AngularJS and how to effectively pass parameters is essential for developers looking

Can anyone help me figure out how to properly pass the html element through my function while using AngularJS? It seems like this method works without AngularJS, but I'm having trouble with the "this" keyword getting confused. Does anyone know how I c ...

What is the significance of the appearance of the letters A and J in the console for Objects?

After running console.log() in JavaScript code, you may notice some random letters like A and j before or after the Object description in the Google Chrome browser console. What is the significance of these letters? ...

The <servicename> is inaccessible within an error function in Angular2

I encountered an unusual issue in angular2. While the code below is functioning correctly: loginResult.subscribe( (data) => this.response = data, (err) => this._ajaxService.handleError(err, 'A string to summarize th ...

React state is not refreshing as expected

I am having trouble updating the state with new employee data. The push function is not inserting the new employee data into the state. In the addPar function, I set up a console log and it shows that the data is there, but it fails to push it to the sta ...

Change the image size as you scroll through the window

While my opacity style is triggered when the page is scrolled down, I am facing an issue with my transform scale not working as expected. Any suggestions on how to troubleshoot this or any missing pieces in my code? Codepen: https://codepen.io/anon/pen/wy ...

Searching for a specific element within a JSON object by iterating through it with JavaScript

Here is the JSON file I am working with: { "src": "Dvc", "rte": { "src": "dvc" }, "msgTyp": "dvc.act", "srcTyp": "dvc.act", "cntxt": "", "obj": { "clbcks": [ { "type": "", "title": "", "fields": [ { "src": "label.PNG", ...

Utilize Angular2 data binding to assign dynamic IDs

Here is the JavaScript code fragment: this.items = [ {name: 'Amsterdam1', id: '1'}, {name: 'Amsterdam2', id: '2'}, {name: 'Amsterdam3', id: '3'} ]; T ...

Every time I begin setting up a new discord bot, this error inevitably pops up

I'm encountering an error when launching my discord bot, which I initially developed a few years ago. Despite trying to troubleshoot by uninstalling and reinstalling node_modules, I am still unable to pinpoint the cause of this issue. Can anyone help ...

"The Ajax POST request to MyUrl returned a 404 error, indicating that the resource

While working on my Grails code, I encountered an error when the Ajax function received a response from the controller action. The parameters are being passed successfully by the Ajax function, and the controller function is executed. However, upon return ...

Troubleshooting issue with running npm start in GIT Bash

Encountered an error message while running the npm start command: [email protected] prestart C:\Project\myapp npm run build [email protected] build C:\Project\myapp tsc -p src/ [email protected] start C:&bsol ...

Guide on transforming 3D obj files into particles using three.js

I've been experimenting with particles in three.js, but I've encountered an issue when trying to convert an obj file (3D model) into particles. Here are the code snippets I've been working with, but so far all my attempts have failed. Does ...

Is there a way to update my profile picture without having to constantly refresh the page after uploading it?

Here is the code for my profile page. I am considering using a callback along with another useEffect function, but I'm unsure. Please help me in finding a solution. For now, let's ignore all the code related to deleting, saving, and handling ingr ...