Esempio n. 1
0
 public static IAsyncEnumerable <IReadOnlyCollection <RestGuildUser> > GetUsersAsync(IGuild guild, BaseDiscordClient client,
                                                                                     ulong?fromUserId, int?limit, RequestOptions options)
 {
     return(new PagedAsyncEnumerable <RestGuildUser>(
                DiscordConfig.MaxMessagesPerBatch,
                async(info, ct) =>
     {
         var args = new GetGuildMembersParams
         {
             Limit = info.PageSize
         };
         if (info.Position != null)
         {
             args.AfterUserId = info.Position.Value;
         }
         var models = await client.ApiClient.GetGuildMembersAsync(guild.Id, args, options);
         return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray();
     },
                nextPage: (info, lastPage) =>
     {
         if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
         {
             return false;
         }
         info.Position = lastPage.Max(x => x.Id);
         return true;
     },
                start: fromUserId,
                count: limit
                ));
 }
Esempio n. 2
0
        //Users
        public static async Task <RestGuildUser> AddGuildUserAsync(IGuild guild, BaseDiscordClient client, ulong userId, string accessToken,
                                                                   Action <AddGuildUserProperties> func, RequestOptions options)
        {
            var args = new AddGuildUserProperties();

            func?.Invoke(args);

            if (args.Roles.IsSpecified)
            {
                var ids = args.Roles.Value.Select(r => r.Id);

                if (args.RoleIds.IsSpecified)
                {
                    args.RoleIds.Value.Concat(ids);
                }
                else
                {
                    args.RoleIds = Optional.Create(ids);
                }
            }
            var apiArgs = new AddGuildMemberParams
            {
                AccessToken = accessToken,
                Nickname    = args.Nickname,
                IsDeafened  = args.Deaf,
                IsMuted     = args.Mute,
                RoleIds     = args.RoleIds.IsSpecified ? args.RoleIds.Value.Distinct().ToArray() : Optional.Create <ulong[]>()
            };

            var model = await client.ApiClient.AddGuildMemberAsync(guild.Id, userId, apiArgs, options);

            return(model is null ? null : RestGuildUser.Create(client, guild, model));
        }
Esempio n. 3
0
        //Users
        public static async Task <RestGuildUser> GetUserAsync(IGuild guild, BaseDiscordClient client,
                                                              ulong id, RequestOptions options)
        {
            var model = await client.ApiClient.GetGuildMemberAsync(guild.Id, id, options).ConfigureAwait(false);

            if (model != null)
            {
                return(RestGuildUser.Create(client, guild, model));
            }
            return(null);
        }
Esempio n. 4
0
        public static async Task <RestGuildUser> GetGuildUserAsync(BaseDiscordClient client,
                                                                   ulong guildId, ulong id)
        {
            var model = await client.ApiClient.GetGuildMemberAsync(guildId, id).ConfigureAwait(false);

            if (model != null)
            {
                return(RestGuildUser.Create(client, new RestGuild(client, guildId), model));
            }
            return(null);
        }
Esempio n. 5
0
        public static async Task <IReadOnlyCollection <RestGuildUser> > SearchUsersAsync(IGuild guild, BaseDiscordClient client,
                                                                                         string query, int?limit, RequestOptions options)
        {
            var apiArgs = new SearchGuildMembersParams
            {
                Query = query,
                Limit = limit ?? Optional.Create <int>()
            };
            var models = await client.ApiClient.SearchGuildMembersAsync(guild.Id, apiArgs, options).ConfigureAwait(false);

            return(models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray());
        }
Esempio n. 6
0
        //Users
        /// <exception cref="InvalidOperationException">Resolving permissions requires the parent guild to be downloaded.</exception>
        public static async Task<RestGuildUser> GetUserAsync(IGuildChannel channel, IGuild guild, BaseDiscordClient client,
            ulong id, RequestOptions options)
        {
            var model = await client.ApiClient.GetGuildMemberAsync(channel.GuildId, id, options).ConfigureAwait(false);
            if (model == null)
                return null;
            var user = RestGuildUser.Create(client, guild, model);
            if (!user.GetPermissions(channel).ViewChannel)
                return null;

            return user;
        }
