Discord.js server welcome

Clash Royale CLAN TAG#URR8PPP
Discord.js server welcome
I have a discord.js bot.
Is it possible for the bot to DM the user who adds it to the server?
Example:
User#0004: adds bot to server
Bot in user#0004's DM: Hi, thanks for adding me to server etc..
Is this possible in Discord.js / node.js..
If so, could someone help me code it? Thanks! ;)
4 Answers
4
Unfortunately that is not possible just using Discord.js
What you could eventually do is have a website using Discord OAuth that the user needs to login with his account, and from there you generate the invite link for the bot, and then the user adds the bot to the server. But still that wouldn't be perfect, for example if 2 users invite the bot at the same time? The bot would only join once but there there would be no way to find out who really invited.
You could probably try something else instead, like trying to find a channel called lobby or general or something similar and then send the message there.
@AidanelGoste the question was to send a DM to the user that invited the Bot to the Server. And not when a new user joins a server.
– André
Jun 22 at 8:15
Also to the person that downvoted, may I know why?
– André
Jun 22 at 8:17
My bad, I was probably tired and misread your answer at the time.
– Aidan el Goste
Jun 25 at 2:45
I've been there too, but unfortunately, you can't just get the ID of the person who added your bot. But there certainly are solutions to this problem. I have a Discord bot too, and when the bot joins a new server, it finds the first channel where it can speak, check if it is a text channel and the bot has permission to speak there, and then sends a welcome message in the found channel.
Here's how I've done it:
client.on("guildCreate", guild =>
var found = false;
guild.channels.forEach(function(channel, id) channel.type != "text")
return;
// If the channel isn't found and the bot has permission to
// send and read messages in the channel, send a welcome message there
if(guild.me.permissionsIn(channel).has("SEND_MESSAGES") && guild.me.permissionsIn(channel).has("VIEW_CHANNEL"))
found = true;
return channel.send("Thanks for inviting me, blablabla")
)
);
For me it works perfectly, if you're having any issues with the code please let me know.
Sadly, you can't get the person that the bot invited, but, you can get the owner of the server where the bot has been invited (this do the most bots), You can write this:
client.on("guildCreate", guild =>
guild.owner.send('Thanks for adding me!')
);
You can easily get this done with this :
client.on("guildCreate", guild =>
guild.owner.send('Thanks! You can use +help to discover commands.')
);
Bonus : Add this code to the trigger to log new joins on the console :
// This event triggers when the bot joins a guild.
console.log(`New guild joined: $guild.name (id: $guild.id). This guild has $guild.memberCount members!`);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This is not true. I clearly remember discord.js commando having a way to send dm's to users, and numerous bots send dm's send serverwide dm's when they join.
– Aidan el Goste
Jun 22 at 5:30