discord.net get every users activity

Viewed 47

I'm writing a bot, which should get a list of all users on a server, figure out if they are playing a game and displaying the user and the game in an embed for now. My problem is, that from everything I found there seems to be a way. But there is no thing like "user.Activity" or "user.Game.Name" available. Is there a way to do it?

My code:

        [Command("playing")]
        [Alias("p")]
        public async Task GetUsersPlaying()
        {
            var users = Context.Guild.Users;

            var embed = new EmbedBuilder { };

            foreach (IGuildUser user in users)
            {
                if (user.Activity is CustomStatusGame statusGame)
                {
                    embed.AddField("User", user, false);
                    embed.AddField("Game", user.Game.Name, true);
                }
            }
            await ReplyAsync(embed: embed.Build());
        }
2 Answers

You are looking for user.Activities.

Surprisingly discord stores multiple activities in a user, despite the fact users are only displayed as having one activity.

        foreach (IGuildUser user in users) {
            var userActivities = user.Activities;

            foreach (var activity in userActivities) {
                // If this activity is a game
                if (activity.Type == ActivityType.Playing) {
                    // embed
                }
            }
        }

EDIT

When you create your DiscordSocketClient object, you need to configure to read activities.

_client = new DiscordSocketClient(new DiscordSocketConfig {
                        GatewayIntents = GatewayIntents.AllUnprivileged |
                    GatewayIntents.GuildPresences | GatewayIntents.GuildMembers
                    });

I fixed the problem. I just needed to activate the corresponding intents in the discord developer portal.

Related