-4
module.exports = async (msg,arg)=>{


const guildid = msg.guild.id
const guild =msg.guild

const display = msg.guild.channels.cache.find(ch => ch.name=='Total Members')

if(!display){
    try {
        const channelName='Total Members'
         await msg.guild.channels.create(channelName, {
            type: "voice", //This create a text channel, you can make a voice one too, by changing "text" to "voice"
            permissionOverwrites: [
               {
                 id: msg.guild.roles.everyone, //To make it be seen by a certain role, user an ID instead
                 allow: ['VIEW_CHANNEL'], //Allow permissions
                 deny: [ 'SEND_MESSAGES','CONNECT'] //Deny permissions
               }
            ],
          })
          msg.channel.send('Successfully created the Channel ')
    }

 catch (error){console.log(error) 
    msg.channel.send('Couldnt create one ')}

}
const display1 = await msg.guild.channels.cache.find(ch => ch.name=='Total Members')
const display1id = await msg.guild.channels.cache.get(display1.id)


setInterval((guild)=>{

const count = msg.guild.memberCount
const channel = msg.guild.channels.cache.get(display1id)
channel.setName(`Total Members: ${count.toLocaleString()}`);
console.log('Updating Member Count');

},5000)



}

The error:

const display1id = await msg.guild.channels.cache.get(display1.id)

TypeError: Cannot read property 'id' of undefined

Can anyone tell me how to solve this error, basically this code helps to see the current member of the guild and it will autoupdate it. It will create a voice channel if it didn't found any one for showing the member.

Skulaurun Mrusal
  • 2,792
  • 1
  • 15
  • 29
Aryan Gagare
  • 11
  • 1
  • 3
  • Well, if the expression `display1.id` fails with `cannot read property id of undefined` then that means `display1` is undefined, i.e. nothing matched the filter in the previous line. – CherryDT Aug 08 '21 at 07:21
  • Does this answer your question? [Detecting an undefined object property](https://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) – Eldar B. Aug 08 '21 at 08:50

1 Answers1

0

The reason it couldn't find the channel is because the channel type for voice is GUILD_VOICE (not just voice) in discord.js v13. That is why it used the default channel type GUILD_TEXT. And text channels can't have capital letters and spaces in their names. It converted the channel name 'Total Members' to 'total-members' and when you tried searching for the channel with .find() it wasn't found, because the channel name was different.

The .find() method returned undefined and you later tried reading a property .id of the returned variable. Thus reading a property .id of undefined.

Also don't use an equal sign when searching for the channel. Use .startsWith() instead, because later you are adding the member count to the channel name.

You can take a look at this:

// ... The variable msg defined somewhere (e.g. as messageCreate callback parameter) ...

const channelName = "Total Members";
let display = msg.guild.channels.cache.find(ch => ch.name.startsWith(channelName));

if(!display) {
    try {
        display = await msg.guild.channels.create(channelName, {
            type: "GUILD_VOICE",
            permissionOverwrites: [
            {
                id: msg.guild.roles.everyone,
                allow: ["VIEW_CHANNEL"],
                deny: ["SEND_MESSAGES", "CONNECT"]
            }
            ],
        });
        msg.channel.send(`Successfully created a channel '${channelName}'.`);
    }
    catch (error) {
        console.log(error);
        msg.channel.send(`Failed to create a channel '${channelName}'.`);
        return;
    }
}

setInterval(() => {
    const count = msg.guild.memberCount;
    display.setName(`${channelName}: ${count.toLocaleString()}`);
    console.log("Updating Member Count ...");
}, 5000);
Skulaurun Mrusal
  • 2,792
  • 1
  • 15
  • 29