示例#1
0
 public IReadOnlyList <GuildMember> GetChannelMembers(ulong channelId, MemberListQueryOptions options = null)
 {
     return(((DiscordSocketClient)Client).GetGuildChannelMembers(Id, channelId, options));
 }
示例#2
0
        public static Task <IReadOnlyList <GuildMember> > GetGuildChannelMembersAsync(this DiscordSocketClient client, ulong guildId, ulong channelId, MemberListQueryOptions options = null)
        {
            if (options == null)
            {
                options = new MemberListQueryOptions();
            }

            Dictionary <int, GuildMember> memberDict = new Dictionary <int, GuildMember>(); // might as well be a List right now, but this makes it easier to add more operations later
            TaskCompletionSource <IReadOnlyList <GuildMember> > task = new TaskCompletionSource <IReadOnlyList <GuildMember> >();
            int pendingRequests = 0;

            void handler(DiscordSocketClient c, GuildMemberListEventArgs args)
            {
                if (args.GuildId == guildId)
                {
                    int combined = 0;
                    foreach (var grp in args.Groups)
                    {
                        combined += grp.Count;
                    }

                    foreach (var op in args.Operations)
                    {
                        if (op["op"].ToString() == "SYNC")
                        {
                            List <GuildMember> newMembers = new List <GuildMember>();

                            foreach (var item in op["items"])
                            {
                                JToken obj = item["member"];

                                if (obj != null)
                                {
                                    newMembers.Add(obj.ToObject <GuildMember>());
                                }
                            }

                            if (newMembers.Count > 0)
                            {
                                int[] range = op["range"].ToObject <int[]>();

                                for (int i = 0; i < newMembers.Count; i++)
                                {
                                    memberDict[i + range[0]] = newMembers[i];
                                }

                                pendingRequests--;
                            }
                        }
                    }

                    if ((memberDict.Count >= options.Count && options.Count > 0) || memberDict.OrderBy(i => i.Key).Last().Key + 1 >= combined)
                    {
                        client.OnMemberListUpdate -= handler;

                        IEnumerable <GuildMember> result = memberDict.Select(i => i.Value);

                        if (options.Count > 0)
                        {
                            result = result.Take(options.Count);
                        }

                        foreach (var member in result)
                        {
                            member.GuildId = guildId;
                        }

                        task.SetResult(result.ToList().SetClientsInList(client));
                    }
                    else if (pendingRequests == 0)
                    {
                        pendingRequests = RequestMembers(client, guildId, channelId, memberDict.OrderBy(i => i.Key).Last().Key);
                    }
                }
            }

            client.OnMemberListUpdate += handler;

            pendingRequests = RequestMembers(client, guildId, channelId, options.Offset);

            return(task.Task);
        }
示例#3
0
 /// <summary>
 /// Warning: this does not work for official guilds
 /// </summary>
 public static IReadOnlyList <GuildMember> GetGuildChannelMembers(this DiscordSocketClient client, ulong guildId, ulong channelId, MemberListQueryOptions options = null)
 {
     return(client.GetGuildChannelMembersAsync(guildId, channelId, options).GetAwaiter().GetResult());
 }
示例#4
0
        /// <summary>
        /// Warning: this does not work for official guilds
        /// </summary>
        public static IReadOnlyList <GuildMember> GetGuildChannelMembers(this DiscordSocketClient client, ulong guildId, ulong channelId, MemberListQueryOptions options = null)
        {
            if (options == null)
            {
                options = new MemberListQueryOptions();
            }

            Dictionary <int, GuildMember> memberDict = new Dictionary <int, GuildMember>(); // might as well be a List right now, but this makes it easier to add more operations later
            bool done            = false;
            int  pendingRequests = 0;

            void handler(DiscordSocketClient c, GuildMemberListEventArgs args)
            {
                if (args.GuildId == guildId)
                {
                    int combined = 0;
                    foreach (var grp in args.Groups)
                    {
                        combined += grp.Count;
                    }

                    foreach (var op in args.Operations)
                    {
                        if (op["op"].ToString() == "SYNC")
                        {
                            List <GuildMember> newMembers = new List <GuildMember>();

                            foreach (var item in op["items"])
                            {
                                JToken obj = item["member"];

                                if (obj != null)
                                {
                                    newMembers.Add(obj.ToObject <GuildMember>());
                                }
                            }

                            if (newMembers.Count > 0)
                            {
                                int[] range = op["range"].ToObject <int[]>();

                                for (int i = 0; i < newMembers.Count; i++)
                                {
                                    memberDict[i + range[0]] = newMembers[i];
                                }

                                pendingRequests--;

                                if ((memberDict.Count >= options.Count && options.Count > 0) || memberDict.OrderBy(i => i.Key).Last().Key + 1 >= combined)
                                {
                                    done = true;
                                }
                                else if (pendingRequests == 0)
                                {
                                    pendingRequests = RequestMembers(client, guildId, channelId, memberDict.OrderBy(i => i.Key).Last().Key);
                                }
                            }
                        }
                    }
                }
            }

            client.OnMemberListUpdate += handler;

            pendingRequests = RequestMembers(client, guildId, channelId, options.Index);

            while (!done && client.LoggedIn)
            {
                Thread.Sleep(10);
            }

            client.OnMemberListUpdate -= handler;

            IEnumerable <GuildMember> members = memberDict.Select(i => i.Value);

            if (options.Count > 0)
            {
                members = members.Take(options.Count);
            }

            return(members.ToList());
        }