[英]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!请注意,您需要启用
GUILDS
、 GUILD_MEMBERS
和GUILD_PRESENCES
意图!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.