/// <inheritdoc />
        public virtual async Task <IModifyRestEntityResult <IWebhook> > ModifyWebhookWithTokenAsync
        (
            Snowflake webhookID,
            string token,
            Optional <string> name    = default,
            Optional <Stream?> avatar = default,
            CancellationToken ct      = default
        )
        {
            var packAvatar = await ImagePacker.PackImageAsync(avatar, ct);

            if (!packAvatar.IsSuccess)
            {
                return(ModifyRestEntityResult <IWebhook> .FromError(packAvatar));
            }

            var avatarData = packAvatar.Entity;

            return(await _discordHttpClient.PatchAsync <IWebhook>
                   (
                       $"webhooks/{webhookID}/{token}",
                       b => b.WithJson
                       (
                           json =>
            {
                json.Write("name", name, _jsonOptions);
                json.Write("avatar", avatarData, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }
Beispiel #2
0
        /// <inheritdoc />
        public virtual async Task <IModifyRestEntityResult <IUser> > ModifyCurrentUserAsync
        (
            Optional <string> username,
            Optional <Stream?> avatar = default,
            CancellationToken ct      = default
        )
        {
            var packAvatar = await ImagePacker.PackImageAsync(avatar, ct);

            if (!packAvatar.IsSuccess)
            {
                return(ModifyRestEntityResult <IUser> .FromError(packAvatar));
            }

            var avatarData = packAvatar.Entity;

            return(await _discordHttpClient.PatchAsync <IUser>
                   (
                       "users/@me",
                       b => b.WithJson
                       (
                           json =>
            {
                json.Write("username", username, _jsonOptions);
                json.Write("avatar", avatarData, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }
Beispiel #3
0
        /// <inheritdoc />
        public virtual async Task <IModifyRestEntityResult <IChannel> > ModifyChannelAsync
        (
            Snowflake channelID,
            Optional <string> name           = default,
            Optional <ChannelType> type      = default,
            Optional <int?> position         = default,
            Optional <string?> topic         = default,
            Optional <bool?> isNsfw          = default,
            Optional <int?> rateLimitPerUser = default,
            Optional <int?> bitrate          = default,
            Optional <int?> userLimit        = default,
            Optional <IReadOnlyList <IPermissionOverwrite>?> permissionOverwrites = default,
            Optional <Snowflake?> parentId = default,
            CancellationToken ct           = default
        )
        {
            if (name.HasValue && (name.Value !.Length > 100 || name.Value !.Length < 2))
            {
                return(ModifyRestEntityResult <IChannel> .FromError("The name must be between 2 and 100 characters."));
            }

            if (topic.HasValue && topic.Value is not null && (topic.Value.Length > 1024 || topic.Value.Length < 0))
            {
                return(ModifyRestEntityResult <IChannel> .FromError("The topic must be between 0 and 1024 characters."));
            }

            if (userLimit.HasValue && userLimit.Value is not null && (userLimit.Value > 99 || userLimit.Value < 0))
            {
                return(ModifyRestEntityResult <IChannel> .FromError("The user limit must be between 0 and 99."));
            }

            return(await _discordHttpClient.PatchAsync <IChannel>
                   (
                       $"channels/{channelID}",
                       b => b.WithJson
                       (
                           json =>
            {
                json.Write("name", name, _jsonOptions);
                json.WriteEnum("type", type, jsonOptions: _jsonOptions);
                json.Write("position", position, _jsonOptions);
                json.Write("topic", topic, _jsonOptions);
                json.Write("nsfw", isNsfw, _jsonOptions);
                json.Write("rate_limit_per_user", rateLimitPerUser, _jsonOptions);
                json.Write("bitrate", bitrate, _jsonOptions);
                json.Write("user_limit", userLimit, _jsonOptions);
                json.Write("permission_overwrites", permissionOverwrites, _jsonOptions);
                json.Write("parent_id", parentId, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }
Beispiel #4
0
        /// <inheritdoc />
        public async Task <IModifyRestEntityResult <IApplicationCommand> > EditGuildApplicationCommandAsync
        (
            Snowflake applicationID,
            Snowflake guildID,
            Snowflake commandID,
            Optional <string> name,
            Optional <string> description,
            Optional <IReadOnlyList <IApplicationCommandOption>?> options,
            CancellationToken ct
        )
        {
            if (name.HasValue && name.Value !.Length is < 3 or > 32)
            {
                return(ModifyRestEntityResult <IApplicationCommand> .FromError
                       (
                           "The name must be between 3 and 32 characters."
                       ));
            }

            if (description.HasValue && description.Value !.Length is < 1 or > 100)
            {
                return(ModifyRestEntityResult <IApplicationCommand> .FromError
                       (
                           "The description must be between 1 and 100 characters."
                       ));
            }

            return(await _discordHttpClient.PatchAsync <IApplicationCommand>
                   (
                       $"applications/{applicationID}/guilds/{guildID}/commands/{commandID}",
                       b => b.WithJson
                       (
                           json =>
            {
                json.Write("name", name, _jsonOptions);
                json.Write("description", description, _jsonOptions);
                json.Write("options", options, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }
Beispiel #5
0
        /// <summary>
        /// Performs a PATCH request to the Discord REST API at the given endpoint.
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        /// <param name="configureRequestBuilder">The request builder for the request.</param>
        /// <param name="allowNullReturn">Whether to allow null return values inside the creation result.</param>
        /// <param name="ct">The cancellation token for this operation.</param>
        /// <typeparam name="TEntity">The entity type to modify.</typeparam>
        /// <returns>A modification result which may or may not have succeeded.</returns>
        public async Task <IModifyRestEntityResult <TEntity> > PatchAsync <TEntity>
        (
            string endpoint,
            Action <RestRequestBuilder>?configureRequestBuilder = null,
            bool allowNullReturn = false,
            CancellationToken ct = default
        )
        {
            configureRequestBuilder ??= _ => { };

            var requestBuilder = new RestRequestBuilder(endpoint);

            configureRequestBuilder(requestBuilder);

            requestBuilder.WithMethod(HttpMethod.Patch);

            try
            {
                using var request  = requestBuilder.Build();
                using var response = await _httpClient.SendAsync
                                     (
                          request,
                          HttpCompletionOption.ResponseHeadersRead,
                          ct
                                     );

                var entityResult = await UnpackResponseAsync <TEntity>(response, allowNullReturn, ct);

                return(!entityResult.IsSuccess
                    ? ModifyRestEntityResult <TEntity> .FromError(entityResult)
                    : ModifyRestEntityResult <TEntity> .FromSuccess(entityResult.Entity));
            }
            catch (Exception e)
            {
                return(ModifyRestEntityResult <TEntity> .FromError(e));
            }
        }
        /// <inheritdoc />
        public virtual async Task <IModifyRestEntityResult <IGuild> > ModifyGuildAsync
        (
            Snowflake guildID,
            Optional <string> name    = default,
            Optional <string?> region = default,
            Optional <VerificationLevel?> verificationLevel = default,
            Optional <MessageNotificationLevel?> defaultMessageNotifications = default,
            Optional <ExplicitContentFilterLevel?> explicitContentFilter     = default,
            Optional <Snowflake?> afkChannelID           = default,
            Optional <TimeSpan> afkTimeout               = default,
            Optional <Stream?> icon                      = default,
            Optional <Snowflake> ownerID                 = default,
            Optional <Stream?> splash                    = default,
            Optional <Stream?> banner                    = default,
            Optional <Snowflake?> systemChannelID        = default,
            Optional <Snowflake?> rulesChannelID         = default,
            Optional <Snowflake?> publicUpdatesChannelID = default,
            Optional <string?> preferredLocale           = default,
            CancellationToken ct = default
        )
        {
            await using var memoryStream = new MemoryStream();

            Optional <string?> iconData = default;

            if (icon.HasValue)
            {
                if (icon.Value is null)
                {
                    iconData = new Optional <string?>(null);
                }
                else
                {
                    var packImage = await ImagePacker.PackImageAsync(icon.Value, ct);

                    if (!packImage.IsSuccess)
                    {
                        return(ModifyRestEntityResult <IGuild> .FromError(packImage));
                    }

                    iconData = packImage.Entity;
                }
            }

            Optional <string?> splashData = default;

            if (splash.HasValue)
            {
                if (splash.Value is null)
                {
                    splashData = new Optional <string?>(null);
                }
                else
                {
                    var packImage = await ImagePacker.PackImageAsync(splash.Value, ct);

                    if (!packImage.IsSuccess)
                    {
                        return(ModifyRestEntityResult <IGuild> .FromError(packImage));
                    }

                    splashData = packImage.Entity;
                }
            }

            var packBanner = await ImagePacker.PackImageAsync(banner, ct);

            if (!packBanner.IsSuccess)
            {
                return(ModifyRestEntityResult <IGuild> .FromError(packBanner));
            }

            var bannerData = packBanner.Entity;

            return(await _discordHttpClient.PatchAsync <IGuild>
                   (
                       $"guilds/{guildID}",
                       b => b.WithJson
                       (
                           json =>
            {
                json.Write("name", name, _jsonOptions);
                json.Write("region", region, _jsonOptions);
                json.WriteEnum("verification_level", verificationLevel, jsonOptions: _jsonOptions);
                json.WriteEnum
                (
                    "default_message_notifications",
                    defaultMessageNotifications,
                    jsonOptions: _jsonOptions
                );

                json.WriteEnum("explicit_content_filter", explicitContentFilter, jsonOptions: _jsonOptions);
                json.Write("afk_channel_id", afkChannelID, _jsonOptions);

                if (afkTimeout.HasValue)
                {
                    json.WriteNumber("afk_timeout", (ulong)afkTimeout.Value.TotalSeconds);
                }

                json.Write("icon", iconData, _jsonOptions);
                json.Write("owner_id", ownerID, _jsonOptions);
                json.Write("splash", splashData, _jsonOptions);
                json.Write("banner", bannerData, _jsonOptions);
                json.Write("system_channel_id", systemChannelID, _jsonOptions);
                json.Write("rules_channel_id", rulesChannelID, _jsonOptions);
                json.Write("public_updates_channel_id", publicUpdatesChannelID, _jsonOptions);
                json.Write("preferred_locale", preferredLocale, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }