admin管理员组文章数量:1024729
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
3 Answers
Reset to default 3Discord.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)
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
3 Answers
Reset to default 3Discord.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)
本文标签: javascriptDiscord js v13 channel filter not workingStack Overflow
版权声明:本文标题:javascript - Discord js v13 channel filter not working - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745610760a2158988.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论