有没有一种方法可以列出不和谐服务器中的频道

时间:2020-08-22 11:48:50

标签: node.js discord.js

我使用discord.js V12,我需要的是这样;

1 = welcome_channel

2 =一般聊天等。

我也在下面使用此代码

const listedChannels = []
    message.guild.channels.cache.forEach(channel => {
      listedChannels.push(channel.name)
    })
    const channelembed = new Discord.MessageEmbed()
    .setTitle("Channel list ")
    .setDescription(listedChannels)
    .setTimestamp()
    .setColor("RANDOM")
    .setFooter("Write the number of the channel")
    message.channel.send(channelembed)

3 个答案:

答案 0 :(得分:0)

您可以通过按名称映射channels.cache集合来做到这一点。

(该过滤器是因为类别也被视为渠道。)

message.guild.channels.cache.filter(channel => channel.type == "text" || channel.type == "voice").map(channel => channel.name).join(", ");

答案 1 :(得分:0)

您可以使用:

const channels = [] // create starting array

message.guild.channels.cache // get all channels
  .filter((channel) => channel.type !== "category") // filter out the categories (they are also counted as channels)
  .forEach((channel) => channels.push(channel.name)); // add each channel name to the array

const channelembed = new Discord.MessageEmbed() // make the embed
  .setTitle("Channel List")
  .setDescription(channels.map((channel, index) => `${index + 1} - ${channel}`).join('\n')) // map the channel names to preferred format
  .setTimestamp()
  .setColor("RANDOM")
  .setFooter("Write the number of the channel");

channels的示例输出:

1 - general
2 - media
3 - bot-spam
4 - announcements

我在服务器上运行了它,这就是我得到的: Example

答案 2 :(得分:0)

@Sintuz和@Jakye我将两者结合在一起,并更改了一些代码,我做到了:

const description = []
let i = 1
message.guild.channels.cache.forEach(channel => {
  if (channel.type == "text") {
  description.push(i + "=" +channel.name + "\n")
    i++
  } else return;
})
const channelembed = new Discord.MessageEmbed()
.setTitle("Write the number of the channel")
.setDescription(description)
.setTimestamp()
.setColor("RANDOM")
.setFooter("Write the number of the channel");
message.channel.send(channelembed)

它有效!