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

I'm currently trying to get the total amount of text channels and voice channels to display in my

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745610762a4635933.html

相关推荐

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

    I'm currently trying to get the total amount of text channels and voice channels to display in my

    15小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信