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);
}
[guildStats]
is an array, and arrays don't have.serverinfo
propertyguildStats
in[]
makes it an array, and arrays don't have a property calledserverinfo
- ... what you want isguildStats.serverinfo.prefix
guildStats.serverinfo.prefix
and now it says " Cannot read property 'prefix' of undefined"console.log(guildStats)