在 Discord.js 中查找所有具有角色的成員 (Finding all members with a role in Discord.js)


問題描述

在 Discord.js 中查找所有具有角色的成員 (Finding all members with a role in Discord.js)

我已經嘗試過使用每個人所說的內容,但它總是給我空數組或未定義的數組。我相信我正在使用 discord.js v12。我真的很確定我確實有這個角色的成員,並且 roleID 是正確的。為什麼這是錯誤的?將 new_channel 與頻道進行比較。我想 find 函數中的 ${} 不是必需的,但這不是問題。直到 roles.cache.get(importantRole.id) 部分都正常,但我似乎無法從中獲取成員。

let importantRole = await new_channel.guild.roles.cache.find(r => r.name === `${new_channel.name}`)
console.log(importantRole.id) // ‑‑> gives the right role ID
let membersWithRole = await new_channel.guild.roles.cache.get(importantRole.id).members.map(m=>m.user.id);
console.log(membersWithRole) // ‑‑> gives []


參考解法

方法 1:

Most likely the members with that role are not cached. You need to:

  1. Enable the GUILD_MEMBERS privileged intent on the Discord developer panel if you have not already (if your bot is less than 100 guilds this is a simple toggle; if your bot has >100 guilds and is verified you need to apply for these, it's to protect user privacy), and
  2. Enable the GUILD_MEMBERS privileged intent in your Discord.js Client's options (the object that is passed to new Client({...}) by passing { ws: { intents: [ 'GUILD_MEMBERS' ] } }
  3. Fetch all of the guild's members when this command/module/etc runs (only do this when strictly necessary, and not on an interval, as it causes an extra API call)

  4. </ol>

    Also, there's no need to do `${variable}` as variable does the same thing. The ` and ${...} syntax are only needed when merging variables with strings `like${this}` (= 'like' + this)

    To fetch all guild members, you can do:

    <Guild>.members.fetch() // => Promise<GuildMemberManager>
    

    Note that for some reason, in my testing, the value of the resolved promise doesn't work as a valid GuildMemberManager, so you should do:

    //                            _ = temporary var
    <Guild>.members.fetch().then((_) => {
        // do something with <Guild>.members.cache
    });
    

    (by mikemaster980xLogN)

    參考文件

    1. Finding all members with a role in Discord.js (CC BY‑SA 2.5/3.0/4.0)

#discord #node.js #discord.js






相關問題

如何讓不和諧的機器人提及用戶? (How to make a discord bot mention a user?)

如何計算表情符號並在不和諧的機器人反應中顯示它們? (How to count the emojis and display them in a discord bot reaction?)

每當機器人啟動時,它就會開始向控制台發送垃圾郵件,沒有明顯的問題 (Whenever the bot starts up, it just starts spamming the console with no visible issue)

Discord.js ReferenceError:嵌入未定義 (Discord.js ReferenceError: embed is not defined)

在 Discord.js 中查找所有具有角色的成員 (Finding all members with a role in Discord.js)

啟動兩個 CPU 阻塞偵聽器並等待其中一個完成 (Starting two cpu blocking listeners and wait until one of them finishes)

discord.py - 猜謎遊戲無響應(更新) (discord.py - Guessing game no response (updated))

\n 換行符不保留代碼塊文本樣式 (\n Line break does not preserve code block text style)

如何在對不同消息做出反應時讓不和諧機器人刪除以前的角色? (How to make discord bot remove the previous role when reacting to a different message?)

一段代碼阻礙了我在學校的 Discord 機器人 (One piece of code hindering my Discord bot for School)

如何使不和諧機器人隨機跳過響應 (How to make discord bot randomly skip a response)

我應該放什麼行,這樣當我寫東西時,Discord 機器人會以我想要的方式響應(discord.py)? (What lines should I put so that when I write something the Discord bot responds in a way that I want (discord.py)?)







留言討論