コード例 #1
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);
        }
コード例 #2
0
        /// <summary>
        ///     Sends a followup message for this interaction.
        /// </summary>
        /// <param name="text">The text of the message to be sent</param>
        /// <param name="embeds">A <see cref="Embed"/> to send with this response.</param>
        /// <param name="isTTS"><see langword="true"/> if the message should be read out by a text-to-speech reader, otherwise <see langword="false"/>.</param>
        /// <param name="allowedMentions">The allowed mentions for this response.</param>
        /// <param name="flags"></param>
        /// <param name="options">The request options for this response.</param>
        /// <returns>
        ///     The sent message.
        /// </returns>
        public async Task <IMessage> FollowupEmbedsAsync(IEnumerable <Embed> embeds, string text = null, bool isTTS = false, AllowedMentions allowedMentions = null,
                                                         int?flags = null, RequestOptions options = null)
        {
            if (!IsValidToken)
            {
                throw new InvalidOperationException("Interaction token is no longer valid");
            }

            var args = new CreateWebhookMessageParams(text)
            {
                IsTTS = isTTS
            };

            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.SendFollowupAsync(Discord.Rest, args, Token, Channel, options).ConfigureAwait(false));
        }
コード例 #3
0
        public async Task <WebhookMessage?> SendFileAsync(IDictionary <string, Stream> files, string text = null, bool isTTS = false, IEnumerable <Embed> embed = null, string username = null, string avatarUrl = null,
                                                          RequestOptions options = null)
        {
            var args = new CreateWebhookMessageParams()
            {
                Embeds = embed?.Select(x => x.ToModel()), Content = text, Tts = isTTS, Username = username, AvatarUrl = avatarUrl
            };

            return(await MultiPartRequest(files, args));
        }
コード例 #4
0
        public async Task <WebhookMessage> EditMessageAsync(ulong messageId, string text = null, bool isTTS      = false,
                                                            IEnumerable <Embed> embed    = null, string username = null, string avatarUrl = null,
                                                            RequestOptions options       = null)
        {
            var args = new CreateWebhookMessageParams()
            {
                Embeds = embed?.Select(x => x.ToModel()), Content = text, Tts = isTTS, Username = username, AvatarUrl = avatarUrl
            };

            return(await PatchRequest(messageId, args));
        }
コード例 #5
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);
        }
コード例 #6
0
        private async Task <WebhookMessage?> PostRequest(CreateWebhookMessageParams args)
        {
            using var request = new HttpRequestMessage(HttpMethod.Post, $"https://discord.com/api/webhooks/{_webhookId}/{_webhookToken}?wait=true");

            request.Content = new StringContent(JsonSerializer.Serialize(args, _options));
            request.Content.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Application.Json);

            var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead,
                                                       CancellationToken.None).ConfigureAwait(false);

            response.EnsureSuccessStatusCode();
            var byteArray = await response.Content.ReadAsByteArrayAsync();

            if (byteArray.Length <= 0)
            {
                return(null);
            }

            var msg = JsonSerializer.Deserialize <WebhookMessage>(byteArray, _options);

            return(msg);
        }
コード例 #7
0
        public async Task SendMessageAsync(string text, bool isTTS = false, Embed[] embeds  = null,
                                           string username         = null, string avatarUrl = null, RequestOptions options = null)
        {
            var 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;
            }
            await ApiClient.CreateWebhookMessageAsync(_webhookId, args, options).ConfigureAwait(false);
        }
コード例 #8
0
        public static async Task <RestFollowupMessage> SendFollowupAsync(BaseDiscordClient client, CreateWebhookMessageParams 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);
        }
コード例 #9
0
        private async Task <WebhookMessage?> MultiPartRequest(IDictionary <string, Stream> files, CreateWebhookMessageParams args)
        {
            using var request = new HttpRequestMessage(HttpMethod.Post, $"https://discord.com/api/webhooks/{_webhookId}/{_webhookToken}?wait=true");
            request.Headers.Add("Connection", "keep-alive");
            request.Headers.Add("Keep-Alive", "600");
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            var values = new Dictionary <string, string> {
                ["payload_json"] = JsonSerializer.Serialize(args, _options)
            };

            var content = new MultipartFormDataContent(boundary);

            foreach (var kvp in values)
            {
                content.Add(new StringContent(kvp.Value), kvp.Key);
            }

            int i = 1;

            foreach (var file in files)
            {
                content.Add(new StreamContent(file.Value), $"file{i++.ToString(CultureInfo.InvariantCulture)}",
                            file.Key);
            }

            var response = await _httpClient
                           .SendAsync(request, HttpCompletionOption.ResponseContentRead, CancellationToken.None)
                           .ConfigureAwait(false);

            response.EnsureSuccessStatusCode();

            return(await response.Content.ReadFromJsonAsync <WebhookMessage>());
        }