Attempting to create a leaderboard of the top Discord servers based on member count using discord.js v13

In my quest to create a list of the top servers based on member count, I have come up with this code snippet:

const embed = new Discord.MessageEmbed()
.setAuthor({ name: `Top Server's`, iconURL: client.user.displayAvatarURL()})
.setColor('#545db8')
for (i = 0; i < 3; i++) {
let guild = client.guilds.cache.sort((a, b) => b.memberCount - a.memberCount).array()[i];
embed.setDescription(`**${i + 1}. ${guild.name}** \n \n Member count: \`${guild.memberCount}\` \n ID: \`${guild.id}\`\n Server Age: <t:${parseInt(guild.createdAt / 1000)}:R>`)
embed.setThumbnail(guild.iconURL())
msg.channel.send({ embeds: [embed] })
}

However, I encountered an error:

TypeError: client.guilds.cache.sort(...).array is not a function

I had successfully used this code in v12, but after switching to v13, errors started appearing. Despite searching through the discord.js documentation, I couldn't find a solution.

Answer №1

const topGuild = [...client.guilds.cache.sort((a, b) => b.memberCount - a.memberCount).values()][i];

In the past, these functions were used to access cached arrays of Collection values and keys that other Collection methods depended on. However, those methods have been updated to no longer rely on cache, resulting in the removal of these arrays and functions.

We now recommend constructing an array by spreading the iterators returned by the base Map class methods instead.

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

Leverage the power of an Express server to manage a Node.js application

I have a node.js application that communicates with a REST API hosted on a separate server. To control this application, I want to create a web interface using express.js which includes HTML, CSS, and Javascript. How can I enable the browser to interact w ...

Clone all documents from a NodeJS mongoose collection and transfer them to a different server's database

I need assistance with migrating my database to a new server. My collection consists of approximately 410,000 documents, and I am looking to transfer them in batches of 100 to my new server that runs on mongodb. Below is the code I have written for this ...

ways to display a chosen option in a dropdown menu

Below is the code snippet I am currently using, which involves JQuery and Bootstrap 4: $('.dropdown-menu li').on('click', function() { var selectedValue = $(this).text(); $('.dropdown-select').text(selectedValue); }); & ...

State properties in Vuex remain unchangeable despite using mutator methods

I've encountered an issue with the code in my vuex module called user.js: import Vue from "vue"; import Vuex from 'vuex'; Vue.use(Vuex); const state = { user: { firstName: null, lastName: null, ...

What is the best way to convert API data into a currency format?

Hello, I need assistance with formatting data retrieved from an API into a currency format. The code below successfully retrieves the data but lacks formatting. For instance, if the data displays as 100000000, I would like it to be formatted as IDR100.000. ...

Tips for identifying `window is not defined` errors during the build process of your NextJS applications

Currently, I am in the process of transitioning an enterprise application (unable to share code) from Create React App to NextJS using TypeScript. Although I have successfully replaced React Router with Next Routes/Pages, I keep running into the same erro ...

Finding the center of a Three.js Scene

I'm trying to find the center of a Scene that I've loaded using ColladaLoader in Three.js. Once I have the center, I want to set it as the target for the Camera. ...

What is the most effective way to transmit a sizeable JSON file to a server?

In my web.config file, I set the maxrequestlength to 10MB. However, there is a feature in my system that allows clients to import a .csv file that can potentially be larger than 10MB. As a result, I am looking for an efficient way to send a large json file ...

Confirming the date's accuracy

Having an issue with Yup validation in a Formik form when it comes to the date field. I want to ensure that users cannot select future dates. The problem is, the validation only triggers when clicking the save button, but I need an immediate error message ...

The globalProperties property of app.config is not defined and cannot be read

When attempting to register a global filter in Vue3, an error is being raised: main.js?56d7:13 Uncaught TypeError: Cannot read property 'globalProperties' of undefined Despite referencing the solution provided in Use filter in Vue3 but can't ...

Is it possible to manually input values when printing a PDF instead of pulling them from the main HTML?

I am facing a challenge in adding a "Print PDF" option to my website because it is built using Ext.js, and therefore the code is not in HTML. Despite searching for ways to print a PDF from the site, all solutions I found involve using HTML. Is there any li ...

Failed attempt to send JSON from PHP to Javascript using AJAX

I've successfully utilized Ajax calls in the past to retrieve JSON objects, however, I'm encountering issues with it now. Here's my current ajax call: function sendContactForm() { var nameInput = $('#nameInput').val(); var emailI ...

You are unable to bind 'ref' while utilizing 'v-for'

Creating polygons inside <svg>...</svg> using v-for: <polygon v-for="id in polygonArr" :key="id" :ref="id" points="15,0 18.541,11.459 30,11.459 20.729,18.541 24.271,30 15,22.918 5.729,30 9.271,18.541 0,11.459 11.459,11.459" /> polygonAr ...

Troubleshooting the "@material-ui/core" issue in React: Step-by-step guide

Issue with React: Unable to locate the '@material-ui/core' module in 'C:\Users\user\Downloads\inTech'22-Web Development (EDUGEN)\edu-gen\src\components' Resolution Command: To resolve, run npm in ...

Webpack: transform code in web workers within the public directory

Currently, I am utilizing Next.js for my project which incorporates webpack 5 to compile TypeScript code. Within my public folder, there are several web worker scripts located at path "/workers/**/*.worker.js". I am curious if it is possible to write the ...

Showing XML content with JavaScript

Trying to parse a simple XML list using JavaScript, but having trouble formatting it the way I need. <TOURS> <MONTH> <TOUR> <NUMBER>1</NUMBER> <VENUE>City Name - Venue Name</VENUE> < ...

The server returned an undefined status code

Seeking guidance in the right direction would be greatly appreciated. I am currently using Node/Express and consider myself a beginner in this area. Attempting to reuse some code from a previous project that was successful, but have encountered roadblocks. ...

What might be causing my list item to execute functions that it shouldn't be?

Greetings! I've recently developed a list component in React Native specifically for my settings page. My goal is for each component on the settings page to have a function, although in some cases it may not be necessary. To achieve this, I have set ...

What is the best way to showcase a component using FlatList?

Discovering the power of React Native combined with TypeScript and Redux Toolkit Hello! I'm currently facing an issue with rendering a list of messages using FlatList. Everything renders perfectly fine with ScrollView, but now I need to implement inf ...

Choose the AuthGuard category in real-time

My application intends to employ two distinct authentication strategies - one for users accessing via a browser and another for the public API. A specific header will be set for browser users, allowing my app to determine the appropriate auth strategy base ...