如何通过按特定属性排序进行映射

时间:2019-07-14 00:50:47

标签: javascript discord.js commando

我想在我的机器人上创建一个功能... -从类别和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命令,该命令从非文本通道中分离#,并按位置映射通道。 实际:机器人按字母顺序映射频道。

1 个答案:

答案 0 :(得分:0)

排序

GuildChannel.positionGuildChannel.calculatedPosition的问题在于返回的头寸基于渠道的类型。例如,所有类别均按顺序排序,并给其与文本通道分开的编号,并给文本通道与语音通道分开的

为了解决这个问题,我们可以创建自己的系统,利用它来发挥我们的优势。我们首先对所有类别进行排序并将其添加到与孩子的排序Collection配对的Collection中。然后,我们进行迭代,并将频道添加到列表中。

格式化

从技术上讲,#符号应该位于非文本通道的前面,因为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文档: