discord.js获取所有当前在线帮会成员用户名

xzlaal3s  于 2021-10-10  发布在  Java
关注(0)|答案(2)|浏览(401)

我正在使用discord.js 12.5.3的最新版本,并尝试了几种不同版本的方法。
在我看到的文档中有两个参数,第一个是“客户端”,第二个只是“数据”,它没有指定需要哪些数据,所以第一次构建discord机器人有点奇怪。
我已尝试将第一个参数仅用于以下代码:

bot.on("message", async (msg) => {
  if (!isCommand(msg) || msg.author.bot) return;

  const guild = new Discord.Guild(bot);
  console.log(guild);

});

然后我得到这个:

<ref *1> Guild {
  members: GuildMemberManager {
    cacheType: [class Collection extends Collection],
    cache: Collection(0) [Map] {},
    guild: [Circular *1]
  },
  channels: GuildChannelManager {
    cacheType: [class Collection extends Collection],
    cache: Collection(0) [Map] {},
    guild: [Circular *1]
  },
  roles: RoleManager {
    cacheType: [class Collection extends Collection],
    cache: Collection(0) [Map] {},
    guild: [Circular *1]
  },
  presences: PresenceManager {
    cacheType: [class Collection extends Collection],
    cache: Collection(0) [Map] {}
  },
  voiceStates: VoiceStateManager {
    cacheType: [class Collection extends Collection],
    cache: Collection(0) [Map] {},
    guild: [Circular *1]
  },
  deleted: false
}

然后我试着 console.log(guild.members); 我得到

<ref *1> GuildMemberManager {
  cacheType: [class Collection extends Collection],
  cache: Collection(0) [Map] {},
  guild: <ref *2> Guild {
    members: [Circular *1],
    channels: GuildChannelManager {
      cacheType: [class Collection extends Collection],
      cache: Collection(0) [Map] {},
      guild: [Circular *2]
    },
    roles: RoleManager {
      cacheType: [class Collection extends Collection],
      cache: Collection(0) [Map] {},
      guild: [Circular *2]
    },
    presences: PresenceManager {
      cacheType: [class Collection extends Collection],
      cache: Collection(0) [Map] {}
    },
    voiceStates: VoiceStateManager {
      cacheType: [class Collection extends Collection],
      cache: Collection(0) [Map] {},
      guild: [Circular *2]
    },
    deleted: false
  }
}

其中缓存是一个 Collection(0) [Map] {}console.log(guild.members.guild.members); 提供与相同的输出 console.log(guild.members); 所有标记为有效的代码块都来自旧版本或对我不起作用。
我在猜测我是否需要在另一个地方登录机器人,但只要消息回复有效。。。以下是代码供参考:

const Discord = require("discord.js");
const bot = new Discord.Client({ disableMentions: "everyone" });
const dotenv = require("dotenv");
dotenv.config();

bot.login(process.env.token);

// event handlers
bot.once("ready", () => {
  console.log("Ready!");
});

bot.on("message", async (msg) => {
  if (!isCommand(msg) || msg.author.bot) return;

  /* 
  // get guild online member usernames

  const guild = new Discord.Guild(bot);
  console.log(guild.members);
   */

  msg.reply(handle(msg.content, msg.author.username));
});

你能告诉我如何达到这个目的吗?谢谢

7gyucuyw

7gyucuyw1#

您可以从中获取发送邮件的当前服务器 message 它本身 message.guild 返回 guild 消息被发送进来了。 guild 有一个 members 返回该公会成员经理的道具。要获取所有成员,可以使用 fetch() 方法,该方法返回承诺并解析为成员集合。收藏品有很多优点 map() 可用于创建用户名数组的方法。
检查用户是否在线有点棘手。你可以查看他们的 presence . 它的 status 道具可以是 online , idle , offlinednd . 你可以 filter 您在上面创建的数组,并且只返回状态为非的数组 offline .
像这样的方法应该会奏效:

bot.on('message', async (msg) => {
  if (!isCommand(msg) || msg.author.bot) return;

  let { guild: { members } } = msg;
  let allMembers = await members.fetch();
  let onlineUsers = allMembers.filter((member) => member.presence.status !== 'offline');
  let usernames = onlineUsers.map((member) => member.displayName);

  // array of usernames of users who are not offline
  console.log(usernames);
});
dfddblmv

dfddblmv2#

解决了的:
我发现我需要在discord developer portal->bot中启用特权网关意图。
然后按如下方式设置机器人:

const Discord = require("discord.js");
const bot = new Discord.Client({ disableMentions: "everyone", ws: { Intents: ["GUILD_MEMBERS", "GUILD_PRESENCES"] } });

最后,我能够获得在线帮会用户,而不是机器人与此

bot.on("message", async (msg) => {
  if (!isCommand(msg) || msg.author.bot) return;

  let {
    guild: { members },
  } = msg;

  let allMembers = await members.fetch();
  let onlineUsers = allMembers.filter((member) => member.presence.status !== "offline");
  let availableUsers = onlineUsers.filter((member) => !member.user.bot);
  let usernames = availableUsers.map((member) => member.displayName);

  console.log(usernames);

  msg.reply(handle(msg.content, msg.author.username));
});

所以这是关于权限和一些调整。这在discord.js文档中没有链接或广告,因此有点混乱。

相关问题