0

我想在我的机器人中创建一个功能... - 从类别和 VC 频道中排除 # - 按频道的数字位置而不是字母顺序对频道进行排序

问题是我不知道如何根据类型从频道中拆分 #。我也想不出一种按位置编号对频道进行排序的方法。

我试过这个 .addField("Server's channels", serv.channels.calculatedPosition.map(c =>${c}).join(' | '),true)

.addField("Server's channels", serv.channels.map(c =>${c}).position.join(' | '),true)

var serv = message.guild

    var myInfo = new discord.RichEmbed()
        .setAuthor(`${serv.name}'s channels`,`${message.guild.iconURL}`)
        .addField(`Server's channels`, serv.channels.map(c => `${c}`).join(' | '),true)
        .setColor(0xffd000)
        .setFooter('Server Roles.')
        .setThumbnail(`${message.guild.iconURL}`)
        message.channel.sendEmbed(myInfo);

期望:discord.js-commando 命令将 # 从不是文本的通道中拆分出来,并按位置映射通道。实际:机器人按字母顺序映射频道。

4

1 回答 1

0

排序

GuildChannel.position和的问题GuildChannel.calculatedPosition是返回的位置是基于通道的类型。例如,所有类别都被排序并与文本通道分开分配编号,文本通道与语音通道分开。

为了解决这个问题,我们可以制作我们自己的系统来利用它来发挥我们的优势。我们首先对所有类别进行排序并将其添加到与其子项的排序集合配对的集合中。然后,我们遍历并将通道添加到列表中。

格式化

从技术上讲,该#符号应该位于非文本频道的前面,因为 Discord 会将他们的提及转换为使用它。但是,它看起来不是很吸引人,逻辑似乎有点缺陷。

如果它不是文本频道,我们所要做的就是使用频道的名称而不是提及它。

编码

您可能需要更改一些变量,并根据需要实现嵌入。这只是一个例子。

const guild = message.guild;

// Comparison function which sorts channels according to appearance within Discord. Name
// is short for 'descending position,' but it also accomodates for voice channel location.
const descPos = (a, b) => {
  if (a.type !== b.type) {
    if (a.type === 'voice') return 1;
    else return -1;
  } else return a.position - b.position;
};

// Create a new Collection to hold categories and their children.
const channels = new Discord.Collection();

// Non-category channels without parent categories will appear at the top.
channels.set('__none', guild.channels.filter(channel => !channel.parent && channel.type !== 'category').sort(descPos));

// Add all the categories in order, mapped by their bolded name, into the Collection.
const categories = guild.channels.filter(channel => channel.type === 'category').sort(descPos);
categories.forEach(category => channels.set(category.id, category.children.sort(descPos)));

const list = [];

// Iterate through the categories and the corresponding Collection of their channels.
for (let [categoryID, children] of channels) {
  // Retrieve the category from it's ID.
  const category = guild.channels.get(categoryID);

  // Push the category name (bolded for readability) into the list.
  if (category) list.push(`**${category.name}**`);

  // Iterate through the Collection of children. Push the mention for text, name for others.
  for (let [, child] of children) list.push(child.type === 'text' ? child : child.name);
  // To answer your comment about adding the emoji for voice channels...
  //                              list.push(child.type === 'text' ? child : ` ${child.name}`);
}

// Send the list of channels, appearing exactly how it does on the side. Make sure the
// joined list isn't too long for a message or embed field first to avoid an error.
message.channel.send(list.join('\n'))
  .catch(console.error);

资源

Discord.js 文档:

于 2019-07-15T20:21:21.510 回答