【问题标题】:Discord.js get user by IDDiscord.js 通过 ID 获取用户
【发布时间】:2021-05-01 19:55:52
【问题描述】:

您好,我有这个头像命令,我想知道您可以使用他们的 ID 代替提及用户而不是提及用户

if(command === "avatar" || command === "av") {
               const user = message.mentions.users.first() || client.users.cache.get(args[0]) || message.author
       if (!user) {
       user = message.author;
       }
      
          const member = message.guild.member(user)
          const avatar = user.avatarURL({dynamic:true})
          if (!avatar) return message.channel.send("This user doesn't have an avatar to show")
          const embed = new Discord.MessageEmbed()
             .setTitle(`${user.username}'s avatar`)
             .setColor('RANDOM')
             .setDescription(`[Click here for the image link](${avatar})`)
             .setImage(avatar)
             .setFooter(`${client.user.username} 2021`)
             .setTimestamp()
     
         message.channel.send(embed)

【问题讨论】:

  • 我看到你已经有代码来获取带有 id 的用户缓存?有什么东西坏了还是你有什么问题?
  • 当我尝试使用用户 ID 时,它只会得到消息作者 pfp

标签: node.js discord discord.js


【解决方案1】:

我不会使用client.users.cache.get(args[0]),而是推荐message.guild.members.cache.get(args[0]).user,这样它会获取一个成员(不是用户),然后从该成员中选择用户。还要确保您在开发者门户上启用了意图,因为没有它们,这将无法工作。

要启用它们,只需选择您的应用,然后转到 bot 选项卡,向下滚动一点,并确保两个 Intent 均已选中。

【讨论】:

    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 2018-09-01
    • 2021-07-30
    • 2021-12-08
    • 1970-01-01
    • 2021-05-17
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多