What is the best way to structure time and avoid mentioning the server name?

After struggling for days, I am reaching out for help as I encounter a null error and difficulty changing the time format in my code below. Despite my efforts, this issue seems unsolvable.

Error #1 ------------------------------

(node:8844) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of null
at Object.execute (C:\filepath\bot\commands\serverinfo.js:22:39)
...
(node:8844) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The main problem lies in extracting the server name for a DM. How can I modify the code to separate the DM portion from the channel portion effectively?

Error #2 ------------------------------

In an attempt to shorten the time and date format, I am facing challenges. The current output is lengthy, while I aim to achieve a simpler MM/DD/YYYY format without success using moment.js.

Desired output:

If you have the solution to assist me, I would deeply appreciate it.

CODE -------------

const { prefix } = require('../config.json');
module.exports = {  
    name: 'serverinfo',
    description: 'sends the serverinfo command',
   ...
}

Answer №1

An error has occurred: TypeError: Cannot read property 'name' of null

It seems that the issue arises because DM channels do not belong to guilds, causing dmChannel.guild to return null. While your attempt to fix it by checking the channel.type is on the right track, there is a flaw in your implementation - the code continues to execute even after identifying the channel as a DM.

Here's a basic example illustrating this behavior:

function isTheNumberOne(num) {
  if (num !== 1) console.log('The number is not one'); 
  console.log('The number is one'); 
};

isTheNumberOne(2)

To address this, you should either introduce an else statement or simply use return within the condition.

function isTheNumberOne(num) {
  if (num !== 1)
    return console.log('The number is not one');
  console.log('The number is one');
};

isTheNumberOne(2)

function isTheNumberOne(num) {
  if (num !== 1)
    return console.log('The number is not one');
  else console.log('The number is one');
};

isTheNumberOne(2)

Your previous attempt with the if (message.channel) statement before sending the embed does not prevent the error since the content has already loaded by that point. Instead, consider using:

if(message.channel.type == "dm") 
 return message.channel.send(keepinservercommandembed);

The desired format for date display is MM/DD/YYYY

You can achieve this formatting using the npm package moment, which appears to be already included in your file. Simply utilize the format function as follows:

{
 name: '**Joined On**:',
 value: moment(message.member.joinedAt).format('MM/DD/YYYY'),
 inline: true,
},

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

Utilizing a single controller in multiple locations within AngularJS

I am currently working on developing an Ionic application which includes screens with lists containing checkboxes and the option to select all items. My experience with AngularJS and Ionic is fairly limited. The "Select All" functionality works as intende ...

Failure to initiate JavaScript function with onClick event

I've been struggling with a simple issue related to calling a javascript function. After spending several hours trying to debug it on my own, I'm reaching out for help in the hopes that a fresh perspective can assist me. Here is the snippet of c ...

Hide the search results if the user leaves the input field blank

I am trying to implement Live Search JSON Data Using Ajax jQuery, and I want to be able to search through multiple JSON files. When the page initially loads with an empty input field, no results are displayed. However, if you type and then delete text in ...

How come my directive is being updated when there are changes in a different instance of the same directive?

For the purpose of enabling Angular binding to work, I developed a straightforward directive wrapper around the HTML file input. Below is the code for my directive: angular.module('myApp').directive('inputFile', InputFileDirective); f ...

Troubleshooting the error "The 'listener' argument must be a function" in Node.js HTTP applications

I'm facing an issue resolving this error in my code. It works perfectly fine on my local environment, but once it reaches the 'http.get' call, it keeps throwing the error: "listener argument must be a function." Both Nodejs versions are iden ...

Is it possible to conceal the dates from past months in the datepicker plugin?

Currently, I am utilizing a datepicker tool from jQuery UI and adjusting its CSS to suit my requirements. My goal now is to hide any dates that are not currently active, specifically those displayed from other months. I am unsure if this can be achieved ...

Revamp List Model through Ajax Integration in ASP .NET MVC5

Could someone please provide a hint on how to update the Model list in the view page after calling the Action Result with an Ajax request? Specifically, how can I refresh the current list model with the result of the Ajax call back? Here is the code for m ...

Adding routes dynamically in nodeJS using Express is a powerful way to manage your server

Is there a way to dynamically add paths to nodeJS within a running program? Instead of relying on kludged solutions like resetting the server when files change, I am looking for a more efficient method. Any suggestions? EDIT: This project is focused on pr ...

What steps are involved in bundling a Node project into a single file?

Recently, I've been using npm to create projects. When it comes to AMD, I find the native node require to be very convenient and effective for my needs. Currently, I rely on grunt-watchify to run my projects by specifying an entry file and an output f ...

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 ...

Modifying the src attribute of an object tag on click: A step-by

So I have an embedded video that I want to dynamically change when clicked on. However, my attempt at doing this using JavaScript doesn't seem to be working. <object id ="video" data="immagini/trailer.png" onclick="trailer()"></object> H ...

"Proper Installation of Angular Project Dependencies: A Step-by-Step

Whenever I clone an Angular project with older versions that are missing the node_modules folder, and then run npm install to install all necessary dependencies, I end up receiving numerous warnings and errors related to version mismatches. Here are some ...

Angular-Translate fails to function within a tag's attribute

For my project, I utilize angular-translate. One of the key definitions looks like this: { "paging":{ "first":"First", "last":"Last", "next":"Next2", "pre":"Previous" } } I implement it in the following way: <uib-pagination first-tex ...

Utilize JavaScript and jQuery to locate a particular character within a string and move it one position back in the sequence

Can you locate a particular character within a string and move it to the position before? For instance: Consider the following string: Kù Iù Mù The desired output is: ùK ùI ùM ...

What is the correct way to accurately determine the width of a block being displayed with the flex-direction:column property?

For instance: HTML console.log($('.container').width()); // 600px as we defined in css. console.log($('.list').width()); // 600px console.log($('.box').eq('-1').position().left + $('.box').outerWidth( ...

autoNumeric JS field text is cleared upon losing focus

While implementing autoNumeric js for thousand separation, I noticed that after entering a value and then focusing out to another field, the entered value gets erased. What could be causing this issue? Below is the code I used: jQuery("#job_no").autoNu ...

Remove the content located beside an input element

Seeking assistance for a straightforward query: Snippet of code: <span> <input id="elemento_20_1" name="elemento_20_1" class="elemento text" size="2" maxlength="2" value="" type="text"> / <label for="elemento_20_1">DD</label> < ...

Using anchor tags to send HTML code through email

I currently have an anchor tag on my website that looks like this: <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e0d11131b11101b3e0d11131b09161b0c1b501d1113">[email protected]</a>?subject=Wel ...

Mobile viewing causing problems with website buttons functionality

While my website functions perfectly on desktop, I am facing an issue where the buttons are not working when accessed through mobile devices. Interestingly, these buttons were operating fine in a previous version of the site. Although I have made minimal ...

AngularJS is unable to properly display (parse) data fetched using $http.get from Laravel5

Working on my SPA project, I have integrated Laravel 5 for database management and used AngularJS for the front-end. All Angular files are stored in the public folder. Instead of displaying a list of users when visiting localhost, it shows: {{ user1.name ...