NodeJS 获取服务器上所有用户的列表discord.js

8tntrjer  于 2023-03-01  发布在  Node.js
关注(0)|答案(2)|浏览(176)

我试图让所有用户从我的服务器与机器人使用discord.js,我写了这段代码,但它不工作,它告诉我:TypeError: client.guilds.get is not a function .下面是我的代码:

'use strict';
const Discord = require('discord.js');
const client = new Discord.Client();
const list = client.guilds.get("myServerID"); 
list.members.forEach(member => console.log(member.user.username)); 
client.login('myTokenID');

谢谢

56lgkhnf

56lgkhnf1#

更新(2023年2月25日):

对于不和谐v14+,
1.确保您有GatewayIntentBits.GuildMembers或类似的Intents。
1.在使用guild.members.cache之前,至少调用一次guild.members.fetch(),我认为后续调用可以使用cache。

const client = new Client({
    intents: [
        GatewayIntentBits.GuildMembers,  // MAKE SURE TO ADD THIS
    ],
});

client.on(Events.ClientReady, async (client) => {
    console.log("client ready");
    const guild = client.guilds.cache.get("<guild_id>");

    console.log("fetching users");
    let res = await guild.members.fetch();
    res.forEach((member) => {
        console.log(member.user.username);
    });
})

自discord.js v12以来,
现在您需要使用.cache访问guilds集合,我还可以看到您正在尝试访问members集合,因此您的解决方案将是:

'use strict';
const Discord = require('discord.js');
const client = new Discord.Client();
const list = client.guilds.cache.get("myServerID"); 
list.members.cache.forEach(member => console.log(member.user.username)); 
client.login('myTokenID');
jchrr9hc

jchrr9hc2#

只需使用.fetch()

const guild = await client.guilds.fetch('your_id')
const members = await guild.members.fetch() // returns Collection

相关问题