Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
Guild is showing undefined discord.js

So I am building my bot using discord.js (v13).

Here is my code:

const { MessageEmbed } = require('discord.js')
const { SlashCommandBuilder } = require("@discordjs/builders")
const guild = require('../config.json');

module.exports = {
data: new SlashCommandBuilder()
    .setName('server')
    .setDescription('Display info about this server.'),

async execute(interaction) {

    const exampleEmbed = new MessageEmbed()
.setColor('#0099ff')
.setTitle(`Server infomation on ${guild.name}`)
.setDescription('Tells Server Info')
.addFields(
    { name: 'Server Name', value: `${guild.name}` },
    { name: '\u200B', value: '\u200B' },
    { name: 'Inline field titlve', alue: 'Some value here', inline: true },
    { name: 'Inline field title', value: 'Some value here', inline: true },
)
.setTimestamp()

    await interaction.reply({embeds: [exampleEmbed]}) ;
},
};

the above is an example of a slash command with an embed. In my embed, I am trying to print the guild name. Using guild.name. I don't get any error in the terminal but when I run the code in my discord server my bot shows undefined.

What am I doing wrong here and how do I fix it?

Edit: My Config.json:-

{
    "token": "Test Test",
    "clientId": "Client Id",
    "guildId": "Guild Id"
}

P.S. I am kinda new to discord.js and javascript

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is that the variable guild is the content of the config.json file. If this file contains an object with the keys guildID and clientID only, guild.name will be undefined.

I think you want to use the guild where the interaction is coming from, in this case, it's interaction.guild:

async execute(interaction) {
  const exampleEmbed = new MessageEmbed()
    .setColor('#0099ff')
    .setTitle(`Server infomation on ${interaction.guild.name}`)
    .setDescription('Tells Server Info')
    .addFields(
      { name: 'Server Name', value: `${interaction.guild.name}` },
      { name: '\u200B', value: '\u200B' },
      { name: 'Inline field title', value: 'Some value here', inline: true },
      { name: 'Inline field title', value: 'Some value here', inline: true },
    )
    .setTimestamp();

  await interaction.reply({ embeds: [exampleEmbed] });
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda