0

I'm trying to make a Discord bot. I've been working on it for ~ a week, when suddenly I get this error: 'Cannot read property 'prefix' of undefined'. This has been messing me up for the last couple of hours. Here is the code (some if it is useless but that may be why) I appreciate all the help I get in advance. (The code error is in line 82-85) If you need any other info please DM me on 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);
}


8
  • [guildStats] is an array, and arrays don't have .serverinfo property Oct 8, 2020 at 23:32
  • I'd like to point out that the .serverinfo is from another file (stats.json), if you look at the top I wrote it in when the bot joins the guild, it writes it in the stats.json folder correctly but when I try to get it, it says that.
    – Stodius
    Oct 8, 2020 at 23:34
  • 1
    regardless ... putting guildStats in [] makes it an array, and arrays don't have a property called serverinfo - ... what you want is guildStats.serverinfo.prefix Oct 8, 2020 at 23:38
  • Oh okay, I've changed it to guildStats.serverinfo.prefix and now it says " Cannot read property 'prefix' of undefined"
    – Stodius
    Oct 8, 2020 at 23:45
  • so, it's the same error - what is output of console.log(guildStats) Oct 8, 2020 at 23:49

1 Answer 1

0

put client.login(token); at bottom and prefix need to be lower case in all ur code like here title: ':x: Prefix', see the p is not lower case

4
  • No that's not the problem, what you said has worked fine, the problem stats in line 80
    – Stodius
    Oct 9, 2020 at 0:05
  • @Stodius and what ur discord name
    – Kofidave
    Oct 9, 2020 at 0:13
  • StodiusDev#0001
    – Stodius
    Oct 9, 2020 at 0:17
  • just added you can I invite to a server and show u something
    – Kofidave
    Oct 9, 2020 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.