最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Discord js v13 channel filter not working - Stack Overflow

programmeradmin2浏览0评论

I'm currently trying to get the total amount of text channels and voice channels to display in my embed, when I try to filter them as I did in discord.js v12 it gives me an output of 0 but if I use no filter and do guild.channels.cache.size, it prints 4 which is the correct amount ( 2 text channels, 1 voice channel , 1 category channel).

If anyone can explain why it's printing 0 and not the correct amount of text/voice channels that would be amazing, I've searched everywhere and can't find a reason why it wouldn't work.

const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed } = require('discord.js');

// EXPORT SERVERINFO COMMAND DATA TO NODE
module.exports = ({
    data: new SlashCommandBuilder()
        .setName('serverinfo')
        .setDescription('Basic Server Info.'),
    async execute(interaction) {
        // REFERENCE THE GUILD
        const guild = interaction.guild;
        // CREATE TEST EMBED
        const serverInfoEmbed = new MessageEmbed();
        serverInfoEmbed.setColor('#36393F');
        serverInfoEmbed.setAuthor('Fyce Bot - /serverinfo', interaction.user.avatarURL(), '/');
        serverInfoEmbed.setTitle('Server Information');
        serverInfoEmbed.setThumbnail(guild.iconURL());
        serverInfoEmbed.addFields(
            { name: 'Name', value: `${guild.name}`, inline: true },
            { name: '\u200B', value: '\u200B', inline: true },
            { name: 'Owner', value: `<@${guild.ownerId}>`, inline: true },
            { name: 'Total Members', value: `${guild.memberCount}`, inline: true },
            { name: 'Users Count', value: `${guild.members.cache.filter(member => !member.user.bot).size}`, inline: true },
            { name: 'Bots Count', value: `${guild.members.cache.filter(member => member.user.bot).size}`, inline: true },
            { name: 'Text Channels', value: `${guild.channels.cache.filter(channels => channels.type === 'text').size}`, inline: true }, // PROBLEM HERE 
            { name: 'Voice Channels', value: `${guild.channels.cache.filter(c => c.type === 'voice').size}`, inline: true }, // PROBLEM HERE 
            { name: 'Roles Count', value: `${guild.roles.cache.size}`, inline: true },
        );
        serverInfoEmbed.setFooter(`${guild.name} - Date Created`);
        serverInfoEmbed.setTimestamp(`${guild.createdAt.toUTCString().substr(0, 16)}`);

        await interaction.reply({ embeds: [serverInfoEmbed] });
    },
});

I'm currently trying to get the total amount of text channels and voice channels to display in my embed, when I try to filter them as I did in discord.js v12 it gives me an output of 0 but if I use no filter and do guild.channels.cache.size, it prints 4 which is the correct amount ( 2 text channels, 1 voice channel , 1 category channel).

If anyone can explain why it's printing 0 and not the correct amount of text/voice channels that would be amazing, I've searched everywhere and can't find a reason why it wouldn't work.

const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed } = require('discord.js');

// EXPORT SERVERINFO COMMAND DATA TO NODE
module.exports = ({
    data: new SlashCommandBuilder()
        .setName('serverinfo')
        .setDescription('Basic Server Info.'),
    async execute(interaction) {
        // REFERENCE THE GUILD
        const guild = interaction.guild;
        // CREATE TEST EMBED
        const serverInfoEmbed = new MessageEmbed();
        serverInfoEmbed.setColor('#36393F');
        serverInfoEmbed.setAuthor('Fyce Bot - /serverinfo', interaction.user.avatarURL(), 'https://github./ttommie/fyce-bot/');
        serverInfoEmbed.setTitle('Server Information');
        serverInfoEmbed.setThumbnail(guild.iconURL());
        serverInfoEmbed.addFields(
            { name: 'Name', value: `${guild.name}`, inline: true },
            { name: '\u200B', value: '\u200B', inline: true },
            { name: 'Owner', value: `<@${guild.ownerId}>`, inline: true },
            { name: 'Total Members', value: `${guild.memberCount}`, inline: true },
            { name: 'Users Count', value: `${guild.members.cache.filter(member => !member.user.bot).size}`, inline: true },
            { name: 'Bots Count', value: `${guild.members.cache.filter(member => member.user.bot).size}`, inline: true },
            { name: 'Text Channels', value: `${guild.channels.cache.filter(channels => channels.type === 'text').size}`, inline: true }, // PROBLEM HERE 
            { name: 'Voice Channels', value: `${guild.channels.cache.filter(c => c.type === 'voice').size}`, inline: true }, // PROBLEM HERE 
            { name: 'Roles Count', value: `${guild.roles.cache.size}`, inline: true },
        );
        serverInfoEmbed.setFooter(`${guild.name} - Date Created`);
        serverInfoEmbed.setTimestamp(`${guild.createdAt.toUTCString().substr(0, 16)}`);

        await interaction.reply({ embeds: [serverInfoEmbed] });
    },
});
Share Improve this question edited Sep 2, 2021 at 12:17 MrMythical 9,0393 gold badges21 silver badges48 bronze badges asked Sep 2, 2021 at 1:53 TommieTommie 511 silver badge5 bronze badges 1
  • 1 You do not have to write "[SOLVED]" in your title. Accepting an answer signals everyone that your question has been successfully answered. – MrMythical Commented Sep 2, 2021 at 12:18
Add a ment  | 

3 Answers 3

Reset to default 3

Discord.js v13 changed the possible values of Channel.type.

Here is how you change it

//text channel filter
- guild.channels.cache.filter(c => c.type === 'text')
+ guild.channels.cache.filter(c => c.type === 'GUILD_TEXT')

//vc filter
- guild.channels.cache.filter(c => c.type === 'voice')
+ guild.channels.cache.filter(c => c.type === 'GUILD_VOICE')

//category filter
- guild.channels.cache.filter(c => c.type === 'category')
+ guild.channels.cache.filter(c => c.type === 'GUILD_CATEGORY')

Replace whatever is preceded with a - with the text below which is preceded with a +

Discord.js v14 updated the way you reference Channel.type

Here is how you change it

// import Channel Types
+ const { ChannelType } = require('discord.js');

//text channel filter
- guild.channels.cache.filter(c => c.type === 'GUILD_TEXT')
+ guild.channels.cache.filter(c => c.type === ChannelType.GuildText)

//vc filter
- guild.channels.cache.filter(c => c.type === 'GUILD_VOICE')
+ guild.channels.cache.filter(c => c.type === ChannelType.GuildVoice)

//category filter
- guild.channels.cache.filter(c => c.type === 'GUILD_CATEGORY')
+ guild.channels.cache.filter(c => c.type === ChannelType.GuildCategory)

Replace whatever is preceded with a - with the text below which is preceded with a +

I tried Tommie's way but it didn't work, so I find this on Dev's Portal and it worked:

Use the integer values to filter the type.

I did like this to filter text channels:

guild.channels.cache.filter(c => c.type === 0)

Hope it helps someone!

(https://discord./developers/docs/resources/channel#channel-object-channel-types)

发布评论

评论列表(0)

  1. 暂无评论