![](/img/trans.png)
[英](DiscordJs v13) How do I get a list of a voice channel in discord interaction guild
[英]How do I get the user count of a voice by channel id? Discordjs v.13
如何通過頻道 ID 獲取語音的用戶數? 我正在使用 Discordjs v.13。
這是我當前的代碼:
async function usercount(channelId) {
try {
let voiceChannel = Client.guilds.cache
.get('guildId')
.channels.cache.get(channelId);
let membersInChannel = voiceChannel.members.size;
console.log(membersInChannel);
} catch (error) {
logger.error('Error while performing usercount');
console.log(error);
}
}
您可以通過其 ID 獲取語音通道並獲取其members
屬性來獲取成員數。 您可能需要使用{ force: true }
選項來跳過緩存檢查並請求 API。 這樣,當有人離開/加入頻道時,號碼仍然是准確的。
請注意,您將需要公會 ID 或語音頻道所屬的公會。
async function userCount(guildId, channelId) {
try {
let guild = client.guilds.cache.get(guildId);
let voiceChannel = await guild.channels.fetch(channelId, { force: true });
return voiceChannel.members?.size;
} catch (error) {
logger.error('Error while performing usercount');
console.log(error);
}
}
這是一個如何使用它的示例。 在這里,我將message.guild.id
作為公會 ID 傳遞,第一個參數作為頻道 ID。
const { Client, Intents } = require('discord.js');
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_VOICE_STATES,
],
});
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
const channelId = args[0];
message.reply(
`There are ${await userCount(
message.guild.id,
channelId,
)} members in the voice channel _"${
message.guild.channels.cache.get(channelId).name
}"_`,
);
});
重要提示:不要忘記您需要啟用GUILD_VOICE_STATES
意圖。 否則,連接成員的數量不會改變。
無法通過通道 ID 獲取語音通道中的當前用戶數。 ID 就像頻道的名稱一樣。 讓您的機器人識別頻道並更改頻道名稱。 但是,您將無法獲取該頻道當前連接的用戶。
還是我誤會了什么? 希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.