【问题标题】:Guild is not defined (Discord.js)公会未定义(Discord.js)
【发布时间】:2020-12-24 15:49:45
【问题描述】:

我在 Js 中制作了一个不和谐的机器人,但遇到了未定义“公会”的问题。我之前制作过类似的 discord.js 机器人,但我从未遇到过这样的错误:

.setTitle(`Server infomation on ${guild.name}`)
                                  ^
ReferenceError: guild is not defined
    at Object.<anonymous> (/home/.Ulur/Downloads/Discordia/index.js:15:36)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)```

还有我的机器人代码:

const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs
 .readdirSync('./commands')
 .filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
 const command = require(`./commands/${file}`);
 client.commands.set(command.name, command);
}
// END OF CONSTANTS

const ServerInfo = new Discord.MessageEmbed()
 .setColor('RANDOM')
 .setTitle(`Server infomation on ${guild.name}`)
 .setDescription(`Command executed by ${message.author.tag}`)
 .addFields(
  { name: 'Members', value: `${guild.memberCount}` },
  { name: 'Region', value: `${guild.region}` },
  { name: 'Owner', value: `${guild.owner.tag}`, inline: true },
  { name: 'Created', value: `${guild.createdAt}`, inline: true }
 );

client.on('ready', () => {
 console.log(`Logged in as ${client.user.tag}`);
});

client.on('message', (message) => {
 if (!message.content.startsWith(prefix) || message.author.bot) return;

 const args = message.content
  .slice(prefix.length)
  .trim()
  .split(/ +/);
 const command = args.shift().toLowerCase();

 if (!client.commands.has(command)) return;

 try {
  client.commands.get(command).execute(message, args);
  console.log(`Executing Command`, `${prefix}${command}`);
 } catch (error) {
  console.error(error);
  message.reply('there was an error trying to execute that command!');
 }
});

client.login(token);

【问题讨论】:

  • 我的意思是.... guild 没有定义。 guild 应该是什么意思?

标签: node.js discord discord.js


【解决方案1】:

您必须使用以下方式定义公会:var guild = client.guilds.cache.get('YOUR_GUILD_ID')

对于留言,你想做:var guild = message.guild

查看有关公会的文档以确定您要显示的公会的哪些属性:https://discord.js.org/#/docs/main/stable/class/Guild

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    您可能希望使用以下方法获取公会对象:

    const guild = client.guilds.cache.get("YOUR_GUILD_ID");
    

    从消息中获取公会并使用您的代码(仅作为示例,因为这会发送嵌入每条消息):

    client.on('message', message => {
    
      let guild = message.guild;
        const ServerInfo = new Discord.MessageEmbed()
        .setColor('RANDOM')
        .setTitle(`Server infomation on ${guild.name}`)
        .setDescription(`Command executed by ${message.author.tag}`)
        .addFields(
            { name: 'Members', value: `${guild.memberCount}` },
            { name: 'Region', value: `${guild.region}` },
            { name: 'Owner', value: `${guild.owner.tag}`, inline: true },
            { name: 'Created', value: `${guild.createdAt}`, inline: true },
        )
    
    });
    

    【讨论】:

      猜你喜欢
      • 2021-04-06
      • 2018-06-24
      • 2021-12-04
      • 2021-05-30
      • 2021-10-06
      • 2021-06-25
      • 2017-07-03
      • 2020-07-05
      • 2021-02-05
      相关资源
      最近更新 更多