discord.js Greeting message not sending - javascript

I was following the discord.js docs and when I tried to test the greeting thing the message didn't send. The channel is right, I even tried doing it with the channel ID (with different code), I also tried just sending a direct message. No error appears, the console is just empty and the message doesn't appear.
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.cache.find(ch => ch.name === 'member-log');
if (!channel) return;
message.channel.send(`Welcome to the server, ${member}!`);
});

There is no need for "cache" in guildMemberAdd, that's why the channel was not found. The message variable was also not defined.
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.find(ch => ch.name === 'member-log');
if (!channel) return;
channel.send(`Welcome to the server, ${member}!`);
});

I had this same exact problem and for me it wasn't a problem in the code itself, I just gave the bot every single permission on the category of the welcome channel as well as the welcome channel itself.
It could also be that you have something else as the const name instead of 'client'.

Related

Discord.js bot Cannot read property 'send' of undefined

I am trying to implement for my discord server and my discord bot a possibility to send a PM message to a lot of users.
I created something like this:
client.on("message", message => {
mentiondm = message.mentions.users.first();
mentionMessage = message.content.slice(22)
mentiondm.send(mentionMessage);
console.log('Message Sent!')
})
It's working for first mentioned user, message is sending but there are several problems:
after sending first message bot is crashing down with this error Cannot read property 'send' of undefined
I tried to change message.mentions.users to message.mentions.roles but then message is not sending.
There is a way to do my functionality better or at least upgrade this what I have here to working properly?
It's because it tries to get the first mentioned user from the bot's message too. Check if the message author is a bot and if it is, don't send the message.
Also, try to check if there is someone mentioned and if not, simply return.
client.on('message', (message) => {
if (message.author.bot) return;
let mentionDM = message.mentions.users.first();
if (!mentionDM)
return console.log('No mentioned user');
let mentionMessage = message.content.slice(22);
mentionDM.send(mentionMessage);
console.log('Message Sent!');
});

Can I print the console log of my discord bot into a specific channel in discord.js?

I am making a discord bot which will send a dm to a particular user if the staff types
"!ping<#mentionusername>"
so, i want my bot to record its console logs of node.js in a particular channel so that other staff members can also see the logs instead of just the developer. Can anyone share with me the code for it?
Maybe you can create your own log function, or hooking the console.log function.
Simple way:
function log(msg)
{
console.log(msg);
const channel = member.guild.channels.cache.find(ch => ch.name === 'log');
if (!channel) return;
channel.send(msg);
}

(Discord.JS) Bot not sending DM message when a user join

For sending the DM Message, i use:
bot.on('guildMemberAdd', (member) => {
member.send('a');
});
I cannot understand what is wrong with this code so that the private message that should be sent is not being sent. Help-me, please!
The problem here is that you used member.send which is unnecessary and that's why the result is bad.
Use message.author.send('your text here');
Also, for the welcome code to work, this is my code:
client.on('guildMemberAdd', guildMember => { let welcome = guildMember.guild.roles.cache.find(role => role.name === 'Verified'); guildMember.roles.add(welcome); guildMember.guild.channels.cache.get('welcome channel id').send(Welcome <#${guildMember.user.id}> to my server! Make sure to check the rules channel!);
})
Also, replace client with bot. I named it differently.
Hope this helps!

Checking for roles in a discord bot error

I used this code to check for roles in my bot, but for some reason I get an error.
client.on("message", message => {
if (message.member.roles.cache.some(role => role.name == "ROLE NAME")) return;
// code...
});
The error is as follows:
TypeError: Cannot read property 'roles' of null
Does anybody have a solution?
It seems that message.member is undefined, you might want to check if this is done in a guild or not. If it is in a guild it will return the member property, while if it isn't, it won't. What you want to do is check if the message was sent from a guild or not, try the code below:
client.on("message", message => {
// `!` means `non-existent` or `is not`, and if the user sends the message from a guild
// this will not be triggered, since we know they are in, rather than not in, but, if
// it was sent outside of a guild, say a DM, then it will return the command, not trigerring
// any errors or such.
if (!message.guild) return;
// This will not allow this command to be triggered by the bot itself, since it may
// return a loop.
if (message.author === client.user) return;
// If the author of the message is a bot, then return, since bots can be used to spam
// and this will also spam your bot's API request. Webhooks work the same way.
// `||` means `or` if you didn't know.
if (message.author.bot || message.webhookID) return;
// Checks if the member has the role "ROLE NAME", and if they do, return.
if (message.member.roles.cache.some(role => role.name == "ROLE NAME")) return;
// code...
});

how to detect an embed and then resend it (discord.js)

so what i am trying to do is detect when a bot sends an embed in a channel, and when it does detect this, to take that embed and resend it the same as it was sent as.
For example, if the bot detects an embed sent in one channel, it will send that exact same embed in another channel. But the reason for this is because I want to take the embeds from multiple bots.
in discordjs.guide it says to use this code:
const receivedEmbed = message.embeds[0];
const exampleEmbed = new Discord.MessageEmbed(receivedEmbed).setTitle('New title');
channel.send(exampleEmbed);
but this has not worked for me
You need to replace channel in the line channel.send(exampleEmbed); with an actual reference to a channel. Since you will be using the message event handler, you can get the channel the message was sent in using message.channel.
I have also added in a check to ensure that the message was sent by a bot and contains an embed.
client.on('message', message => {
// check to ensure message was sent by bot and contains embed
if (!message.author.bot || !message.embeds[0]) return;
const receivedEmbed = message.embeds[0];
const exampleEmbed = new Discord.MessageEmbed(receivedEmbed).setTitle('New title');
// send in same channel
message.channel.send(exampleEmbed);
// send in different channel
client.channels.fetch(/* Channel ID */).then(channel => {
channel.send(exampleEmbed);
});
// alternatively, you can use this (but the function must be asynchronous)
const channel = await client.channels.fetch(/* Channel ID */);
channel.send(exampleEmbed);
});
For more information on valid properties and methods, read the Discord.js docs.
The following code will check if the message has any embeds and will resend the first one.
if (message.embeds) {
message.channel.send({ embed: message.embeds.first() || message.embeds[0] })
};
const embed = message.embeds[0];
const editedEmbed = embed
.setTitle('Edited!')
.addField('Test Field!', 'This is a test', true);
message.channel.send(editedEmbed);
This worked perfectly fine for me.
The problem will be that you don't have a TextChannel selected. (message.channel)

Categories

Resources