简体   繁体   English

如何获取用户活动 discord.js

[英]How do I get the users activity discord.js

Problem: I have been trying to add a "Activity" field to my info command, and its not working.问题:我一直在尝试将“活动”字段添加到我的信息命令中,但它不起作用。

Expected result: Responds with the other stuff, along with the Activity field, showing what the user is doing.预期结果:响应其他内容以及 Activity 字段,显示用户正在做什么。

Actual response: Shows everything else correctly, except the Activity field, which says undefined / null .实际响应:正确显示其他所有内容,但 Activity 字段除外,该字段显示undefined / null

Code:代码:

if (command == "info") {
  const embed = new Discord.MessageEmbed()
  let member = message.mentions.members.first() || message.guild.members.cache.get(args[1]) || message.guild.member(message.author)
    const Roles = new Array()
    message.guild.roles.cache.forEach(role => {
      if (member.roles.cache.find(r => r.id == role.id)) {
        Roles.push(role)
      }
        })
    console.log(member.presence)
  embed.setAuthor(user.tag, member.user.avatarURL())
  embed.setTitle(`👋 Welcome to ${user.username}'s Profile!`)
  embed.addField('#️⃣ Tag:', member.user.tag)
  embed.addField('📡 Joined Guild at:', member.user.joinedAt)
  embed.addField('💬 Joined Discord at:', member.user.cretedAt
)
if (!member.presence.activities) {
  embed.addField('⚽️ Activity:', 'Not playing anything')
} else {
  embed.addField('⚽️ Activity:', `${member.presence.activities.type} ${member.presence.activities.name}\n${member.presence.activities.details}\n${member.presence.activities.state}`)
}
  embed.addField('📱 Platform:', member.presence.clientStatus)
  embed.addField('🤖 Bot:', user.bot)
  embed.addField('📜 Roles:', Roles.join(' | '))
  embed.setFooter('Bot made with discord.js')
  message.channel.send(embed)
  }

Presence.activities is an array of Activity . Presence.activities是一个Activity数组。

// Add check for empty activities array
if (!member.presence.activities || member.presence.activities.length == 0) {
    embed.addField('⚽️ Activity:', 'Not playing anything')
} else {
    const activity = member.presence.activities[0];
    embed.addField('⚽️ Activity:', `${activity.type} ${activity.name}\n${activity.details}\n${activity.state}`);
}

Most of the time you will only need the first item of the array.大多数情况下,您只需要数组的第一项。 Because you rarely see users with multiple activities.因为你很少看到用户有多个活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM