I'm currently working on a discord.js bot to start getting some practice with node.js I have currently run into a problem when trying to get all the members of a guild with a certain role. Here is the relevant code:
const { SlashCommandBuilder } = require('@discordjs/builders');
const fetch = require('node-fetch');
module.exports = {
data: new SlashCommandBuilder()
.setName("server")
.setDescription("Get server information"),
async execute(interaction) {
const serverInfo = await getServerInfo(interaction);
await interaction.reply({ embeds: [serverInfo] });
}
}
async function getServerInfo(interaction) {
console.log("function called")
let fetchedMemebers = await interaction.guild.members.fetch()
let roles = await interaction.guild.roles.fetch()
const totalMembers = fetchedMemebers.memberCount;
const adminRole = roles.find(role => role.name === "Admin") //the role to check
const totalAdmin = adminRole.members.length;
const serverInfo = { // create the embed
color: 0x0099ff,
title: `${interaction.guild.name}\'s Server Information`,
description: `Members: ${totalMembers}\nAdmins (Members with the "admin" role): ${totalAdmin}\nDate created: ${interaction.guild.createdAt.toDateString()}`,
author: {
name: `${interaction.user.username}`,
icon_url: `${interaction.user.avatarURL()}`,
},
timestamp: new Date()
}
return serverInfo;
};
I have the following error:
DiscordAPIError: Invalid Form Body
data.embeds[0].description: This field is required
at RequestHandler.execute (/workspace/node.js-verve-bot/node_modules/discord.js/src/rest/RequestHandler.js:349:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (/workspace/node.js-verve-bot/node_modules/discord.js/src/rest/RequestHandler.js:50:14)
at async CommandInteraction.reply (/workspace/node.js-verve-bot/node_modules/discord.js/src/structures/interfaces/InteractionResponses.js:98:5)
at async Object.execute (/workspace/node.js-verve-bot/commands/server.js:11:9)
at async Client.<anonymous> (/workspace/node.js-verve-bot/index.js:46:3) {
method: 'post',
path: '/interactions/918268169842483211/aW50ZXJhY3Rpb246OTE4MjY4MTY5ODQyNDgzMjExOmVLZnZiSnJVVEdSMEdkVnJyTTR6ZFZOU3h1WkF0VnVFUllPQmZPb05RZmdZbmw3WjJpWjhRN3F4enI2SGhjek4xNDdJdjVrbGxRMXVwSzd3c0VjaldLS2phVVZpd25mUlJ0emJ1S3pRbTdDVjlTQzIxR21USUhvczVOelFLV0kx/callback',
code: 50035,
httpStatus: 400,
requestData: { json: { type: 4, data: [Object] }, files: [] }
}
Yes, I know this isn't my question, and I don't believe this is my problem either. I know my embed is proper. I know something is wrong with how I'm getting the current guild's members, I have verified this by trying to console.log(totalMembers)
, but you get nothing. Not even an output. I'm not sure what I'm doing wrong here. Any ideas? Edit: changed my code based on the most recent answer, though the same error occurs. Also if you console.log(adminRole.members.length)
there is no output again.