te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Discord.js server welcome - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Discord.js server welcome - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 25, 2018 at 19:45 willwill 711 gold badge3 silver badges8 bronze badges 1
  • StackOverflow is not a code-writing service. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help – Andreas Commented Jun 28, 2019 at 3:31
Add a ment  | 

7 Answers 7

Reset to default 9

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

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 wele 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 wele 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 e 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.");
  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}!`);
}
发布评论

评论列表(0)

  1. 暂无评论