简体   繁体   中英

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! ;)

You can easily get this done with this code, although this code assumes that the owner added the bot:

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!`);

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.

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) {
      // If a channel is already found, nothing more needs to be done
      if(found == true || 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.

This is possible through the client's guildCreate event. So:

<client>.on('guildCreate', guild => {
   guild.owner.send("Thanks for adding me to your server!")
})

However, you might come across the error of Cannot read property 'send' of null , this is because the guild's owner is a nullable property as it might not be in cache. To fix this, you can utilize guild.members.fetch(guild.owner.user.id) getting the member into cache. However, you do need to await the returned promise so:

<client>.on('guildCreate', guild => {
   let owner = await guild.members.fetch(guild.owner.user.id)
   owner.send("Thanks for adding me to your server!")
})

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!')
});

Try something like this

const Discord = require("discord.js");
const client = new Discord.Client();

client.on("guildCreate", server => {
  let embed = new Discord.RichEmbed() // Makes a pretty embed
    .setTitle("Thanks for adding me to your server!")
    .setDescription("something here...")
    .setColor("RANDOM")
    .setFooter("Copyright ExampleBot.com");
  server.owner.send(embed);
});

client.login("token");

First, we need to check if the bot has been added to the guild. This can be done by using this code:

client.on("guildMemberAdd", (guild) => {

});

After we do that, we should then actually send the owner the message:

client.on("guildMemberAdd", (guild) => {
    guild.owner.send(`Hello, thanks for adding me to ${guild.name}!`);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM