public static async Task <Message> ModifyInteractionResponseAsync(BaseDiscordClient client, string token, Action <MessageProperties> func,
                                                                          RequestOptions options = null)
        {
            var args = new MessageProperties();

            func(args);

            var embed  = args.Embed;
            var embeds = args.Embeds;

            bool hasText       = !string.IsNullOrEmpty(args.Content.GetValueOrDefault());
            bool hasEmbeds     = embed.IsSpecified && embed.Value != null || embeds.IsSpecified && embeds.Value?.Length > 0;
            bool hasComponents = args.Components.IsSpecified && args.Components.Value != null;

            if (!hasComponents && !hasText && !hasEmbeds)
            {
                Preconditions.NotNullOrEmpty(args.Content.IsSpecified ? args.Content.Value : string.Empty, nameof(args.Content));
            }

            var apiEmbeds = embed.IsSpecified || embeds.IsSpecified ? new List <API.Embed>() : null;

            if (embed.IsSpecified && embed.Value != null)
            {
                apiEmbeds.Add(embed.Value.ToModel());
            }

            if (embeds.IsSpecified && embeds.Value != null)
            {
                apiEmbeds.AddRange(embeds.Value.Select(x => x.ToModel()));
            }

            Preconditions.AtMost(apiEmbeds?.Count ?? 0, 10, nameof(args.Embeds), "A max of 10 embeds are allowed.");

            if (!args.Attachments.IsSpecified)
            {
                var apiArgs = new ModifyInteractionResponseParams
                {
                    Content         = args.Content,
                    Embeds          = apiEmbeds?.ToArray() ?? Optional <API.Embed[]> .Unspecified,
                    AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value?.ToModel() : Optional <API.AllowedMentions> .Unspecified,
                    Components      = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() : Optional <API.ActionRowComponent[]> .Unspecified,
                    Flags           = args.Flags
                };

                return(await client.ApiClient.ModifyInteractionResponseAsync(apiArgs, token, options).ConfigureAwait(false));
            }
            else
            {
                var apiArgs = new UploadWebhookFileParams(args.Attachments.Value.ToArray())
                {
                    Content           = args.Content,
                    Embeds            = apiEmbeds?.ToArray() ?? Optional <API.Embed[]> .Unspecified,
                    AllowedMentions   = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value?.ToModel() : Optional <API.AllowedMentions> .Unspecified,
                    MessageComponents = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() : Optional <API.ActionRowComponent[]> .Unspecified,
                };

                return(await client.ApiClient.ModifyInteractionResponseAsync(apiArgs, token, options).ConfigureAwait(false));
            }
        }
Пример #2
0
        public static async Task <Discord.API.Message> ModifyFollowupMessageAsync(BaseDiscordClient client, RestFollowupMessage message, Action <MessageProperties> func,
                                                                                  RequestOptions options = null)
        {
            var args = new MessageProperties();

            func(args);

            var embed  = args.Embed;
            var embeds = args.Embeds;

            bool hasText       = args.Content.IsSpecified ? !string.IsNullOrEmpty(args.Content.Value) : !string.IsNullOrEmpty(message.Content);
            bool hasEmbeds     = embed.IsSpecified && embed.Value != null || embeds.IsSpecified && embeds.Value?.Length > 0 || message.Embeds.Any();
            bool hasComponents = args.Components.IsSpecified && args.Components.Value != null;

            if (!hasComponents && !hasText && !hasEmbeds)
            {
                Preconditions.NotNullOrEmpty(args.Content.IsSpecified ? args.Content.Value : string.Empty, nameof(args.Content));
            }

            var apiEmbeds = embed.IsSpecified || embeds.IsSpecified ? new List <API.Embed>() : null;

            if (embed.IsSpecified && embed.Value != null)
            {
                apiEmbeds.Add(embed.Value.ToModel());
            }

            if (embeds.IsSpecified && embeds.Value != null)
            {
                apiEmbeds.AddRange(embeds.Value.Select(x => x.ToModel()));
            }

            Preconditions.AtMost(apiEmbeds?.Count ?? 0, 10, nameof(args.Embeds), "A max of 10 embeds are allowed.");

            var apiArgs = new ModifyInteractionResponseParams
            {
                Content         = args.Content,
                Embeds          = apiEmbeds?.ToArray() ?? Optional <API.Embed[]> .Unspecified,
                AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value.ToModel() : Optional <API.AllowedMentions> .Unspecified,
                Components      = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() : Optional <API.ActionRowComponent[]> .Unspecified
            };

            return(await client.ApiClient.ModifyInteractionFollowupMessageAsync(apiArgs, message.Id, message.Token, options).ConfigureAwait(false));
        }