首页 > 解决方案 > 我似乎无法获取用户状态

问题描述

const { Discord, MessageEmbed } = require("discord.js");

const user =
    message.mentions.users.first() ||
    message.author || client.users.cache.get((u) => u.id === args[0]);
const avatar = user.displayAvatarURL();
const member = message.guild.members.cache.get(user.id);
const status = user.presence.status;
const clientStatus = user.presence.clientStatus;

const embed = new MessageEmbed()
    .setTitle(`This is ${user.username}`)
    .setColor("RANDOM")
    .setDescription("Users info:")
    .setFooter(`${user.id}`, avatar)
    .setThumbnail(avatar)
    .setTimestamp(Date.now())
    .addFields(
        { name: "User tag", value: `${user.tag}` },
        { name: "nickname", value: `${member.nickname}` || "none" },
        { name: "joined Discord", value: `${user.createdAt}` },
        { name: "joined Server", value: `${member.joinedAt}` },
        { name: "Roles", value: `${member.roles.cache.size - 1}` },
        { name: "status", value: `${status}` },
        { name: "Device", value: `${clientStatus}` }
    );

message.channel.send({ embeds: [embed] });

错误:

const status = user.presence.status;
                             ^
TypeError: Cannot read property 'status' of undefined

标签: javascriptdiscord.js

解决方案


确保您GUILD_PRESENCES启用了意图。另请注意,存在意图是特权,因此您需要在 Discord 开发者门户中允许它。

在此处输入图像描述

TypeError:无法读取未定义的属性“状态”

User没有属性.presence,使用GuildMember.presence代替。

const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_PRESENCES"] });

client.on("messageCreate", (message) => {
    if (message.content == "!status") {
        const status = message.member?.presence?.status;
        if (!status) {
            message.channel.send("Couldn't read your status!");
            return;
        }
        message.channel.send(status);
    }
});

在此处输入图像描述

要将此示例应用于您的代码,请更改以下内容:

// ... member defined ...
const status = member?.presence?.status;
const clientStatus = member?.presence?.clientStatus;

使用 discord.js ^13.0.1


推荐阅读