【问题标题】:"Cannot read property 'prefix' of undefined" when making a Discord bot制作 Discord 机器人时“无法读取未定义的属性‘前缀’”
【发布时间】:2021-01-24 01:18:23
【问题描述】:

我正在尝试制作一个 Discord 机器人。我一直在研究它一个星期,突然我收到这个错误:'无法读取未定义的属性'前缀''。在过去的几个小时里,这一直困扰着我。这是代码(如果它没用,但可能就是原因)我感谢我提前获得的所有帮助。 (代码错误在第 82-85 行)如果您需要任何其他信息,请在 discord 上私信我:StodiusDev#0001

/* eslint-disable no-multiple-empty-lines */
/* eslint-disable no-unused-vars */
const Discord = require('discord.js');
const fs = require('fs');
const jsonfile = require('jsonfile');

const client = new Discord.Client({
  partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
});
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const moderationFiles = fs.readdirSync('./commands/moderation').filter(file => file.endsWith('.js'));
const funFiles = fs.readdirSync('./commands/fun').filter(file => file.endsWith('.js'));
const miscFiles = fs.readdirSync('./commands/miscellanious').filter(file => file.endsWith('.js'));
const {
  token,
  bot_age,
  words_array,
  bot_info,
} = require('./config.json');

client.once('ready', () => {
  console.log('Bot is ready.');
  client.user.setActivity('!help commands');
});

client.login(token);

client.on('guildCreate', guild => {
  stats[guild.id] = {};
  const guildStats = stats[guild.id];
  guildStats['serverinfo'] = {
    serverid: guild.id,
    prefix: '!',
  };
  jsonfile.writeFileSync('stats.json', stats);
  guildStats['moderationsettings'] = {
    adminrole: 0,
  };
  jsonfile.writeFileSync('stats.json', stats);
  guildStats['modlogs'] = {
    hasmodlogs: false,
    modlogsID: 0,
  };
  jsonfile.writeFileSync('stats.json', stats);

  const embed = new Discord.MessageEmbed()
    .setTitle('Thanks for Adding Me!')
    .setDescription('Thank you for adding me! This bot is a moderation and fun bot, so you don\'t need any more! For more help, say `!help commands`. To add a admin role, say `!setadmin <roleID>`.')
    .setColor('BLUE');

  const msgEmbed1 = guild.owner.send(embed);

  guild.systemChannel.send('Thanks for inviting me, here are a list of all my commands! :wink:', {
    embed: {
      title: ':x: Prefix',
      color: 0x2471a3,
      description: 'The prefix for all my commands is \'!\'. To view more of my commands say `!help commands`',
      fields: [{
        name: ':tada: Fun',
        value: 'hi, dankrate, gayrate, 8ball, meme, pun, roll, coinflip, doge, kappa, lenny, lol, megusta, pepe, sanic, spiderman, spooderman, troll, wat, dolan, notsure, alone, pupper, kitty',
      },
      {
        name: ':boom: Moderation',
        value: 'setadmin, ban, kick, modlogss create',
      },
      {
        name: ':tools: Miscellaneous',
        value: 'help commands, ping, invite',
      },
      ],
      footer: {
        text: 'Bot created by StodiusDev#0001.',
      },
    },
  });
});

client.on('message', async message => {
  stats[message.guild.id] = {};
  const guildStats = stats[message.guild.id];
  if (!message.content.startsWith([guildStats].serverinfo.prefix) || message.author.bot) return;

  const args = message.content.slice([guildStats].serverinfo.prefix.length).trim().split(/ +/);
  const commandName = args.shift().toLowerCase();

  if (!client.commands.has(commandName)) return;
  const command = client.commands.get(commandName);
  if (!args.length & command.args === true) {
    const embed = new Discord.MessageEmbed()
      .setColor('#FF0000')
      .setTitle('ERROR')
      .setDescription('Not enough arguments. Please say `!help commands` for a full list of commands.')
      .setFooter('Bot Error Log');
    return message.channel.send(embed);
  }
});

for (const file of funFiles) {
  const command = require(`./commands/fun/${file}`);
  client.commands.set(command.name, command);
}

for (const file of commandFiles) {
  const command = require(`./commands/${file}`);
  client.commands.set(command.name, command);
}

for (const file of moderationFiles) {
  const command = require(`./commands/moderation/${file}`);
  client.commands.set(command.name, command);
}

for (const file of miscFiles) {
  const command = require(`./commands/miscellanious/${file}`);
  client.commands.set(command.name, command);
}


【问题讨论】:

  • [guildStats]是一个数组,数组没有.serverinfo属性
  • 我想指出 .serverinfo 来自另一个文件(stats.json),如果你看一下我在机器人加入公会时写的顶部,它会写进去stats.json 文件夹正确,但是当我尝试获取它时,它会这么说。
  • 不管...将guildStats 放入[] 使其成为一个数组,而数组没有名为serverinfo 的属性 - ... 你想要的是guildStats.serverinfo.prefix跨度>
  • 哦,好吧,我已将其更改为guildStats.serverinfo.prefix,现在它显示“无法读取未定义的属性'前缀'”
  • 所以,同样的错误 - console.log(guildStats) 的输出是什么

标签: javascript discord discord.js


【解决方案1】:

client.login(token); 放在底部,prefix 需要在您的所有代码中为小写,如此处title: ':x: Prefix', 看到 p 不是小写

【讨论】:

  • 不,这不是问题,你说的效果很好,问题统计在第 80 行
  • @Stodius 和你不和谐的名字
  • StodiusDev#0001
  • 刚刚添加了你可以邀请到服务器并向你展示一些东西
猜你喜欢
  • 2020-07-20
  • 1970-01-01
  • 2021-04-25
  • 2021-07-04
  • 2019-04-26
  • 2019-01-27
  • 2021-09-25
  • 2020-08-17
  • 2021-03-01
相关资源
最近更新 更多