TypeError: Cannot read properties of undefined (reading 'send') - javascript

I am writing a discord bot with node.js. I want the bot to send a message automatically when a member joins or leaves the channel. But I keep getting an error about the send function. Can you help me?
[1]: https://i.stack.imgur.com/4URud.png
module.exports = (client) => {
client.on('guildMemberRemove', (member) => {
console.log(member);
member.guild.channels.cache.get("1234567890912345678").send("byeee");
});
}

member.guild.channels.cache.get("1234567890912345678") is undefined, so there is no send() function. You need to identify why that channel is undefined or doesn't exist or find that channel using another method.

Related

I trying to make a discord bot using javascript..but it giving me error

enter image description here
it giving me this error:
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
^
TypeError: Cannot read properties of undefined (reading 'FLAGS')
I expect it to run my bot on my discord channel
It says that the object Intents are undefined and doesn't have property FLAGS. Please double-check your object, print it with console.log see if it actually there.

Discord.js bot doesnt see messages

My bot is supposed to send a message in console when message is sent. But it doesn't trigger after I am writing a messages on discord server.
Bot is online and joined the server. He also got all needed permissions.
CODE:
const Discord = require('discord.js');
const bot = new Discord.Client({disableEveryone: false, intents: ["0x0000000000000008"]});
bot.on('ready', () => {
console.log('Loaded!');
bot.user.setActivity('Running a test, hopefully.');
});
bot.on("Message", (message) => {
console.log('Message!');
});
bot.login("bot token");
You're using incorrect event name for the incoming message (Message). Correct name of this event is depends on your version of discord.js. If you're using version v13.* or greater, you should use messageCreate event:
// Correct event name for discord.js v13+
client.on('messageCreate', message => {
console.log('Message received! Message content: ' + message.content);
});
// And this event is deprecated
client.on('message', message => {
console.log('Message received! Message content: ' + message.content);
});
As an additional note regarding how you initialize your client: you should use Intents class on v13 or GatewayIntentBits on v14 for better readability. You can read more about intents for v13 here and changed initialization for v14 here.
In your event listener you have logged a string, not the message object
bot.on("message", (message) => {
console.log('Message!'); //incorrect - string, will log 'Message!'
console.log(message.content); //correct - content property of message obj, will log message content
});
You will also need to add the correct intents. Since MessageContent is a privileged gateway intent, you'll need to enable it in the Discord Developer Portal.
Check out a good article on this in the docs.

How to use "client" in my commands? (Discord.js v13)

TypeError: Cannot read properties of null (reading 'tag')
This error comes whenever I try to use client in a command like this: client.user.tag
my message.js includes:
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS]});
and
command.execute(client, message, args, MessageEmbed);
and my command file also contains: execute(client, message, args, MessageEmbed)
then also i am getting this error: TypeError: Cannot read properties of null (reading 'tag') a help will be really appreciated...
UPDATE:
Ok i just console logged the client and got this:
user: null,
application: null,
now why is the client user = null?
This looks like a caching issue make sure that you have not disabled user caching since this will include the client user.
client is a variable that holds the Discord bot client. You should read the docs to learn more about how it can be used.
It seems you are trying to get the client tag. If you read the docs, you will see that the client object has a property called user, which holds the user object. Since user objects hold the tag property, you're done.
client.user.tag

How to send an auto-welcome message?

I tried to make that my bot would send an auto-welcome message to new members.
Here is the code that didn't work:
bot.on('guildMemberAdd', member => {
member.guild.users.get(`${member.id}`).send("Heyo!");
});
Do you know a way I can fix it?
member already has a send() method.
bot.on('guildMemberAdd', member => {
member.send("Heyo!")
.catch(console.error);
});
Documentation on GuildMember

UnhandledPromiseRejectionWarning (Discord Bot)

Hey sorry if I posted this in the wrong place, I just signed up here (:
So I'm working on this discord bot that sends a DM to everyone in my server, but whenever it fails to send a dm (probably because someone has dm's disabled or other reasons) I get this error UnhandledPromiseRejectionWarning
I want it so that it keeps sending messages to other users, even tho a error comes along. Which right now, it stops sending to other users when the error comes.
My Code:
bot.on('message', message => {
if (message.content.startsWith('+dmall')) {
try {
message.guild.members.forEach(member =>
member.send("test"))
}
catch(error) {
console.log('Couldnt send dm to a user!');
}
}
})
UnhandledPromiseRejection comes from your member.send method which returns a promise. To fix this I've added a .catch to the method
message.guild.members.forEach(member => {
member.send("test").catch(console.log('Couldnt send dm to a user!'))
});
However please note that sending a DM to everyone in a guild is strictly against Discord ToS, and you can be reported for mass DMing, so use at your own risk.

Categories