Reputation: 176
I am trying to develop a command that allows the user to send a certain message on all text channels, this is what I did:
else if(command == 'snd') {
if(suffix == 'all'){
let channels = client.guilds.channels;
for (const channel of channels.values())
{
message.guild.channels.cache.get(channel.id).send(sParameter[1])
}
}
}
So, I run this command:
! sir snd all -test
and I get this error:
for (const channel of channels.values())
^
TypeError: Cannot read property 'values' of undefined
at Client.<anonymous> (C:\Users\utente\Desktop\bouncerBot\main.js:159:48)
at Client.emit (node:events:394:28)
at MessageCreateAction.handle (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:18)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:345:31)
at WebSocketShard.onPacket (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)
at WebSocketShard.onMessage (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10)
at WebSocket.onMessage (C:\Users\utente\Desktop\bouncerBot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (node:events:394:28)
at Receiver.receiverOnMessage (C:\Users\utente\Desktop\bouncerBot\node_modules\ws\lib\websocket.js:970:20)
at Receiver.emit (node:events:394:28)
at Receiver.dataMessage (C:\Users\utente\Desktop\bouncerBot\node_modules\ws\lib\receiver.js:517:14)
at Receiver.getData (C:\Users\utente\Desktop\bouncerBot\node_modules\ws\lib\receiver.js:435:17)
at Receiver.startLoop (C:\Users\utente\Desktop\bouncerBot\node_modules\ws\lib\receiver.js:143:22)
at Receiver._write (C:\Users\utente\Desktop\bouncerBot\node_modules\ws\lib\receiver.js:78:10)
at writeOrBuffer (node:internal/streams/writable:389:12)
Could anyone tell me why I'm getting that error? I am using discord.js 13.0.1
Edit:
C:\Users\utente\Desktop\bouncerBot\main.js:158
channel.send(sParameter[1])
^
TypeError: channel.send is not a function
at C:\Users\utente\Desktop\bouncerBot\main.js:158:25
at Map.forEach (<anonymous>)
at Client.<anonymous> (C:\Users\utente\Desktop\bouncerBot\main.js:157:36)
at Client.emit (node:events:394:28)
at MessageCreateAction.handle (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:18)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:345:31)
at WebSocketShard.onPacket (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)
at WebSocketShard.onMessage (C:\Users\utente\Desktop\bouncerBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10)
at WebSocket.onMessage (C:\Users\utente\Desktop\bouncerBot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (node:events:394:28)
at Receiver.receiverOnMessage (C:\Users\utente\Desktop\bouncerBot\node_modules\ws\lib\websocket.js:970:20)
at Receiver.emit (node:events:394:28)
at Receiver.dataMessage (C:\Users\utente\Desktop\bouncerBot\node_modules\ws\lib\receiver.js:517:14)
at Receiver.getData (C:\Users\utente\Desktop\bouncerBot\node_modules\ws\lib\receiver.js:435:17)
at Receiver.startLoop (C:\Users\utente\Desktop\bouncerBot\node_modules\ws\lib\receiver.js:143:22)
Upvotes: 0
Views: 214
Reputation: 2286
Here is where you are wrong:
client.guilds
returns a collection of cached guilds with the client so you may access the channels directly as client.channels
since they are available to your cache seperately ( cause of it being undefined ) , now the collection needs the object as whole to send a Message
to, so you may not need the Object.values()
of it and need to access their IDs each time that is just ineffecient you can create a for
loop directly. How?, Let me show you!
client.channels.cache.forEach( channel => {
channel.send(sParameter[1])
});
Also note that if there are more guilds your bot is in then you may not be able to fetch the channels from Message#Guild since they would be unavailable to that guild so your logic completely falsey
Upvotes: 1