javascript - 我似乎无法获取用户状态
问题描述
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
解决方案
确保您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
。
推荐阅读
- django - 如何使用 django 在网页上显示多个视频流?
- java - 将 JTextarea 添加到 JScrollPane。什么是可见的,第一个还是第二个?
- android - Android中的Native C - 插入其他cpp文件的问题
- python - 获取DataFrame中特定日期范围内的最小值和最大值
- python - 如何从 GCP 中的 python 脚本将文件上传到 GCS?
- swift - 带碰撞检测的 enumareteChildNodes swift
- reactjs - 单元测试不会更新样式输入的值
- testing - 如何在 CI 中测试(或运行)Intellij IDEA 运行配置?
- recursion - How is this recursive CodeQL predicate is evaluated?
- python - 为什么 BeautifulSoup 会从我的 HTML 中删除所有格式?