Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

How do I fix my member count in v12 of discord.js

I am trying to get the member count but I keep getting an error and I don't know what to do now.

Here is my code:

bot.on('ready',() =>{ 
    let myGuild = bot.guilds.fetch('759858083718496266'); // Discord server ID
    let memberCount = myGuild.memberCount;
    let memberCountChannel = channel.messages.cache.get('123456789012345678');; // kanalens ID
    memberCountChannel.setName('??Members ? ' + memberCount)
    .catch(error => console.log(error));

    console.log(`${bot.user.username} er klar.`) 
//.then(result => console.log(result))
})

bot.on('guildMemberAdd', member => {
    let myGuild = bot.guilds.fetch('759858083718496266');
    let memberCount = myGuild.memberCount;
    let memberCountChannel = myGuild.channels.fetch('792504113673142333');
    memberCountChannel.setName('??Members ? ' + memberCount)
    .catch(error => console.log(error));
});

bot.on('guildMemberRemove', member => {
    let myGuild = bot.guilds.fetch('759858083718496266');
    let memberCount = myGuild.memberCount;
    let memberCountChannel = myGuild.channels.fetch('792504113673142333');
    memberCountChannel.setName('??Members ' + memberCount)
    .catch(error => console.log(error));
});

I have tried with get instance of fetch and I have tried cache.get but received the same error:

let memberCountChannel = myGuild.channels.fetch('792504113673142333'); // kanalens ID
                                              ^
TypeError: Cannot read property 'fetch' of undefined
        at Client.<anonymous> (C:UserslauriDesktopQuebecCityindex.js:140:47)
        at Client.emit (node:events:376:20)
        at WebSocketManager.triggerClientReady (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:431:17)
        at WebSocketManager.checkShardsReady (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:415:10)
        at WebSocketShard.<anonymous> (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:197:14)
        at WebSocketShard.emit (node:events:376:20)
        at WebSocketShard.checkReady (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:475:12)
        at WebSocketShard.onPacket (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:447:16)
        at WebSocketShard.onMessage (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:301:10)
        at WebSocket.onMessage (C:UserslauriDesktopQuebecCity
ode_moduleswslibevent-target.js:132:16)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

.fetch() does not return a value, it returns a Promise. You are trying to treat myGuild as if it is a Guild object, when in reality it is a Promise that is "promising" you a Guild object. So how do you obtain the Guild object? There are two methods: Promises have a .then(value => {}) function which you could use, or you could use the simpler async/await method.

So here's how that would look in your ready event:

bot.on("ready", async () => {

    let myGuild = await bot.guilds.fetch('759858083718496266'); // Discord server ID
    let memberCount = myGuild.memberCount;
    let memberCountChannel = await myGuild.channels.fetch('123456789012345678');

    //rest of your code

});

Simply add async before your function declaration, and add await before each .fetch(). You need to do this in each of your event handlers; the above is only an example in your ready event handler.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...