Esempio n. 1
0
        /// <exception cref="ArgumentNullException"></exception>
        async Task <DiscordMessage> CreateMessage(Snowflake channelId, HttpContent fileContent, string fileName,
                                                  CreateMessageOptions options)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                // Technically this is also handled when setting the field on the multipart form data
                throw new ArgumentNullException(nameof(fileName));
            }

            DiscordApiData returnData = await rest.Send(() =>
            {
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,
                                                                    $"{RestClient.BASE_URL}/channels/{channelId}/messages");

                MultipartFormDataContent data = new MultipartFormDataContent();
                data.Add(fileContent, "file", fileName);

                if (options != null)
                {
                    DiscordApiData payloadJson = options.Build();

                    data.Add(new StringContent(payloadJson.SerializeToJson()), "payload_json");
                }

                request.Content = data;

                return(request);
            }, $"channels/{channelId}/messages").ConfigureAwait(false);

            return(new DiscordMessage(this, returnData));
        }
Esempio n. 2
0
        /// <summary>
        /// Posts a message to a text channel.
        /// <para>Note: Bot user accounts must connect to the Gateway at least once before being able to send messages.</para>
        /// <para>Requires <see cref="DiscordPermission.SendMessages"/>.</para>
        /// <para>Requires <see cref="DiscordPermission.SendTtsMessages"/> if TTS is enabled on the message.</para>
        /// </summary>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="options"/> is null.</exception>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <DiscordMessage> CreateMessage(Snowflake channelId, CreateMessageOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            DiscordApiData requestData = options.Build();

            DiscordApiData returnData = await rest.Post($"channels/{channelId}/messages", requestData,
                                                        $"channels/{channelId}/messages").ConfigureAwait(false);

            return(new DiscordMessage(this, returnData));
        }