Así que estoy construyendo mi bot usando discord.js (v13).
Aquí está mi código:
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]}) ; }, };lo anterior es un ejemplo de un comando de barra inclinada con una inserción. En mi inserción, estoy tratando de imprimir el nombre del gremio. Usando guild.name. No recibo ningún error en la terminal, pero cuando ejecuto el código en mi servidor de Discord, mi bot se muestra indefinido.
¿Qué estoy haciendo mal aquí y cómo lo soluciono?
Editar: Mi Config.json: -
{ "token": "Test Test", "clientId": "Client Id", "guildId": "Guild Id" }PD: soy un poco nuevo en discord.js y javascript
El problema es que la variable guild es el contenido del archivo config.json . Si este archivo contiene un objeto con las claves guildID y clientID únicamente, guild.name no estará undefined .
Creo que quieres usar el guild de donde proviene la interaction , en este caso, es 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] }); }