Esempio n. 7
0
 internal static RestUser Create(BaseDiscordClient discord, IGuild guild, EventUserModel model)
 {
     if (model.Member.IsSpecified)
     {
         var member = model.Member.Value;
         member.User = model.User;
         return(RestGuildUser.Create(discord, guild, member));
     }
     else
     {
         return(RestUser.Create(discord, model.User));
     }
 }
Esempio n. 8
0
        public static async Task <RestGuildUser> GetGuildUserAsync(BaseDiscordClient client,
                                                                   ulong guildId, ulong id, RequestOptions options)
        {
            RestGuild guild = await GetGuildAsync(client, guildId, false, options).ConfigureAwait(false);

            if (guild == null)
            {
                return(null);
            }

            API.GuildMemberJson model = await client.ApiClient.GetGuildMemberAsync(guildId, id, options).ConfigureAwait(false);

            if (model != null)
            {
                return(RestGuildUser.Create(client, guild, model));
            }

            return(null);
        }
        internal async Task PopulateAsync(DiscordRestClient discord, RestGuild guild, IRestMessageChannel channel, T model)
        {
            var resolved = model.Resolved.Value;

            if (resolved.Users.IsSpecified)
            {
                foreach (var user in resolved.Users.Value)
                {
                    var restUser = RestUser.Create(discord, user.Value);

                    Users.Add(ulong.Parse(user.Key), restUser);
                }
            }

            if (resolved.Channels.IsSpecified)
            {
                var channels = await guild.GetChannelsAsync().ConfigureAwait(false);

                foreach (var channelModel in resolved.Channels.Value)
                {
                    var restChannel = channels.FirstOrDefault(x => x.Id == channelModel.Value.Id);

                    restChannel.Update(channelModel.Value);

                    Channels.Add(ulong.Parse(channelModel.Key), restChannel);
                }
            }

            if (resolved.Members.IsSpecified)
            {
                foreach (var member in resolved.Members.Value)
                {
                    // pull the adjacent user model
                    member.Value.User = resolved.Users.Value.FirstOrDefault(x => x.Key == member.Key).Value;
                    var restMember = RestGuildUser.Create(discord, guild, member.Value);

                    GuildMembers.Add(ulong.Parse(member.Key), restMember);
                }
            }

            if (resolved.Roles.IsSpecified)
            {
                foreach (var role in resolved.Roles.Value)
                {
                    var restRole = RestRole.Create(discord, guild, role.Value);

                    Roles.Add(ulong.Parse(role.Key), restRole);
                }
            }

            if (resolved.Messages.IsSpecified)
            {
                foreach (var msg in resolved.Messages.Value)
                {
                    channel ??= (IRestMessageChannel)(Channels.FirstOrDefault(x => x.Key == msg.Value.ChannelId).Value ?? await discord.GetChannelAsync(msg.Value.ChannelId).ConfigureAwait(false));

                    RestUser author;

                    if (msg.Value.Author.IsSpecified)
                    {
                        author = RestUser.Create(discord, msg.Value.Author.Value);
                    }
                    else
                    {
                        author = RestGuildUser.Create(discord, guild, msg.Value.Member.Value);
                    }

                    var message = RestMessage.Create(discord, channel, author, msg.Value);

                    Messages.Add(message.Id, message);
                }
            }

            if (resolved.Attachments.IsSpecified)
            {
                foreach (var attachment in resolved.Attachments.Value)
                {
                    var discordAttachment = Attachment.Create(attachment.Value);

                    Attachments.Add(ulong.Parse(attachment.Key), discordAttachment);
                }
            }
        }