如何在 Discord.JS v12 中查看用户正在玩的游戏
Posted
技术标签:
【中文标题】如何在 Discord.JS v12 中查看用户正在玩的游戏【英文标题】:How to see which game a user is playing in Discord.JS v12 【发布时间】:2021-03-21 13:18:46 【问题描述】:我正在通过在线指南的帮助开发一个不和谐机器人。问题是,这些指南主要是 Discord.JS v11 指南,而我使用的是 Discord.JS v12。所以对我帮助不大。我想了解如何在 !userinfo 命令上获取用户正在玩的游戏的名称。除游戏字段外,所有字段都显示正确的内容。我已经在开发者门户中启用了显示状态功能。它总是在其中显示 None,尽管可能是因为它不是经过验证的游戏名称或 Zoom Classes(XD) 之类的东西或未经验证的东西。你能告诉我解决方法吗?代码:
case "userinfo":
commandsrun = commandsrun + 1;
let user;
if (message.mentions.users.first())
user = message.mentions.users.first();
useralso = message.mentions.users.first();
else
user = message.author;
useralso = message.member;
//i made useralso so it shows nickname correctly
const userStatus =
online: "Online",
idle: "Idle/Inactive",
dnd: "Busy/Do Not Disturb",
offline: "Unavailable/Offline"
const mememb = message.guild.member(user)
Promise.resolve(mememb).then(function ()
const memberemb = message.guild.members.fetch(user);
Promise.resolve(memberemb).then(function (info)
var roles = info.roles.cache.map(roles => `$roles`).join(', ');
roles = roles.slice(0, -9);
const embed = new Discord.MessageEmbed()
.setColor("GREEN")
.setThumbnail(message.author.avatarURL)
.addField(`$user.tag`, `$user`, true)
.addField("ID:", `$user.id`, true)
.addField("Nickname:", `$useralso.nickname ? `$useralso.nickname` : 'None'`, true)
.addField("Status:", `$userStatus[user.presence.status]`, true)
.addField("Game:", `$user.presence.game ? user.presence.game.name : 'None'`, true)
.addField("Joined The Server On:", `$moment.utc(mememb.joinedAt).format("dddd, MMMM Do YYYY")`, true)
.addField("Account Created On:", `$moment.utc(user.createdAt).format("dddd, MMMM Do YYYY")`, true)
.addField("Roles:", info.roles.cache ? roles : "None", true)
.setFooter(`Replying to $message.author.username#$message.author.discriminator`)
message.channel.send( embed );
);
);
break;
请告诉我修复并在您的答案中发送修复代码。谢谢。
【问题讨论】:
【参考方案1】:您好,您已经告诉过您正在使用 discord.js v12,您需要使用 Presence
类中的 activities
属性。这是你必须做的 -
user.presence.activities[0].name // this will output the game or any other activity
但如果用户使用自定义状态,那么您可能会得到 Name
未定义来解决这个问题,我们需要过滤数组!
var presence = user.presence.activities.filter(x=>x.type === "PLAYING") //outputs the presence which the user is playing
presence[0].name // will give the name of the game
你的最终输出应该是 -
case "userinfo":
commandsrun = commandsrun + 1;
let user;
if (message.mentions.users.first())
user = message.mentions.users.first();
useralso = message.mentions.users.first();
else
user = message.author;
useralso = message.member;
//i made useralso so it shows nickname correctly
const userStatus =
online: "Online",
idle: "Idle/Inactive",
dnd: "Busy/Do Not Disturb",
offline: "Unavailable/Offline"
const mememb = message.guild.member(user)
Promise.resolve(mememb).then(function ()
var presence = user.presence.activities.length ?user.presence.activities.filter(x=>x.type === "PLAYING") : null;
const memberemb = message.guild.members.fetch(user);
Promise.resolve(memberemb).then(function (info)
var roles = info.roles.cache.map(roles => `$roles`).join(', ');
roles = roles.slice(0, -9);
const embed = new Discord.MessageEmbed()
.setColor("GREEN")
.setThumbnail(message.author.avatarURL)
.addField(`$user.tag`, `$user`, true)
.addField("ID:", `$user.id`, true)
.addField("Nickname:", `$useralso.nickname ? `$useralso.nickname` : 'None'`, true)
.addField("Status:", `$userStatus[user.presence.status]`, true)
.addField("Game:", `$presence && presence.length ? presence[0].name : 'None'`, true)
.addField("Joined The Server On:", `$moment.utc(mememb.joinedAt).format("dddd, MMMM Do YYYY")`, true)
.addField("Account Created On:", `$moment.utc(user.createdAt).format("dddd, MMMM Do YYYY")`, true)
.addField("Roles:", info.roles.cache ? roles : "None", true)
.setFooter(`Replying to $message.author.username#$message.author.discriminator`)
message.channel.send( embed );
);
);
break;
【讨论】:
如果您收到错误Cannot read property 'filter' of undefined
,这是由于最近的网关更改,请考虑阅读此内容 - discordjs.guide/popular-topics/intents.html以上是关于如何在 Discord.JS v12 中查看用户正在玩的游戏的主要内容,如果未能解决你的问题,请参考以下文章
如何将服务器中每个用户的 id 放入数组中? (Discord.js v12)
如何检查用户是不是具有特定角色 discord.js v12? [复制]