Exemple #1
0
        private async Task <IMessage> FollowupFileAsyncInternal(Stream stream, string filename, string text, bool isTTS, IEnumerable <Embed> embeds,
                                                                string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler, int?flags)
        {
            var args = new UploadWebhookFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS, IsSpoiler = isSpoiler
            };

            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = avatarUrl;
            }
            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            if (allowedMentions != null)
            {
                args.AllowedMentions = allowedMentions.ToModel();
            }
            if (flags.HasValue)
            {
                args.Flags = flags.Value;
            }

            return(await InteractionHelper.SendFollowupFileAsync(Discord.Rest, args, Token, Channel, options).ConfigureAwait(false));
        }
Exemple #2
0
        public static async Task <ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
                                                       IEnumerable <Embed> embeds, string username, string avatarUrl, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions, InteractionRow[] components)
        {
            UploadWebhookFileParams args = new UploadWebhookFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS, IsSpoiler = isSpoiler
            };

            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = avatarUrl;
            }
            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            if (allowedMentions != null)
            {
                args.AllowedMentions = allowedMentions.ToModel();
            }
            if (components != null)
            {
                args.Components = components.Select(x => x.ToModel()).ToArray();
            }

            API.MessageJson msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options).ConfigureAwait(false);

            return(msg.Id);
        }
        public static async Task <ulong> SendFileAsync(DiscordWebhookClient client, IEnumerable <KeyValuePair <string, Stream> > files, string text, bool isTTS,
                                                       IEnumerable <Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler)
        {
            var args = new UploadWebhookFileParams(files)
            {
                Content = text, IsTTS = isTTS, IsSpoiler = isSpoiler
            };

            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = avatarUrl;
            }
            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            if (allowedMentions != null)
            {
                args.AllowedMentions = allowedMentions.ToModel();
            }
            var msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options).ConfigureAwait(false);

            return(msg.Id);
        }
        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));
            }
        }
Exemple #5
0
        public static async Task <ulong> SendFilesAsync(DiscordWebhookClient client,
                                                        IEnumerable <FileAttachment> attachments, string text, bool isTTS, IEnumerable <Embed> embeds, string username,
                                                        string avatarUrl, AllowedMentions allowedMentions, MessageComponent components, RequestOptions options,
                                                        MessageFlags flags)
        {
            embeds ??= Array.Empty <Embed>();

            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");
            Preconditions.AtMost(embeds.Count(), 10, nameof(embeds), "A max of 10 embeds are allowed.");

            foreach (var attachment in attachments)
            {
                Preconditions.NotNullOrEmpty(attachment.FileName, nameof(attachment.FileName), "File Name must not be empty or null");
            }

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            if (flags is not MessageFlags.None and not MessageFlags.SuppressEmbeds)
            {
                throw new ArgumentException("The only valid MessageFlags are SuppressEmbeds and none.", nameof(flags));
            }

            var args = new UploadWebhookFileParams(attachments.ToArray())
            {
                AvatarUrl         = avatarUrl,
                Username          = username, Content = text,
                IsTTS             = isTTS,
                Embeds            = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional <API.Embed[]> .Unspecified,
                AllowedMentions   = allowedMentions?.ToModel() ?? Optional <API.AllowedMentions> .Unspecified,
                MessageComponents = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional <API.ActionRowComponent[]> .Unspecified,
                Flags             = flags
            };
            var msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options).ConfigureAwait(false);

            return(msg.Id);
        }
        public async Task SendFileAsync(Stream stream, string filename, string text, bool isTTS = false,
                                        string username = null, string avatarUrl = null, RequestOptions options = null)
        {
            var args = new UploadWebhookFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS
            };

            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = username;
            }
            await ApiClient.UploadWebhookFileAsync(_webhookId, args, options).ConfigureAwait(false);
        }
Exemple #7
0
        public static async Task <ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
                                                       IEnumerable <Embed> embeds, string username, string avatarUrl, RequestOptions options)
        {
            var args = new UploadWebhookFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS
            };

            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = avatarUrl;
            }
            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            var msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options).ConfigureAwait(false);

            return(msg.Id);
        }
        public static async Task <RestFollowupMessage> SendFollowupAsync(BaseDiscordClient client, UploadWebhookFileParams args,
                                                                         string token, IMessageChannel channel, RequestOptions options = null)
        {
            var model = await client.ApiClient.CreateInteractionFollowupMessageAsync(args, token, options).ConfigureAwait(false);

            var entity = RestFollowupMessage.Create(client, model, token, channel);

            return(entity);
        }