Esempio n. 1
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. 2
0
 public static IAsyncEnumerable <IReadOnlyCollection <RestGuildUser> > GetUsersAsync(IGuildChannel channel, IGuild guild, BaseDiscordClient client,
                                                                                     ulong?fromUserId, int?limit, RequestOptions options)
 {
     return(new PagedAsyncEnumerable <RestGuildUser>(
                DiscordConfig.MaxUsersPerBatch,
                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).ConfigureAwait(false);
         return models
         .Select(x => RestGuildUser.Create(client, guild, x))
         .Where(x => x.GetPermissions(channel).ReadMessages)
         .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. 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
        //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. 6
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. 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);
                }
            }
        }
Esempio n. 10
0
 /// <inheritdoc />
 public Task RemoveRoleAsync(IRole role, RequestOptions options = null)
 => RestGuildUser.RemoveRoleAsync(role, options);
Esempio n. 11
0
 /// <inheritdoc />
 public ChannelPermissions GetPermissions(IGuildChannel channel)
 => RestGuildUser.GetPermissions(channel);
Esempio n. 12
0
 /// <inheritdoc />
 public Task ModifyAsync(Action <GuildUserProperties> func, RequestOptions options = null)
 => RestGuildUser.ModifyAsync(func, options);
Esempio n. 13
0
 /// <inheritdoc />
 public Task AddRoleAsync(ulong roleId, RequestOptions options = null)
 => RestGuildUser.AddRoleAsync(roleId, options);
Esempio n. 14
0
 /// <inheritdoc />
 public Task AddRoleAsync(IRole role, RequestOptions options = null)
 => RestGuildUser.AddRoleAsync(role);
Esempio n. 15
0
 /// <summary>
 /// Constructs a new <see cref="RestGuildUserAbstraction"/> around an existing <see cref="Rest.RestGuildUser"/>.
 /// </summary>
 /// <param name="restGuildUser">The value to use for <see cref="Rest.RestGuildUser"/>.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="restGuildUser"/>.</exception>
 public RestGuildUserAbstraction(RestGuildUser restGuildUser)
     : base(restGuildUser)
 {
 }
Esempio n. 16
0
 /// <summary>
 /// Converts an existing <see cref="RestGuildUser"/> to an abstracted <see cref="IRestGuildUser"/> value.
 /// </summary>
 /// <param name="restGuildUser">The existing <see cref="RestGuildUser"/> to be abstracted.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="restGuildUser"/>.</exception>
 /// <returns>An <see cref="IRestGuildUser"/> that abstracts <paramref name="restGuildUser"/>.</returns>
 public static IRestGuildUser Abstract(this RestGuildUser restGuildUser)
 => new RestGuildUserAbstraction(restGuildUser);
Esempio n. 17
0
 /// <inheritdoc />
 public Task RemoveRolesAsync(IEnumerable <IRole> roles, RequestOptions options = null)
 => RestGuildUser.RemoveRolesAsync(roles, options);
Esempio n. 18
0
 /// <inheritdoc />
 public Task KickAsync(string reason = null, RequestOptions options = null)
 => RestGuildUser.KickAsync(reason, options);
Esempio n. 19
0
 /// <inheritdoc />
 public Task AddRolesAsync(IEnumerable <IRole> roles, RequestOptions options = null)
 => RestGuildUser.AddRolesAsync(roles);
Esempio n. 20
0
 /// <inheritdoc />
 public Task AddRolesAsync(IEnumerable <ulong> roleIds, RequestOptions options = null)
 => RestGuildUser.AddRolesAsync(roleIds, options);