Discord.js Bot repeats the same welcome message - discord

How do I prevent the bot from sending the same welcome message twice in a row? I think it has something to do with defining the server message intent.
client.on('guildMemberAdd', (member) => {
client.channels.cache.get('channelID').send("welcome");

Try this, this might help:-
client.on('guildMemberAdd', member => {
member.guild.channels.get('channelID').send("Welcome");
});

Removed the parentheses from client.on('guildMemberAdd', member => {

Related

Fetching all guild members [djs11]

I was wondering is it possible to fetch through all members in the guild and get their IDs?
I made something like
msg.guild.members.forEach(guildMember => {
guildMember.fetchMembers().then(console.log(guildMember.id))
})
But I got an error so I guess I did it in
TypeError: guildMember.fetchMembers is not a function
Also, I am using discord v11
Thanks in advance.
You could simply console log all user IDs like this:
msg.guild.members.forEach(guildMember => {
console.log(guildMember.user.id);
})

Leave message discord.js v12

I have a porblem. My leave message. There I have:
client.on("guildMemberAdd", member => {
const welcomeChannel = member.guild.channels.cache.find(
channel => channel.name === "welcome"
);
welcomeChannel.send(`Hosgeldin :heart: ${member}`);
});
client.on("guildMemberRemove", member => {
const welcomeChannel = member.guild.channels.cache.find(
channel => channel.name === "gelen-giden"
);
welcomeChannel.send(`Bye ${member}!`);
});
and then when somebody left then came <#id>.
I want to find his id because then I can make an link of the person who left.
It look like https://discordapp.com/users/732569703142129685 .
And then it should write 'Member' and this 'member' should be the link.
Please in v12.
Discord returns <#id> because the member that left doesn't have more guilds(servers) in common with you and he is no more in your cache. If you need his id for some deletion of rows in db you can get it as member.id
When a user leaves, he is no longer on the guild, so you can't find a channel on a server he isn't anymore in. You'll need to use client.channels.cache.find() in this case.
have a good day.

welcome event sometimes not firing all the way discord.js

My bot is suppose to welcome a member in both the main join/leave channel and also in the chat room so that way we can all welcome the user. For some reason there's a bug where sometimes it'll not send the welcome message to the chat room.
Error:
(node:194) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of null
The welcome event:
client.on('guildMemberAdd', async (member) => {
const welcomeEmbed = new Discord.RichEmbed()
.setAuthor(member.user.tag, member.user.avatarURL)
.setColor(`GREEN`)
.setDescription(`Welcome **${member.user.username}** to member.guild.name! Consider reading <# {HIDDEN}> to have a basic understand of what we do and do not allow.
Come hang out with us in <#{HIDDEN}> and have a good time!`)
.setFooter(`Member ID: ${member.id}`)
member.guild.channels.find(channel => channel.id === 'HIDDEN').send(welcomeEmbed);
const welcomeEmbed2 = new Discord.RichEmbed()
.setAuthor("New Member Joined!", member.user.avatarURL)
.setDescription(`**${member.user.tag} has joined the server!**`)
.setColor('GREEN')
member.guild.channels.find(channel => channel.id === 'HIDDEN').send(welcomeEmbed2); // This is the one providing the error sometimes
});
I've tried different things such as a .then or just recoding it in different ways to see if it'll work. So far, this has been the only thing my friends do not understand why it's providing errors
You can just fetch the channel by ID using client
client.channels.fetch("SOME_CHANNEL_ID").then(channel => {
channel.send(welcomeEmbed);
})

How to make a bot greet newcomers?

I would like to make a bot that greets users that joined the server.
Any help is appreciated.
Basically, you need to listen for the guildMemberAdd event, which is when someone joins.
After that, you need to check if the server is YOUR server, get the channel, and send the welcome message.
client.on('guildMemberAdd', async member => {
if (member.guild.id !== "YOUR-GUILD-ID") return;
var channel = client.channels.cache.get('YOUR-CHANNEL-ID');
channel.send(`Welcome to the server, <#!${member.id}>!`);
});
const defaultChannel = guild.channels.find(channel => channel.permissionsFor(guild.me).has("SEND_MESSAGES"));
const userlist = newUsers.map(u => u.toString()).join(" ");
defaultChannel.send("Welcome our new users!\n" + userlist);
newUsers.clear();
this is a code sample
Welcome dm message guildMemberAdd (Discord.js Version 11.4.2)
client.on('guildMemberAdd', async member =>{
await member.send(Embed);
});
Goodbye dm message guildMemberRemove (Discord.js Version 11.4.2)
client.on('guildMemberRemove', async member =>{
await member.send(Embed);
});

How to reply to any DMs sent to the bot?

I am trying to make my bot replying to any DM sent to it.
So I currently have this:
client.on('msg', () => {
if (msg.channel.DMChannel) {
msg.reply("You are DMing me now!");
}
});
But unfortunately, it does not reply to any DM.
I've tried to replace msg.channel.DMChannel with msg.channel.type == 'dm' but this didn't work.
I also tried to replace msg.reply with msg.author.send and msg.channel.send but they all didn't work.
Any help would be appreciated.
Thanks!
On the official documentation I don't see mentioned the event client.on('msg') only client.on('message').
With that out of the way:
client.on('message', msg => {
if (msg.channel.type == "dm") {
msg.author.send("You are DMing me now!");
return;
}
});
Just tried this and worked without any issues.
Your function isn't receiving the proper 'msg' object, try this:
client.on('message', async message => {
if (message.channel.type == 'dm') {
message.reply("You are DMing me now!");
}
});
Edit: I referenced sample code here to come to this conclusion. Maybe this link will help you in the future. Good luck!

Resources