简体   繁体   English

公会显示未定义的 discord.js

[英]Guild is showing undefined discord.js

So I am building my bot using discord.js (v13).所以我正在使用 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.使用 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.我在终端中没有收到任何错误,但是当我在 Discord 服务器中运行代码时,我的机器人显示未定义。

What am I doing wrong here and how do I fix it?我在这里做错了什么,我该如何解决?

PS I am kinda new to discord.js and javascript PS 我对 discord.js 和 javascript 有点陌生

The problem is that the variable guild is the content of the config.json file.问题在于变量guildconfig.json文件的内容。 If this file contains an object with the keys guildID and clientID only, guild.name will be undefined .如果这个文件包含一个只有guildIDclientID键的对象, guild.name将是undefined

I think you want to use the guild where the interaction is coming from, in this case, it's interaction.guild :我想你想使用interaction来自的guild ,在这种情况下,它是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] });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM