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?
PS I am kinda new to discord.js and javascript
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 titlve', alue: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.setTimestamp();
await interaction.reply({ embeds: [exampleEmbed] });
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.