首页 > 解决方案 > 是否可以在 js 中创建一个不和谐的机器人,从邀请链接或服务器 ID 获取服务器的成员数?

问题描述

所以我想用js做一个不和谐的机器人。如果您向机器人发送邀请链接,它应该能够告诉您服务器的成员数。可以这样编程吗?

标签: javascriptdiscorddiscord.js

解决方案


是的,您可以通过让机器人在加入后将公会中的所有用户放入一个数组并发送该数组的长度来做到这一点。

const Discord = require("discord.js");
const bot = new Discord.Client();
const token = 'YOUR_TOKEN';

bot.on('guildCreate', guild => {
  //first the bot is going to find a channel to send the user count in
  let defaultChannel = "";
  guild.channels.forEach((channel) => {
    if (channel.type == "text" && defaultChannel == "") {
      if (channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
        defaultChannel = channel;
      }
    }
  })
  let arr = guild.members.array();
  defaultChannel.send(arr.length);

});

bot.login(token);

推荐阅读