Example #1
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);
        }
Example #2
0
        public static async Task <ulong> SendMessageAsync(DiscordWebhookClient client,
                                                          string text, bool isTTS, IEnumerable <Embed> embeds, string username, string avatarUrl, RequestOptions options, AllowedMentions allowedMentions, InteractionRow[] components)
        {
            CreateWebhookMessageParams args = new CreateWebhookMessageParams(text)
            {
                IsTTS = isTTS
            };

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

            API.MessageJson model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options : options).ConfigureAwait(false);

            return(model.Id);
        }
Example #3
0
        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);
        }
 internal RestInternalWebhook(DiscordWebhookClient apiClient, Model model)
 {
     _client   = apiClient;
     Id        = model.Id;
     ChannelId = model.Id;
     Token     = model.Token;
 }
        internal static RestInternalWebhook Create(DiscordWebhookClient client, Model model)
        {
            var entity = new RestInternalWebhook(client, model);

            entity.Update(model);
            return(entity);
        }
Example #6
0
        public static async Task <ulong> SendFileAsync(DiscordWebhookClient client, string filePath, string text, bool isTTS,
                                                       IEnumerable <Embed> embeds, string username, string avatarUrl, RequestOptions options)
        {
            string filename = Path.GetFileName(filePath);

            using (var file = File.OpenRead(filePath))
                return(await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, options).ConfigureAwait(false));
        }
Example #7
0
        public static async Task <ulong> SendFileAsync(DiscordWebhookClient client, string filePath, string text, bool isTTS,
                                                       IEnumerable <Embed> embeds, string username, string avatarUrl, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions, InteractionRow[] components)
        {
            string filename = Path.GetFileName(filePath);

            using (FileStream file = File.OpenRead(filePath))
                return(await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, options, isSpoiler, allowedMentions, components).ConfigureAwait(false));
        }
Example #8
0
        public static async Task <ulong> SendFileAsync(DiscordWebhookClient client, string filePath, string text, bool isTTS,
                                                       IEnumerable <Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options,
                                                       bool isSpoiler, MessageComponent components, MessageFlags flags = MessageFlags.None)
        {
            string filename = Path.GetFileName(filePath);

            using (var file = File.OpenRead(filePath))
                return(await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, isSpoiler, components, flags).ConfigureAwait(false));
        }
Example #9
0
        /// <exception cref="InvalidOperationException">Could not find a webhook with the supplied credentials.</exception>
        public static async Task <RestInternalWebhook> GetWebhookAsync(DiscordWebhookClient client, ulong webhookId)
        {
            WebhookModel model = await client.ApiClient.GetWebhookAsync(webhookId).ConfigureAwait(false);

            if (model == null)
            {
                throw new InvalidOperationException("Could not find a webhook with the supplied credentials.");
            }
            return(RestInternalWebhook.Create(client, model));
        }
Example #10
0
        public static async Task <ulong> SendFileAsync(DiscordWebhookClient client, IEnumerable <string> filePaths, string text, bool isTTS,
                                                       IEnumerable <Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler)
        {
            var files = filePaths.Select(x => new KeyValuePair <string, Stream>(Path.GetFileName(x), File.OpenRead(x)));
            var id    = await SendFileAsync(client, files, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, isSpoiler).ConfigureAwait(false);

            foreach (var file in files)
            {
                file.Value.Dispose();
            }
            return(id);
        }
Example #11
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);
        }
Example #12
0
        public static async Task ModifyMessageAsync(DiscordWebhookClient client, ulong messageId,
                                                    Action <WebhookMessageProperties> func, RequestOptions options)
        {
            var args = new WebhookMessageProperties();

            func(args);

            if (args.AllowedMentions.IsSpecified)
            {
                var allowedMentions = args.AllowedMentions.Value;
                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.");

                // check that user flag and user Id list are exclusive, same with role flag and role Id list
                if (allowedMentions?.AllowedTypes != null)
                {
                    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));
                    }
                }
            }

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

            await client.ApiClient.ModifyWebhookMessageAsync(client.Webhook.Id, messageId, apiArgs, options)
            .ConfigureAwait(false);
        }
Example #13
0
        public static async Task <WebhookModel> ModifyAsync(DiscordWebhookClient client,
                                                            Action <WebhookProperties> func, RequestOptions options)
        {
            WebhookProperties args = new WebhookProperties();

            func(args);
            ModifyWebhookParams apiArgs = new ModifyWebhookParams
            {
                Avatar = args.Image.IsSpecified ? args.Image.Value?.ToModel() : Optional.Create <ImageModel?>(),
                Name   = args.Name
            };

            if (!apiArgs.Avatar.IsSpecified && client.Webhook.AvatarId != null)
            {
                apiArgs.Avatar = new ImageModel(client.Webhook.AvatarId);
            }

            return(await client.ApiClient.ModifyWebhookAsync(client.Webhook.Id, apiArgs, options).ConfigureAwait(false));
        }
Example #14
0
        public static async Task <ulong> SendMessageAsync(DiscordWebhookClient client,
                                                          string text, bool isTTS, IEnumerable <Embed> embeds, string username, string avatarUrl,
                                                          AllowedMentions allowedMentions, RequestOptions options, MessageComponent components, MessageFlags flags)
        {
            var args = new CreateWebhookMessageParams
            {
                Content = text,
                IsTTS   = isTTS,
                Flags   = flags
            };

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

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

            var model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options : options).ConfigureAwait(false);

            return(model.Id);
        }
Example #15
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 DeleteMessageAsync(DiscordWebhookClient client, ulong messageId, RequestOptions options, ulong?threadId)
 {
     await client.ApiClient.DeleteWebhookMessageAsync(client.Webhook.Id, messageId, options, threadId).ConfigureAwait(false);
 }
Example #17
0
 public WebhookException(DiscordWebhookClient client)
 {
     Client = client;
 }
Example #18
0
 public WebhookException(DiscordWebhookClient client, string message) : base(message)
 {
     Client = client;
 }
Example #19
0
 public static async Task <ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
                                                IEnumerable <Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler)
 {
     return(await SendFileAsync(client, new[] { new KeyValuePair <string, Stream>(filename, stream) }, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, isSpoiler).ConfigureAwait(false));
 }
Example #20
0
 public static Task <ulong> SendFileAsync(DiscordWebhookClient client, FileAttachment attachment, string text, bool isTTS,
                                          IEnumerable <Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions,
                                          MessageComponent components, RequestOptions options, MessageFlags flags)
 => SendFilesAsync(client, new FileAttachment[] { attachment }, text, isTTS, embeds, username, avatarUrl, allowedMentions, components, options, flags);
Example #21
0
 public static async Task DeleteAsync(DiscordWebhookClient client, RequestOptions options)
 {
     await client.ApiClient.DeleteWebhookAsync(client.Webhook.Id, options).ConfigureAwait(false);
 }
 public static Task <ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
                                          IEnumerable <Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler,
                                          MessageComponent components)
 => SendFileAsync(client, new FileAttachment(stream, filename, isSpoiler: isSpoiler), text, isTTS, embeds, username, avatarUrl, allowedMentions, components, options);