/// <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)); }
/// <summary> /// Posts a message to a text channel with a file attachment. /// <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="fileData"/> is null, /// or if <paramref name="fileName"/> is null or only contains whitespace characters. /// </exception> /// <exception cref="DiscordHttpApiException"></exception> public Task <DiscordMessage> CreateMessage(Snowflake channelId, Stream fileData, string fileName, CreateMessageOptions options = null) { if (fileData == null) { throw new ArgumentNullException(nameof(fileData)); } return(CreateMessage(channelId, new StreamContent(fileData), fileName, options)); }
/// <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)); }
/// <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 = new DiscordApiData(DiscordApiDataType.Container); requestData.Set("content", options.Content); requestData.Set("tts", options.TextToSpeech); requestData.SetSnowflake("nonce", options.Nonce); if (options.Embed != null) { requestData.Set("embed", options.Embed.Build()); } DiscordApiData returnData = await rest.Post($"channels/{channelId}/messages", requestData, $"channels/{channelId}/messages").ConfigureAwait(false); return(new DiscordMessage(this, returnData)); }
/// <summary> /// Posts a message to a text channel with a file attachment. /// <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="fileName"/> is null or only contains whitespace characters. /// </exception> /// <exception cref="DiscordHttpApiException"></exception> public Task <DiscordMessage> CreateMessage(Snowflake channelId, ArraySegment <byte> fileData, string fileName, CreateMessageOptions options = null) { return(CreateMessage(channelId, new ByteArrayContent(fileData.Array, fileData.Offset, fileData.Count), fileName, options)); }