Retrieve the server administrators by identifying the servers where the bot is currently active

I'm attempting to create a Discord bot that can send all the server owner's IDs and usernames in a single embed. Here is the code I currently have:

else if (command === '!test'){
        const owners = client.guilds.cache.forEach(guild => console.log('<@' + guild.ownerID + '>'));
        const owners1 = JSON.stringify(owners)

        const embed = new Discord.MessageEmbed()
                .setColor('#fc2eff')
                .setTitle('Owners who added bartt')
                .setDescription(owners1)
                .setTimestamp();
        
            msg.channel.send(embed);
}

The current code logs the IDs to the console, which is useful, but I would prefer to display them in an embed rather than just the console.

My preference is to log the Discord names instead of the IDs, as Discord does not always show users by their ID alone.

Answer №1

To get a list of concatenated IDs, utilize Iterable#map() and then join them using Array#join(). The resulting string can be used in the embed.

const creators = client.guilds.cache.map(guild => `<@${guild.ownerID}>`);

const embed = new Discord.MessageEmbed()
    .setColor('#fc2eff')
    .setTitle('Creators of awesome content')
    .setDescription(creators.join(',\n'))
    .setTimestamp();
        
msg.channel.send(embed);

Answer №2

To fetch the owner's username, you can follow this code snippet:

const { username } = await message.client.users.fetch(message.guild.ownerID);

// You can also store the username in a variable for later use
const serverOwner = username;

Remember to make sure your execute method is marked as async.

An alternative approach is using the guild's cache, although I faced issues when the owner went offline and was no longer cached leading to errors.

The notion of multiple owners is unclear since typically there is only one who initiated the server creation process.

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 quickest method for setting up types for node packages?

Whenever I need to use typed packages in my Node.js projects, there are two steps I have to take: Firstly, install the original package. For example: npm install express -S Secondly, install its type definition package. npm install @types/express -D I f ...

React not functioning properly when packaged with Webpack

I tried implementing React in the simplest way possible, but I am facing some issues. After bundling React and opening the index.html page, it shows up completely blank with no console errors to indicate any mistakes. Below is my webpack.config.js: const ...

JavaScript CheckBox Color Change Not Functioning

Hello, I am currently experimenting with the checkAll function. When I click on the checkAll checkbox, it should select all rows and change their background color accordingly. Below is the JavaScript code I am using: function checkAll(objRef) { v ...

Styling Images with Gradient using CSS

Looking to create a unique effect on an image by adding a radial gradient that seamlessly fades into the background color. Despite my efforts, I haven't been able to achieve this using filters and my current code is a bit messy. Here's what I ha ...

Adding an image to a select option in HTML using PHP - A step-by-step guide

How can I include an image within a select option using PHP in HTML? The image needs to be retrieved from a database first. echo '<option value='.$ifet['productimage'].'><img src='.$ifet['productimage'].&a ...

Optimizing the management of data retrieved from the backend in React

My web application follows this flow: When on the Investments screen, an API request is made to a server, returning an array of investment objects that are then displayed as a list. Clicking on an item redirects the user to the Investment Details screen, ...

What is the best way to incorporate a JSON check into this stored procedure?

I am facing a challenge where I need to update/insert around 1000 records, and I'm considering using a stored procedure. One suggestion was to utilize MERGE with a table-valued parameter for this task. However, one of the columns contains a JSON strin ...

Challenging questions about Ember.js: managing save and delete operations for hasMany relationships

Currently, I am working on my first Ember project which is a quiz generator. I have successfully implemented the functionality to create, edit, and delete quizzes, and save them to local storage. However, I am facing challenges in saving/deleting quiz ques ...

Display toastR when the form is submitted by clicking on it

As a practice, I am developing a simple PHP shop and I want to enhance its functionality by showing a ToastR notification when the submit button is clicked with the message "Product successfully added to cart". However, being new to PHP, I am facing an iss ...

Transforming log information into JSON documents for storage in MongoDB

Looking for a solution to store log files (Log4j) from a remote machine into mongodb? Consider mongodb's schema-less data storage ideal for storing log data. Wondering how to convert log data into json documents for mongodb? There might be a better wa ...

How can I access the value of a textbox located in the footer row of a gridview control using JavaScript?

Below is the code snippet for a gridview in an aspx page, where the gridview is placed inside a form tag. <Grdview:GridViewExtended ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="Grid" onpageindexchanging="GridView1_Pa ...

Adjust the aesthetic based on whether the field is populated or empty

I have a simple text field on my website that triggers a search when the user inputs a value. I am wondering if it is possible to style the text field differently depending on whether it is empty or has content. Specifically, I want to change the border c ...

Javascript encounters difficulty in converting timestamp to a date format

I am utilizing this particular function export const getDate = (stamp: string) => { console.log(stamp) //1581638400000 let date : any = Date.parse(stamp) console.log(date) //NaN let month = date.getMonth() let day = date.getDay() ...

Filtering collections by value in a field using Meteor.js

Currently, I am working on a project in meteor.js where I need to retrieve all collections with a specific value in one of their fields: Posts.insert({ tags: ['test', 'test1', 'test2'], categories: ['test', &a ...

When a click event occurs, it returns either the element itself or one of its child elements,

Check out this HTML code snippet: <div class="list-group"> <a href="javascript:;" @click="showDetails(notification, $event)" class="list-group-item" v-for="notification in notifications" :key=& ...

Checkbox with an indeterminate state in Angular

I'm currently working on updating an AngularJS (1.5) setup where a parent checkbox becomes indeterminate if one of its children is selected, and all the children are selected if the parent is selected. My main challenge lies in converting the old ES5 ...

Show temporary information on the user's side in a table

To better explain the issue, please refer to the accompanying image. I am working with a Master/Detail form, specifically a Bill of Materials, which involves storing data in two separate database tables. The top portion of the form (Product, Quantity) is ...

Is there a way to hide the <v-otp-input> field in a Vue.js application?

For more information, please visit https://www.npmjs.com/package/@bachdgvn/vue-otp-input <script> export default { name: 'App', methods: { handleOnComplete(value) { console.log('OTP completed: ', value); } ...

Troubleshooting issue with applying hover effect to child divs

How come when I hover over one of the child items in my parentDiv, the background of the last childDiv changes no matter which child I place my mouse on? for (let i = 0; i < Number(height); i++) { for (let j = 0; j < Number(width); j++ ...

Troubleshooting disappearing values in POST request using ajax and jQuery serialize() method

When I make a post request, I am only able to retrieve two out of four values. I am expecting to get the id, step, name, and email from the form but the only ones I receive are from the hidden inputs. It seems like the jQuery serialize() function might be ...