简体   繁体   English

如何从 Discord.js v13 中的交互中获取用户活动?

[英]How to get user activities from interaction in Discord.js v13?

How can I get the activities from interaction.options ?如何从interaction.options获取activities If I use interaction.options.getUser , I receive the following error:如果我使用interaction.options.getUser ,我会收到以下错误:

cannot read properties of undefined (reading 'activities')

Here is my code:这是我的代码:

const user = interaction.options.getUser('target');

const activity = user.presence.activities.type;

User s don't have a presence property, only GuildMember s have. User没有presence属性,只有GuildMember有。 What you can do is to fetch the member by its ID first:您可以做的是首先通过其 ID fetch成员:

const user = interaction.options.getUser('target');
const member = await interaction.guild.members.fetch(user);

And then, you can get the activities :然后,您可以获得activities

const activities = member.presence?.activities;

activities returns an array of Activity , so you'll need to grab the first one for example to log its type : activities返回一个Activity数组,因此您需要获取第一个以记录其type

console.log(activities[0]?.type)

Please note that you'll need to have the GUILDS , GUILD_MEMBERS , and GUILD_PRESENCES intents enabled!请注意,您需要启用GUILDSGUILD_MEMBERSGUILD_PRESENCES意图!

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

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