简体   繁体   中英

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

How can I get the activities from interaction.options ? If I use interaction.options.getUser , I receive the following error:

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. What you can do is to fetch the member by its ID first:

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

And then, you can get the 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 :

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

Please note that you'll need to have the GUILDS , GUILD_MEMBERS , and GUILD_PRESENCES intents enabled!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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