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

            if (!packAvatar.IsSuccess)
            {
                return(Result <IWebhook> .FromError(new GenericError("Failed to pack avatar."), packAvatar));
            }

            var avatarData = packAvatar.Entity;

            return(await _discordHttpClient.PatchAsync <IWebhook>
                   (
                       $"webhooks/{webhookID}",
                       b => b.WithJson
                       (
                           json =>
            {
                json.Write("name", name, _jsonOptions);
                json.Write("avatar", avatarData, _jsonOptions);
                json.Write("channel_id", channelID, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }
        /// <inheritdoc />
        public Task <IModifyRestEntityResult <IMembershipScreening> > ModifyGuildMembershipScreeningFormAsync
        (
            Snowflake guildID,
            Optional <bool> isEnabled = default,
            Optional <IReadOnlyList <IMembershipScreeningField> > formFields = default,
            Optional <string> description = default,
            CancellationToken ct          = default
        )
        {
            return(_discordHttpClient.PatchAsync <IMembershipScreening>
                   (
                       $"guilds/{guildID}/member-verification",
                       b =>
            {
                Optional <string> formFieldsString = default;
                if (formFields.HasValue)
                {
                    formFieldsString = JsonSerializer.Serialize(formFields.Value, _jsonOptions);
                }

                b.WithJson
                (
                    json =>
                {
                    json.Write("enabled", isEnabled, _jsonOptions);
                    json.Write("form_fields", formFieldsString, _jsonOptions);
                    json.Write("description", description, _jsonOptions);
                }
                );
            },
                       ct: ct
                   ));
        }
        /// <inheritdoc />
        public virtual async Task <Result <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(new GenericError("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(new GenericError("The topic must be between 0 and 1024 characters."));
            }

            if (userLimit.HasValue && userLimit.Value is not null && (userLimit.Value > 99 || userLimit.Value < 0))
            {
                return(new GenericError("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
                   ));
        }
Пример #4
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
                   ));
        }
        /// <inheritdoc />
        public virtual async Task <Result <IApplicationCommand> > EditGlobalApplicationCommandAsync
        (
            Snowflake applicationID,
            Snowflake commandID,
            Optional <string> name,
            Optional <string> description,
            Optional <IReadOnlyList <IApplicationCommandOption>?> options,
            CancellationToken ct
        )
        {
            if (name.HasValue && name.Value.Length is < 1 or > 32)
            {
                return(new GenericError
                       (
                           "The name must be between 1 and 32 characters."
                       ));
            }

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

            return(await _discordHttpClient.PatchAsync <IApplicationCommand>
                   (
                       $"applications/{applicationID}/commands/{commandID}",
                       b => b.WithJson
                       (
                           json =>
            {
                json.Write("name", name, _jsonOptions);
                json.Write("description", description, _jsonOptions);
                json.Write("options", options, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }
 /// <inheritdoc />
 public Task <Result <IStageInstance> > UpdateStageInstanceAsync
 (
     Snowflake channelID,
     Optional <string> topic = default,
     Optional <StagePrivacyLevel> privacyLevel = default,
     CancellationToken ct = default
 )
 {
     return(_discordHttpClient.PatchAsync <IStageInstance>
            (
                $"stage-instances/{channelID}",
                r => r.WithJson
                (
                    json =>
     {
         json.Write("topic", topic);
         json.Write("privacy_level", privacyLevel);
     }
                ),
                ct: ct
            ));
 }
 /// <inheritdoc />
 public virtual Task <Result <ITemplate> > ModifyGuildTemplateAsync
 (
     Snowflake guildID,
     string templateCode,
     string name,
     Optional <string> description,
     CancellationToken ct = default
 )
 {
     return(_discordHttpClient.PatchAsync <ITemplate>
            (
                $"guilds/{guildID}/templates/{templateCode}",
                b => b.WithJson
                (
                    j =>
     {
         j.WriteString("name", name);
         j.Write("description", description, _jsonOptions);
     }
                ),
                ct: ct
            ));
 }
Пример #8
0
 /// <inheritdoc />
 public virtual Task <Result <IEmoji> > ModifyGuildEmojiAsync
 (
     Snowflake guildID,
     Snowflake emojiID,
     Optional <string> name = default,
     Optional <IReadOnlyList <Snowflake>?> roles = default,
     CancellationToken ct = default
 )
 {
     return(_discordHttpClient.PatchAsync <IEmoji>
            (
                $"guilds/{guildID}/emojis/{emojiID}",
                b => b.WithJson
                (
                    json =>
     {
         json.Write("name", name, _jsonOptions);
         json.Write("roles", roles, _jsonOptions);
     }
                ),
                ct: ct
            ));
 }
Пример #9
0
        /// <inheritdoc />
        public virtual async Task <Result <IChannel> > ModifyChannelAsync
        (
            Snowflake channelID,
            Optional <string> name           = default,
            Optional <Stream> icon           = 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,
            Optional <VideoQualityMode?> videoQualityMode = default,
            Optional <bool> isArchived = default,
            Optional <TimeSpan> autoArchiveDuration = default,
            Optional <bool> isLocked = default,
            CancellationToken ct     = default
        )
        {
            if (name.HasValue && (name.Value.Length > 100 || name.Value.Length < 2))
            {
                return(new GenericError("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(new GenericError("The topic must be between 0 and 1024 characters."));
            }

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

            Optional <string> base64EncodedIcon = default;

            if (icon.HasValue)
            {
                byte[] bytes;
                if (icon.Value is MemoryStream ms)
                {
                    bytes = ms.ToArray();
                }
                else
                {
                    await using var copy = new MemoryStream();
                    await icon.Value.CopyToAsync(copy, ct);

                    bytes = copy.ToArray();
                }

                base64EncodedIcon = Convert.ToBase64String(bytes);
            }

            return(await _discordHttpClient.PatchAsync <IChannel>
                   (
                       $"channels/{channelID}",
                       b => b.WithJson
                       (
                           json =>
            {
                json.Write("name", name, _jsonOptions);
                json.Write("icon", base64EncodedIcon, _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);
                json.WriteEnum("video_quality_mode", videoQualityMode, jsonOptions: _jsonOptions);
                json.Write("archived", isArchived, _jsonOptions);

                if (autoArchiveDuration.HasValue)
                {
                    json.WriteNumber("auto_archive_duration", autoArchiveDuration.Value.TotalMinutes);
                }

                json.Write("locked", isLocked, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }
Пример #10
0
        /// <inheritdoc />
        public virtual async Task <Result <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?> discoverySplash                = default,
            Optional <Stream?> banner                         = default,
            Optional <Snowflake?> systemChannelID             = default,
            Optional <SystemChannelFlags> systemChannelFlags  = default,
            Optional <Snowflake?> rulesChannelID              = default,
            Optional <Snowflake?> publicUpdatesChannelID      = default,
            Optional <string?> preferredLocale                = default,
            Optional <IReadOnlyList <GuildFeature> > features = default,
            Optional <string?> description                    = 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(Result <IGuild> .FromError(new GenericError("Failed to pack icon."), 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(Result <IGuild> .FromError(new GenericError("Failed to pack splash."), packImage));
                    }

                    splashData = packImage.Entity;
                }
            }

            Optional <string?> discoverySplashData = default;

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

                    if (!packImage.IsSuccess)
                    {
                        return(Result <IGuild> .FromError(new GenericError("Failed to pack discovery splash."), packImage));
                    }

                    discoverySplashData = packImage.Entity;
                }
            }

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

            if (!packBanner.IsSuccess)
            {
                return(Result <IGuild> .FromError(new GenericError("Failed to pack banner."), 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("discovery_splash", discoverySplashData, _jsonOptions);
                json.Write("banner", bannerData, _jsonOptions);
                json.Write("system_channel_id", systemChannelID, _jsonOptions);
                json.Write("system_channel_flags", systemChannelFlags, _jsonOptions);
                json.Write("rules_channel_id", rulesChannelID, _jsonOptions);
                json.Write("public_updates_channel_id", publicUpdatesChannelID, _jsonOptions);
                json.Write("preferred_locale", preferredLocale, _jsonOptions);
                json.Write("features", features, _jsonOptions);
                json.Write("description", description, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }