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)
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
Related
I started reworking my bot to V14 and when I wanted to create the Ping Command I came to a slight issue. When I try to call the client.ws.ping it says TypeError: Cannot read properties of undefined (reading 'ws').
Code:
const { SlashCommandBuilder } = require('../node_modules/#discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with the Bots Ping!'),
async execute(interaction, client) {
await interaction.reply({
content: `Pong! The current API Ping is: ${client.ws.ping}ms and the Bot Ping is: ${message.createdTimestamp - interaction.createdTimestamp}ms.`,
ephemeral: false
});
},
};
Error:
max#MacBook-Pro Blax-Bot % node .
I'm currently logged in in [REDACTED], on 9 Servers with 7 people.
TypeError: Cannot read properties of undefined (reading 'ws')
at Object.execute (/Users/max/Documents/Blax-Bot/commands/ping.js:8:79)
at Client.<anonymous> (/Users/max/Documents/Blax-Bot/main.js:79:17)
at Client.emit (node:events:513:28)
at InteractionCreateAction.handle (/Users/max/Documents/Blax-Bot/node_modules/discord.js/src/client/actions/InteractionCreate.js:81:12)
at module.exports [as INTERACTION_CREATE] (/Users/max/Documents/Blax-Bot/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (/Users/max/Documents/Blax-Bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
at WebSocketShard.onPacket (/Users/max/Documents/Blax-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:480:22)
at WebSocketShard.onMessage (/Users/max/Documents/Blax-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:320:10)
at WebSocket.onMessage (/Users/max/Documents/Blax-Bot/node_modules/ws/lib/event-target.js:199:18)
at WebSocket.emit (node:events:513:28)
Thanks in advance.
Change the order of your execute function from this:
async execute(interaction, client) {
To this:
async execute(client, interaction) {
Putting it in a different order fails, that's what I found investigating. I hope it has been helpful for you.
I used replit database to save channel id
This is my code:
client.channels.cache.get(db.get(args[1])).send(args[2]);
and i get this error:
client.channels.cache.get(db.get(args[1])).send(args[2]);
^
TypeError: Cannot read properties of undefined (reading 'send')
at Client.<anonymous> (/home/runner/anonymesssendbot/index.js:44:49)
at Client.emit (node:events:390:28)
at Client.emit (node:domain:475:12)
at MessageCreateAction.handle (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/actions/MessageCreate.js:34:18)
at Object.module.exports [as MESSAGE_CREATE] (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:346:31)
at WebSocketShard.onPacket (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:478:22)
at WebSocketShard.onMessage (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:317:10)
at WebSocket.onMessage (/home/runner/anonymesssendbot/node_modules/ws/lib/event-target.js:199:18)
repl process died unexpectedly: exit status 1
I'm guessing that db.get() returns a promise which you cannot pass as a value. Try retrieving the value first and then get the channel. Something like
const id = await db.get(args[1]);
const channel = client.channels.cache.get(id);
channel.send(args[2]);
There is also a possibility that the id is stored incorrectly in your server. So have a look at that. You could log the id to check if that is the case.
I am in the process of making a bot that has features that only work on a specific server. The server is about trading cards, and I want to make a bot that can moderate the server, and that people can have fun with.
The error looks like this:
c:\Users\REDACTED\Desktop\Sky\index.js:10
if (message.content === 'sky claim') {
^
ReferenceError: message is not defined
at Client.<anonymous> (c:\Users\kelly\Desktop\Sky\index.js:10:3)
at Client.emit (events.js:327:22)
at MessageCreateAction.handle (c:\Users\REDACTED\Desktop\Sky\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (c:\Users\REDACTED\Desktop\Sky\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (c:\Users\REDACTED\Desktop\Sky\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (c:\Users\REDACTED\Desktop\Sky\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (c:\Users\REDACTED\Desktop\Sky\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (c:\Users\REDACTED\Desktop\Sky\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (c:\Users\REDACTED\Desktop\Sky\node_modules\ws\lib\websocket.js:845:20)
The code for my Discord bot is here.
client.on("message", (msg) => {
if (message.content === "sky claim") {
message.channel.send("${message.author} claimed 250 SkyBucks. Spend SkyBucks on colored roles.");
console.log("${message.author} executed command successfully");
}
});`
I only added the part of the code that was giving the error.
If you read your error you should be able to find out why it doesn’t work:
client.on('message', message => {
if (message.content === 'sky eBay') {
message.channel.send('Our eBay store is #CollectAmazing, I will drop a link:');
message.channel.send('https://www.ebay.com/str/collectamazing');
}
})
Change all of the client.on('message', msg to client.on('message', message
I am making a !stats command in my discord.js bot using this Discord.js Guide.
As I am using the basic command handler, here is my stats.js code:
module.exports = {
name: 'stats',
description: 'React to a message',
execute(message, client) {
message.channel.send(`Server count: ${client.guilds.cache.size}`);
},
};
And then I am getting this error:
TypeError: Cannot read property 'cache' of undefined
at Object.execute (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\commands\Utility\stats.js:5:55)
at Object.execute (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\events\message.js:69:12)
at Client.<anonymous> (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\bot.js:30:61)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\ws\lib\event-target.js:132:16)
What can I do to solve this?
Whatever is before the cache property is undefined.
So in your case that's client.guilds. Given that this can't be undefined for a proper djs client object you are probably not passing your values in the proper order when calling that file. That is, client is probably not an actual djs client class/object.
What you discord.js version use?
Caches added on 12version.
Use npm i discord.js#12 or just remove called on field cache.
module.exports = {
name: 'stats',
description: 'React to a message',
execute(message, client) {
message.channel.send(`Server count: ${client.guilds.size}`);
},
};
I really don't know what's going on here. My code is inside the Akairo Framework but the error points in every direction to discord.js itself. Here's the error:
/home/runner/guard/Listeners/automod/nicknames.js:14
if (message.member.displayName.includes(word)) {
^
TypeError: Cannot read property 'displayName' of null
at module.exports.exec (/home/runner/guard/Listeners/automod/nicknames.js:14:32)
at Client.emit (events.js:327:22)
at Client.EventEmitter.emit (domain.js:483:12)
at MessageCreateAction.handle (/home/runner/guard/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/home/runner/guard/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/runner/guard/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/home/runner/guard/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/home/runner/guard/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/home/runner/guard/node_modules/ws/lib/event-target.js:125:16)
at WebSocket.emit (events.js:315:20)
This is my code: (bannedWords is an array of strings that I run through to check against the member's nickname) (it's inside a message event listener)
const bannedWords = require('./badwords.json').words;
for (const word of bannedWords) {
if (message.member.displayName.includes(word)) {
message.member.setNickname(`Moderated Nickname`)
}
}
There is a bit of a logical flaw in your code: you do not check if the message was sent in DMs. If the message was indeed a DM, then message.member would be null (as DM channels are not part of any server), which pretty much explains your error.
You should fix this by either explicitly checking (and returning) if the channel is a DM, or if it is a falsy value
//some code ...
if(!message.member) return;
for (const word of bannedWords) {
if (message.member.displayName.includes(word)) {
message.member.setNickname(`Moderated Nickname`)
}
}
TypeError: Cannot read property 'displayName' of null
This means "message.member" is null and it cannot find the property "displayName", which then makes sense.
I think we need to see more of your code in order to figure out where it went wrong. I'm guessing you are trying to run that if statement on a member object that doesn't exist.