Discord.js throws ReferenceError: message is not defined - javascript

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

Related

TypeError: Cannot read properties of undefined (reading 'ws') - Discord.js V14

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.

How to send a message in a specific channel with id

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.

discord auto role when status matches

Ive tries some different things to make this work but it's not wanting to work
so am basicly trying to check someones custom status for a specific thing if there is for example .gg/test in it and give them a role than
My code
client.on('presenceUpdate', async (oldPresence, newPresence) => {
const role = newPresence.guild.roles.cache.get("927284722059595828");
const member = newPresence.member
const activities = member.user.presence.activities[0];
if (activities && (activities.state.includes( ".gg/test" ) || activities.state.includes("discord.gg/test" ))) {
return newPresence.member.roles.add(role)
} else {
if(member.roles.cache.get(role.id)) {
newPresence.member.roles.remove(role)
}
}
})
so can anyone correct the code to the correct format
and if possable make this multi server usable with a command thanks
the error i get
TypeError: Cannot read properties of undefined (reading 'id')
at Client.<anonymous> (/root/raegay2/boy.js:19:36)
at Client.emit (node:events:390:28)
at PresenceUpdateAction.handle (/root/raegay2/node_modules/discord.js/src/client/actions/PresenceUpdate.js:39:19)
at Object.module.exports [as PRESENCE_UPDATE] (/root/raegay2/node_modules/discord.js/src/client/websocket/handlers/PRESENCE_UPDATE.js:4:33)
at WebSocketManager.handlePacket (/root/raegay2/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/root/raegay2/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/root/raegay2/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/root/raegay2/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (node:events:390:28)
at Receiver.receiverOnMessage (/root/raegay2/node_modules/ws/lib/websocket.js:825:20)
I just tested your code and it worked fine. Did you enable Presence Intent on your bots discord developer page? If you are using Gateway Intents, you must specify the GUILD_PRESENCES intent in order to receive Presence Update events.

Discord SDK client.guilds.channels.values is undefined

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

Getting an error on Discord JS "cannot read property of 'content' undefined"

CODE IS BELOW
I have a simple discord bot and added a message file under events--guild
module.exports = (Discord, client, message) => {
const prefix = '!';
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command = client.commands.get(cmd);
if(command) commmand.execute(client, message, args, Discord);
}
```
Just Incase You need it here is my main file
```
const Discord = require('discord.js');
const client = new Discord.Client();
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
['command_handler', 'event_handler'].forEach(handler =>{;
require(`./handlers/${handler}`)(client, Discord)
})
client.login('My Token');
full error below
TypeError: Cannot read property 'content' of undefined
at Object.module.exports (E:\Software\Discord Bots\BombBot\events\guild\message.js:3:17)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (E:\Software\Discord Bots\BombBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (E:\Software\Discord Bots\BombBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (E:\Software\Discord Bots\BombBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (E:\Software\Discord Bots\BombBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (E:\Software\Discord Bots\BombBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (E:\Software\Discord Bots\BombBot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:315:20)
About: this discord bot is a simple bot that I am trying do with a command and event handler as I am new to discord js and not to javascript, I tried to see if there was a few issues, but couldn't find any. The debug window can't help either because its a value/variable that shows as undefined but is defined above.
as I am a bit new I used the tutorial: https://www.youtube.com/watch?v=Sihf7B8D4Y8 and rewrote the code multiple times. If you have an idea please tell me before I rewrite it again.
The Debugger is below
E:\Software\node JS\node.exe .\index.js
BOT Is ONLINE
events/client/ready.js:4
Uncaught TypeError: Cannot read property 'content' of undefined
No debugger available, can not send 'variables'
Process exited with code 1
It's hard to determine the origin of the error based on the info you provided.
The message content is not guaranteed. Messages from other bots that are embeds have no content, thus it's undefined. To fix this, add Optional Chaining in your condition:
if(!message.content?.startsWith(prefix) || message.author.bot) return;
If that doesn't work, that probably means your message object is not Discord Message at all. Make sure you pass the arguments to your function correctly.

Categories