Discord Welcome Channel Message - node.js

I made a discord bot and now I want to make it even more complex and I want the bot to also tell me when a user joins and leave the server in a Channel
I made the code like this and it sends only Welcome but doesn't take the users Name or ID.
Can someone help me with this, please?
client.on('guildMemberAdd', member => {
member.guild.channels.get('channel id').send("Welcome");
});
The bot sends the "Welcome" but without telling me the users Name and ID like other bots do.

client.on('guildMemberAdd', member => {
member.guild.channels.get('channel id').send("Welcome ${member.user.username} ${member.id}");
});

Related

How can you detect if a user replied to a message of a bot in discord.js?

I have searched the discord.js documentation but I can't find a way to make the bot detect if their own messages were replied by a user in a discord server. Is there a way to do this?
Thank you so much,
Paul10
All messages have a message_reference property. This contains the guild, channel and message id. This property is null if it isn’t a reply. I assume you already know the channel.
//async function
//message is an instance of Message
let { channel, message_reference: reply } = message
const repliedTo = await channel.messages.fetch(reply.message_id);
//repliedTo is now the replied message with author, content, etc

How to get all non bot users in discord js using a discord bot in nodejs

I have created a discord bot by taking reference from this digital ocean link.
Now I can send message to any channel using the bot but my requirement is to send dm to user of that server.
For that I have tried many SO answers and followed other links, but all the solutions end up to be same.
I have tried this two way to get the users of a guild and send dm to any one selected user.
1st way - Get all users of guild (server)
const client_notification = new Discord.Client();
client_notification.on('ready', () => {
console.log("Notification manager ready");
let guild = client_notification.guilds.cache.get("Server ID");
guild.members.cache.forEach(member => console.log("===>>>", member.user.username));
});
client_notification.login("login");
Output
Notification manager ready
===>>> discord notification
By this way it only returns me the bot name itself. Although the membersCount is 6.
2nd way - send dm to user directly (server)
client.users.cache.get('<id>').send('<message>');
It gives me undefined in output.
My configs,
Node version: 10.16.3
discord.js version: 12.5.1
My question is how to get all the guild members in discord.js?
Discord added privileged gateway intents recently. So to fetch all member data you need to enable that in the developer portal. After that you need to fetch all the members available, for that we can use the fetchAllMembers client option, and then we need to filter out bot users, so that we don't message them.
const client_notification = new Discord.Client({fetchAllMembers:true}); //Fetches all members available on startup.
client_notification.on('ready', () => {
console.log("Notification manager ready");
let guild = client_notification.guilds.cache.get("Server ID");
guild.members.cache.filter(member => !member.user.bot).forEach(member => console.log("===>>>", member.user.username));
});
client_notification.login("login");
The .filter() method filters out all the bots and gives only the real users.
You should avoid using fetchAllMembers when your bot gets larger though, as it could slow down your bot and use lots of memory.
I think the problem is related to updating the bot policy for discord. Do you have this checkbox checked in the bot settings?
https://discord.com/developers/applications
Some info about client.users.cache:
Its a cache collection, so if you restart bot, or bot never handle user message or actions before, this collection will be empty. Better use guild.members.cache.get('')

Discord.js : How to make a bot DM all members in a server?

I want my bot to DM all members in my server, but i have no clue on how to go about this. if you can help I'd appreciate it!
Discord.js v12
message.guild.members.cache.forEach(member => { // Looping through each member of the guild.
// Trying to send a message to the member.
// This method might fail because of the member's privacy settings, so we're using .catch
member.send("Hello, this is my message!").catch(e => console.error(`Couldn't DM member ${member.user.tag}`));
});

How do I make a bot say a certain message when it first joins a server?

I'm trying to make a discord bot say a certain message when it first joins a Discord Server, so when the bot first joins a Discord Server, it will say something along the lines of "Hello everyone....". I looked at a lot of sources but none seem to be JavaScript. Can anyone help?
client.on('guildCreate', joinedGuild => {
//Blah
}
Documentation for the guildCreate handler here.
You can set up an event listener to execute whenever it registers a new guild.
For example:
Client.on("guildCreate", guild => {
// Code to be executed whenever a new guild is registered
});
You can find out more here.

How to get contacts on Telegram

I use node.js module for Telegram bot.
I'm trying to get the user's contact on telegram using telegram API.
Telegram API has 2 types: Bot API and Telegram API.
I think Bot API can not get the user's contacts.
In Telegram API there is the method contact.getContacts. But I don't know how to use it.
How can I get the contacts on Telegram?
this code will give you the contact, user shares his/her contact with your bot , user types command '/special' will be prompted with button to allow bot to get contact and on agreeing in your node server you may log contact info remember to declare Markup ---->
//declare Markup
const {Extra,Markup}= Telegraf;
bot.command('special', (ctx) => {
return ctx.reply('Special buttons keyboard', Extra.markup((markup) => {
return markup.resize()
.keyboard([
markup.contactRequestButton('contact')
])
}))
})
//listens for the click on contact button
bot.on('contact', (ctx) => {
console.log(ctx.update.message.contact);
//logs { phone_number: '254*******',
//first_name: 'nelsonBlack',
//user_id: 73***** }
})
Bot API can get contact info too; I think it is easier in this case.
You can try reply keyboard with request_contact. If the user clicks it, you will receive message update with Contact field.

Resources