0
votes

I want to create a script to know which one has my bot in servers and create my an invitation, the second I want to create a command who send one private message to all members (no its not a raid bot)

  1. script :

    client.on("ready", () => {
    client.user.setActivity('ESPADA');
        client.guilds.forEach(serv => {
          serv.channels.random().createInvite().then(invite => console.log(`>${serv.name} | ${invite}`))
          .catch(console.log(`>${serv.name} | Entrain d'envoyer une invite... `));
        });

  1. and my error :

    (node:10752) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined at Client.client.on (c:\Users\mathis\Downloads\self bot ash\Raid.js:10:18) at Client.emit (events.js:189:13) at WebSocketManager.triggerClientReady (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketManager.js:433:17) at WebSocketManager.checkShardsReady (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketManager.js:417:10) at WebSocketShard.shard.on.unavailableGuilds (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketManager.js:199:14) at WebSocketShard.emit (events.js:189:13) at WebSocketShard.checkReady (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketShard.js:466:12) at WebSocketShard.onPacket (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketShard.js:438:16) at WebSocketShard.onMessage (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10) at WebSocket.onMessage (c:\Users\mathis\node_modules\ws\lib\event-target.js:120:16)

  2. my DM command

    
        if(message.content === "!mp"){
            if(message.deletable) message.delete();
            i = 0;
            message.guild.members.forEach(member => { ```
    
    4. the error :
    
    TypeError: message.guild.members.forEach is not a function
        at Client.<anonymous> (c:\Users\mathis\Downloads\self bot ash\Raid.js:98:31)
        at Client.emit (events.js:194:15)
        at MessageCreateAction.handle (c:\Users\mathis\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
        at Object.module.exports [as MESSAGE_CREATE] (c:\Users\mathis\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
        at WebSocketManager.handlePacket (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
        at WebSocketShard.onPacket (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketShard.js:435:22)
        at WebSocketShard.onMessage (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
        at WebSocket.onMessage (c:\Users\mathis\node_modules\ws\lib\event-target.js:120:16)
        at WebSocket.emit (events.js:189:13)
        at Receiver.receiverOnMessage (c:\Users\mathis\node_modules\ws\lib\websocket.js:801:20)
    

thank you for the repply!

2

2 Answers

1
votes

This seems to be an issue with your discord.js version being v12 or greater (At least in your DM Command).
As of v12, Client#guilds and Guild#members no longer return a Collection instead they return a Manager. So now Guild#members returns a GuildMemberManager and Client#guilds returns a GuildManager.

Luckily there's still a way to gain access to the respective collections of Managers, and thats via: <SomeManager>.cache.
The cache property is in every single Manager without exception (As it's a property within the BaseManager).
So to gain access to the client.guilds or guild.members collections you'd do the following:

Client#guilds:

// This returns the collection: Collection<Snowflake, Guild>
// which then you can use methods on such as .forEach() or .every()
client.guilds.cache; 

// Using .forEach()
client.guilds.cache.forEach(guild => console.log(guild.name));

and Guild#members:

// This returns the collection: Collection<Snowflake, GuildMember>
// which then you can use methods on such as .forEach() or .every()
guild.members.cache;

// Using .forEach()
guild.members.cache.forEach(member => console.log(member.displayName));

There are a few other Manager types, I'd recommend consulting this to see the changes in v12. The older tutorials on youtube use discord.js v11 or under and I haven't seen any updated versions yet.

0
votes

My suggestion is to use every rather than forEach; I have encountered problems like this before.

P.S. if this is a selfbot, keep in mind that they are against Discord's terms of service.