Utilize a bot to scan through an array for specific elements

I'm currently working on a project to create a bot that can extract information from an info.json file and display it in the form of a rich embed within a Discord Channel.

One challenge I've encountered is that my JSON file contains multiple arrays that require access through the "prefix + player name" command.

Here's an example array from the JSON file:

{
  "playername": "riky",
  "age": 20,
  "height": 172
}

So, when someone searches for a player like riky using the prefix + riky command, the bot should display all the associated information such as age, height, etc.

If anyone has any insights or solutions for this issue, your help would be greatly appreciated!

Answer №1

To access your JSON file, I utilized the fs library and implemented a for loop to scan through all the JSON arrays in search of the playername provided in your command. In case the bot locates an array matching this playername, it stores the age and height values in new variables.

Following this process, you can craft your RichEmbed and dispatch it within the designated channel.

Feel free to explore the code snippet below:

const fs = require('fs');
const Discord = require('discord.js');

const jsonFile = fs.readFile('PATH of your JSON');
const playerName = msg.content.split(' ').slice(1);

let age;
let hight;

for (var i = 0; i < jsonFile.length; i++) {
  if (jsonFile[i].playername === playerName) {
    age = jsonFile[i].age;
    hight = jsonFile[i].hight;
  }
}

if (!age || !height) return message.channel.send('Couldn\'t find this player!');

const embed = new Discord.RichEmbed()
  .setTitle(playername)
  .addField('Age', age)
  .addField('Hight', hight);

message.channel.send({ embed });

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 for transforming nested JSON into a dataframe?

{ "RecordSet": { "geometryType": "esriGeometryPoint", "spatialReference": { "wkid": "DLT0" }, "features": [{ "geometr ...

The display/block feature will only function if the div element is contained within a table

I am facing an issue with hiding/showing two div elements alternatively. The code works perfectly when the divs are placed within a table, but fails when they are not in a table due to compatibility issues with Internet Explorer. I prefer not to use a tabl ...

The promise catch method does not handle JSON parsing correctly

Utilizing Angular's Http to interact with my API has been successful for handling responses with a status of 200. The data is parsed correctly and outputted as expected within the first .then() block. However, when encountering an error with a status ...

What is the significance of property placement in a Mongo query: at the beginning versus at the

Currently, I am in the process of creating a query to retrieve data from the mango collection. Interestingly, I have written the same query in two different ways. Here is my working query: db.getCollection('routes').find({"routes.routeId": "r1q ...

What is the current state of Javascript in versions 3.4 and 3.5 of ANTL

Can someone provide information on the current status of the JavaScript target in ANTLR 3.4 or 3.5? I've searched online for answers but haven't found anything conclusive. While I know it was fixed in v3.3 after being broken in v3.2, there is no ...

Having trouble with JSONP cross-domain AJAX requests?

Despite reviewing numerous cross-domain ajax questions, I am still struggling to pinpoint the issue with my JSONP request. My goal is simple - to retrieve the contents of an external page cross domain using JSONP. However, Firefox continues to display the ...

Troubleshooting Angular 2 Fallback Route Failure

My current project is using Angular 2 Webpack Starter but I am having trouble with the fallback route. In my app.routes.ts file, I have defined the routes as follows: import { Routes } from '@angular/router'; import { HomeComponent } from &apos ...

Tips for transferring a set-returning function to a LATERAL JOIN in PostgreSQL

Attempted Query: select created_at, sum((json_array_elements(shipping_lines::json) ->> 'price')::float) as shipping_price from t1 group by 1 Error Encountered: ERROR: aggregate function calls cannot contain set-returning function calls ...

Can you provide instructions on how to utilize the fingerprintjs2 library?

I'm diving into the world of device identification with fingerprintjs2, but encountering a roadblock due to my inexperience with Libraries. The error message I keep running into is Uncaught ReferenceError: Fingerprint2 is not defined. Take a look at ...

The compiler option 'esnext.array' does not provide support for utilizing the Array.prototype.flat() method

I'm facing an issue with getting my Angular 2 app to compile while using experimental JavaScript array features like the flat() method. To enable these features, I added the esnext.array option in the tsconfig.json file, so the lib section now includ ...

Is recursion effective in this scenario? (javascript/node.js)

Currently, I am working on creating a TV using a Raspberry Pi and JavaScript to play the same playlist repeatedly. Although playing the files is not an issue, I am facing a challenge with the asynchronous nature of JavaScript. Below is the code that is cau ...

What could be preventing the jQuery from showing the JSON response?

I am having an issue with a jQuery script that is supposed to pull a quote from an API in JSON format and display it on the page. However, for some reason, I am unable to retrieve data from the API. Can you help me figure out what is wrong here? Thank yo ...

I'm looking to design a navbar similar to the one in the provided link, and I'd like it to appear when scrolling

I'm struggling to figure out how this particular navbar was created. How can I add a navlist for Videos and make the navbar visible upon scrolling? Are there any resources, articles, or hints to help me get started with this? LINK - ...

Tips for identifying a pair of numbers (with varying ranges) in which one number must be present at all times

I am currently trying to understand the regex I have implemented in Angular 2 using Typescript: /[0-5]?[0-9]?/ Surprisingly, this regular expression works flawlessly to match any valid minute from 1 to 59, even though it seems like an empty string would ...

Using JavaScript variables within an HTML tag

I am attempting to embed a JS variable into HTML tags, but for some reason the movie is not loading. Here is the code I am using: var filmLocation = "images/main.swf"; <script>document.write('<PARAM name="movie" value="' + filmLocat ...

Guide to accessing data from dot net core in React Native

Screenshot of Postman showing dot net core API Although Postman successfully fetches data from my dot net core API, I am encountering difficulties in retrieving the data in React Native using the same API. I have tried various solutions, including changin ...

Using JQuery to hide elements by setting CSS display as none instead of block

Is there a way to change the display of a content div to block when a specific tab is selected by the user, while hiding all other content divs? Here is the JQuery code I have tried so far: $(document).ready(function() { function resetTabs() { $("# ...

Arrangement of components within an entity

I have an instance (let's refer to it as myObject) that is structured like this (when you log it to the console): >Object {info1: Object, info2: Object, info3: Object, info4: Object,…} >info1: Object >info2: Object Id: 53 ...

Sending a multitude of variables using strings, evaluating them through various functions, and incorporating a variety of methods

To put it simply, my goal is to utilize an object literal that allows me to pass an unknown quantity of variables in any order to a function. While this may seem straightforward in principle, within my code, this object literal is passed to a second functi ...

Exploring the Scope of React Functional Components

My current dilemma involves a React Functional Component acting as a video player. I'm facing an issue where I need to invoke a function named onEnded when the video player triggers its ended event. This function takes in two parameters: a callback pa ...