【问题标题】:Unable to fetch all guild members through Discordjs v13无法通过 Discordjs v13 获取所有公会成员
【发布时间】:2021-10-20 00:39:40
【问题描述】:

我一直在尝试使用文档here 中提到的语法来获取我的公会中所有成员的列表。但我没有收到任何输出。下面是我正在使用的代码。

const { Client, Intents, Guild } = require("discord.js");
require("dotenv").config();
const commandHandler = require("./commands");
const fetch = require("node-fetch");

const client = new Client({
    intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_PRESENCES"],
});

client.once("ready", async (data) => {
    console.log("Ready!");
    console.log(`Logged in as ${client.user.tag}!`);
    const Guilds = client.guilds.cache.map((guild) => guild);
    console.log(Guilds[0]);
    await Guilds[0].members.fetch().then(console.log).catch(console.error);
});

client.on("interactionCreate", async (interaction) => {
    if (!interaction.isCommand()) return;

    if (interaction.commandName === "ping") {
        await interaction.reply(client.user + "????");
    }
});

client.on("messageCreate", commandHandler);
client.login(process.env.BOT_TOKEN); // Kushagra's bot

我正在通过第 13 行的 console.log 获取有关 Guild 实例的信息。但是第 15 行没有得到任何输出。 但是,我注意到如果我在第 14 行向fetch() 命令添加选项,我会得到一些输出。当我添加{query: 'random text'} 时,我收到了一些输出。

只是想了解为什么我无法获得所有成员的列表。 谢谢!????

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    我发现我错过了 Intent。 似乎为了获取成员,您需要在创建客户端实例时添加意图 GUILD_MEMBERS。

    所以用下面的替换客户端创建就可以了。

    const client = new Client({ 
        intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_PRESENCES", "GUILD_MEMBERS"] 
    });
    

    【讨论】:

    • 非常感谢。我花了几个小时寻找修复方法,直到我意识到我没有添加意图。
    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 2022-01-23
    • 2022-01-11
    • 1970-01-01
    • 2020-11-11
    • 2022-01-13
    • 2021-12-03
    相关资源
    最近更新 更多