列出所有公会的用户 ID (discord.js)

2022-12-31前端开发问题
8

本文介绍了列出所有公会的用户 ID (discord.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我知道此代码中的某些元素可能已被弃用.但是,我找到了这段代码,并在一定程度上对其进行了更改,但我收到了一个错误.我正在寻找所有公会中的用户 ID,我知道您可能会受到所有公会中用户数量的限制.

<块引用>

TypeError: client.guilds.forEach 不是函数

//公会成员IDclient.on('消息', 消息 => {if (message.content === '$listmids') {if (message.author.id !== "651167823749578798") 返回;client.guilds.forEach(成员 => {console.log(`${member.id}`)var idserver = (`${member.id}`)//id 每个公会var memserver = client.guilds.get(idserver);//检查公会idmemserver.members.forEach(成员 => {console.log(member.user.id);//console.log 查看该公会中的 id 用户});//获取所有公会的用户 ID});}});

解决方案

你不需要获取用户,他们已经被缓存了.你不能使用

您还可以使用 websocket 事件 GUILD_CREATE 在机器人启动时将所有用户 ID 记录到控制台:

client.ws.on("GUILD_CREATE", (data) => {console.log(`列出来自公会'${data.name}'的用户ID`);console.log(data.members.map(m => m.user.id));});


至于与discord.js 13.xx的兼容性,是完全兼容的,但是需要GUILDSintent来接收GUILD_CREATE网络套接字.并且 GUILD_PRESENCES 意图将用户包含在公会数据中.

还有 GUILD_MESSAGES 以使命令正常工作.并为 messageCreate 而不是 message 侦听 (client.on(...)),它已被弃用.

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

使用 discord.js ^12.5.3.

I'm aware that some of the elements in this code has probably been deprecated. However, I found this code and I have altered it to some extent but I am receiving a error. I'm looking to fetch user ids in all guilds, I understand that you can get ratelimited by the amount of users in all guilds.

TypeError: client.guilds.forEach is not a function

// guild member ids
client.on('message', message => {
    if (message.content === '$listmids') {
        if (message.author.id !== "651167823749578798") return;
        client.guilds.forEach(member => {
            console.log(`${member.id}`)

            var idserver = (`${member.id}`) // id each guild
            var memserver = client.guilds.get(idserver); // check guild with id

            memserver.members.forEach(member => {
                console.log(member.user.id); // console.log see id user in that guild
            }); // get userid all guild
        });
    }
});

解决方案

You don't need to fetch the users, they are already cached. You can't use GuildManager.fetch() nor UserManager.fetch() to fetch all the users or guilds, since it has an id (Twitter Snowflake) as a required parameter (referring to @MrMythical's answer).

When a discord bot is started an event GUILD_CREATE is emitted on the websocket for each guild. The client uses this to cache all the .guilds and .users, etc.

So you can simply use:

// An array of user ids
const users = client.users.cache.map(u => u.id);

To implement this in a command:

client.on("message", (message) => {

    if (message.author.id == client.user.id) return;

    if (message.content == "$listmids") {
        const users = client.users.cache.map(u => u.id);
        console.log(`Listing user ids from all guilds:`);
        console.log(users);
    }

});

You can also use the websocket event GUILD_CREATE to log all user ids to console on bot's startup:

client.ws.on("GUILD_CREATE", (data) => {
    console.log(`Listing user ids from a guild '${data.name}'`);
    console.log(data.members.map(m => m.user.id));
});


As for compatibility with discord.js 13.x.x, it is fully compatible, but you need GUILDS intent to receive GUILD_CREATE on the websocket. And GUILD_PRESENCES intent to include the users in the guild data.

Also GUILD_MESSAGES for the command to work. And listen (client.on(...)) for messageCreate instead of message, it is deprecated.

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

Using discord.js ^12.5.3.

这篇关于列出所有公会的用户 ID (discord.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13