首页 > 解决方案 > Discord - 显示具有指定编号的成员加入位置

问题描述

我想告诉你我在参数中输入的用户数量是加入服务器的顺序。喜欢 = 当我使用时,.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

这个用于显示接合位置的命令,但我很困惑,我该怎么做?

标签: javascriptdiscorddiscord.js

解决方案


尝试这个:

// 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'].


推荐阅读