Discord.js can't read property send of undefined - discord.js

I've recently learned the basics of Discord.js and I tried to make my personal bot just for fun and one of the command in that bot is a !say command that lets me make the bot say whatever I want wherever I want but I'm always getting this error:
Cannot read property 'send' of undefined
Code:
client.on('message', message => {
if(message.content === `${prefix}say`) {
const arguments = message.content.slice(prefix.length).split(/ +/);
const location = client.channels.chache.find(channel=>channel.id === arguments.shift());
const whatToSay = arguments.slice(1);
location.send(whatToSay);
// !say (channel Id) blah blah blah
}
});
I tried converting the arrow function into a normal function but it didn't work.
I tried replacing the arguments.shift() in the 4th line and it still didn't work.
I don't know where the mistake is

Check the spelling of the properties in your code.
client.channels.cache and arguments.shift

Related

Why isn't my code working? Discord.js index.js client.on

It's not sending a message, there are no errors I tried modifying the code nothing is working the bot welcome message still works I think that it's not updating with replit.
I tried modifying the code switching some stuff around basically doing stuff if I see any flaws I made.
client.on('message', message => {
// We'll want to check if the message is a command, and if it is, we'll want to handle it
if (!message.content.startsWith(prefix) || message.author.bot) return;
// Split the message into an array of arguments, with the command being the first element
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
// Check if the command is "privatevc"
if (command === 'Privatevc') {
// Check if the member is in a voice channel
if (!message.member.voice.channel) {
// If the member is not in a voice channel, send a message letting them know they need to be in one to use the command
return message.channel.send('You need to be in a voice channel to use this command.');
}
// If the member is in a voice channel, create a private voice channel for them
message.member.voice.createChannel({type: 'Voice'}).then(channel => {
// Send a message to the member letting them know the private voice channel has been created
message.channel.send(`Private voice channel created for you: ${channel}`);
}).catch(console.error); // If there was an error creating the private voice channel, log it to the console
}
});
This code is for Discord.js v12. If using the newest (v14) you need to use the messageCreate event instead of message. So the code would be:
client.on('messageCreate', message => {
You might also enable the MessageContent and GuildMessages intents.
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent
To make it all work you need to enable at least the third option, as you might see in the screenshot.

Error when console.log the displayname of the message owner

I get this Error:
TypeError: Cannot read property 'displayName' of null
This is the code that should console-log the command and the username of the user who used it:
client.on('message', (message) => {
const { content, member } = message
const autor = member.displayName || member.username
aliases.forEach((alias) => {
const command = `${prefix}${alias}`
if (content.startsWith(`${command}`) || content === command) {
console.log(`${autor} used: ${command}`)
callback(message)
}
})
})
In the console I get the username but it still gives an error. It only gives this error if I use a specific command. The command sends an embed of the message. Then it sends a copy of the message to the user who sent it.
if (message.content.includes('...'))
{message.delete({timeout: 800})
.then(message.member.send(message.content))
message.channel.send(embed)
Thank you for your help
If you want to use the nickname primarily, you can make your bot default to the username if the nickname is unavailable.
For your first section, you can use a ternary operator.
const autor = member ? member.displayName : message.author.username
If member returns null, it'll instead use the username property of the author which is never null.
For the second part of your code, you might as well replace message.member.send() with message.author.send() since then former provides no advantages over the latter and instead only leads to instances such as this one.
As from the documentation about message.member:
Represents the author of the message as a guild member. Only available if the message comes from a guild where the author is still a member
That means the error you are having may happen if the user is not a member of the server/guild anymore, or is a private message.
You can use message.author if you just want the username. Or you can return the function when message.member is null.

I'm having trouble with moving a user that is already in a voice channel to another voice channel

else if (command === 'move'){
const member = client.users.cache.find(user => user.username == "0.o");
const chan = client.channels.cache.get('761321016760336444');
member.setVoiceChannel(chan);
Error
TypeError: member.setVoiceChannel is not a function
i've seen setVoiceChannel used before, not sure why it's not working for me
If you're using discord.js v12 you should do this instead
member.voice.setChannel(chan);
you can read more about the voice state here

Discord js TypeError: Cannot read property members

Hello i have this code,
user = message.guild.members.fetch(id2).then((use,err)
And i have this error
TypeError: Cannot read property 'members' of null
Please can yuo help me ?
Thank you
message.guild is not initialized. You could check if it is null before use eg
if(message.guild){
user = message.guild.members.fetch(id2).then((use,err) ...
}else{
//do something when it is not initialized
}
Your error occurs because the message object refers to a message that was received as a DM. Because of how DMs work, there is no guild or member property for such message (they are left as nulls).
To avoid that, you should handle direct messages slightly differently. The easiest and most commonly used way is to completely stop direct messages from running your message event code. This can be done by adding
if (message.channel.type === 'dm') return;
at the very top of your event.
As that makes it impossible to initiate commands in DMs, even if they don't need to be executed in a guild to work (like ping command for example), this might be not what you want. In such case, you should implement a way to determine if command someone tried to run in DM is "allowed" to be executed there. Implementations for that vary depending on command handling implementation, but snippet below is basic princinple.
client.on('message', message => {
if (message.author.bot || !message.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).split(/ /g);
const command = args.shift().toLowerCase();
if (command === 'memberinfo') {
if (message.channel.type === 'dm') return message.reply('this command cannot be run in DMs.');
// Actual command code
}
});

"Announce" command giving errors in console

I'm trying to write an announce command which when used sends an announcement to the announcements channel, but only if you have a certain role, that works, but it shows an error in my console.
The error is TypeError: client.fetchGuild is not a function
if (await client.fetchGuild(message.guild.id).fetchMember(message.author.id).hasPermission("MENTION_EVERYONE") && message.content.startsWith(adminPrefix + 'announce')) {
const trueMessage = message.content.substr(10)
client.channels.get('545343689216491541').send('#everyone, ' + trueMessage)
}
How do I make it send no errors
P.s. I'm new to this, very new.
fetchGuild
is not a function. Use
client.guilds.get(id)
or
message.guild
as you already have the guild attached to the message object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=guilds
In order to verify if a member has a role, you'd better use this:
if(message.member.roles.cache.some(role => role.name === 'role name'))
then send the message inside the if statement

Resources