Discord JS - 计算语音和文本通道
Discord JS - Counting voice and text channels
正在尝试为文本和语音频道制作一个计数器。所以它应该排除类别的数量。
module.exports = async (client) => {
const guild = client.guilds.cache.get('912706237806829598');
setInterval(async () => {
const channelCount = (await guild.channels.fetch()).filter(
(channel) => channel.type !== 'category'
).size;
const channel = guild.channels.cache.get('960790479925039124');
channel.setName(`╭・Channels: ${channelCount.toLocaleString()}`);
console.log('Updating Channel Count');
console.log(channelCount);
}, 600000);
};
这给了我 183,这是错误的。我想要达到的数字是 155。我有 28 个类别,所以如果类别没有被过滤,183 是有意义的。过滤类别比我预期的要困难得多。
我也试过在缓存上过滤。 guild.channels.cache.filter(channel => channel.type !== 'category').size;
但结果相同 (183)。所以过滤没有按预期工作。
如果您使用 discord.js v13+,channel types are now uppercase 并符合 Discord 的命名约定。请参阅下面的更改:
channel type
v12
v13
DM channel
dm
DM
group DM channel
N/A
GROUP_DM
guild text channel
text
GUILD_TEXT
guild text channel's public thread channel
N/A
GUILD_PUBLIC_THREAD
guild text channel's private thread channel
N/A
GUILD_PRIVATE_THREAD
guild voice channel
voice
GUILD_VOICE
guild stage voice channel
N/A
GUILD_STAGE_VOICE
guild category channel
category
GUILD_CATEGORY
guild news channel
news
GUILD_NEWS
guild news channel's public thread channel
N/A
GUILD_NEWS_THREAD
guild store channel
store
GUILD_STORE
generic channel of unknown type
unknown
UNKNOWN
如您所见,category
现在是 GUILD_CATEGORY
。这意味着,您的过滤器 (channel.type !== 'category'
) 不会匹配任何内容,因为不再有 category
类型。要解决此问题,您可以使用以下方法:
const channelCount = (await guild.channels.fetch()).filter(
(channel) => channel.type !== 'GUILD_CATEGORY'
).size;
正在尝试为文本和语音频道制作一个计数器。所以它应该排除类别的数量。
module.exports = async (client) => {
const guild = client.guilds.cache.get('912706237806829598');
setInterval(async () => {
const channelCount = (await guild.channels.fetch()).filter(
(channel) => channel.type !== 'category'
).size;
const channel = guild.channels.cache.get('960790479925039124');
channel.setName(`╭・Channels: ${channelCount.toLocaleString()}`);
console.log('Updating Channel Count');
console.log(channelCount);
}, 600000);
};
这给了我 183,这是错误的。我想要达到的数字是 155。我有 28 个类别,所以如果类别没有被过滤,183 是有意义的。过滤类别比我预期的要困难得多。
我也试过在缓存上过滤。 guild.channels.cache.filter(channel => channel.type !== 'category').size;
但结果相同 (183)。所以过滤没有按预期工作。
如果您使用 discord.js v13+,channel types are now uppercase 并符合 Discord 的命名约定。请参阅下面的更改:
channel type | v12 | v13 |
---|---|---|
DM channel | dm |
DM |
group DM channel | N/A | GROUP_DM |
guild text channel | text |
GUILD_TEXT |
guild text channel's public thread channel | N/A | GUILD_PUBLIC_THREAD |
guild text channel's private thread channel | N/A | GUILD_PRIVATE_THREAD |
guild voice channel | voice |
GUILD_VOICE |
guild stage voice channel | N/A | GUILD_STAGE_VOICE |
guild category channel | category |
GUILD_CATEGORY |
guild news channel | news |
GUILD_NEWS |
guild news channel's public thread channel | N/A | GUILD_NEWS_THREAD |
guild store channel | store |
GUILD_STORE |
generic channel of unknown type | unknown |
UNKNOWN |
如您所见,category
现在是 GUILD_CATEGORY
。这意味着,您的过滤器 (channel.type !== 'category'
) 不会匹配任何内容,因为不再有 category
类型。要解决此问题,您可以使用以下方法:
const channelCount = (await guild.channels.fetch()).filter(
(channel) => channel.type !== 'GUILD_CATEGORY'
).size;