不和谐-显示具有指定编号的成员加入位置

时间:2020-03-17 03:23:42

标签: javascript discord discord.js

我想向您展示我在参数中输入的用户数量是加入服务器的顺序。像=,当我使用.join 1时,我想显示第一个成员加入服务器。我用

let arr = message.guild.members.filter(a => !a.user.bot).array().sort((b, a) => b.joinedTimestamp - a.joinedTimestamp) 
let map = arr.indexOf(sesmi) + 1

此命令用于显示作业位置,但是我很困惑,我该怎么做?

1 个答案:

答案 0 :(得分:0)

尝试一下:

// if the first argument is not a number (this message is kind of bad so you can change it)
if (isNaN(args[0])) return message.reply('you must specify what number user you want to get!')

const members = message.guild.members.cache
  .filter(member => !member.user.bot)
  // sorted is a member on Discord's utility class Collection that doesn't modify the original collection
  .sorted((a, b) => a.joinedTimestamp - b.joinedTimestamp)
  .array()

// the number user to get
const n = Number(args[0])
// if there are not enough members
if (n > members.length) {
  // You only really need this if there is ever going to be only 1 member in the server
  // and if you care about grammar. You could also just do
  // return message.reply(`there are only ${members.length} members!`)
  const plural = members.length !== 1
  return message.reply(`there ${plural ? 'are' : 'is'} only ${members.length} member${plural ? 's' : ''}!`)
}
message.channel.send(members[n - 1].user.tag)

我假设args将是一个字符串数组,并将参数传递给命令(例如.join 1将具有args ['1']