/// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="DiscordHttpApiException"></exception> async Task <DiscordMessage> ExecuteWebhook(Snowflake webhookId, string token, HttpContent fileContent, string fileName, ExecuteWebhookOptions options, bool waitAndReturnMessage) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (string.IsNullOrWhiteSpace(token)) { throw new ArgumentException("Token cannot be empty or only contain whitespace characters.", nameof(token)); } if (string.IsNullOrEmpty(fileName)) { // Technically already handled when adding the field to the multipart form data. throw new ArgumentNullException(nameof(fileName)); } DiscordApiData returnData = await rest.Send(() => { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"{RestClient.BASE_URL}/webhooks/{webhookId}/{token}"); 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); }, $"webhooks/{webhookId}/token").ConfigureAwait(false); return(waitAndReturnMessage ? new DiscordMessage(this, returnData) : null); }
/// <summary> /// Executes a webhook. /// <para>Note: Returns null unless <paramref name="waitAndReturnMessage"/> is set to true.</para> /// </summary> /// <param name="webhookId">The ID of the webhook to execute.</param> /// <param name="token">The webhook's token.</param> /// <param name="waitAndReturnMessage">Whether to wait for the message to be created /// and have it returned from this method.</param> /// <exception cref="ArgumentException">Thrown if the token is empty or only contains whitespace characters.</exception> /// <exception cref="ArgumentNullException">Thrown if the token or <paramref name="options"/> is null.</exception> /// <exception cref="DiscordHttpApiException"></exception> public async Task <DiscordMessage> ExecuteWebhook(Snowflake webhookId, string token, ExecuteWebhookOptions options, bool waitAndReturnMessage = false) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } if (string.IsNullOrWhiteSpace(token)) { throw new ArgumentException("Token cannot be empty or only contain whitespace characters.", nameof(token)); } DiscordApiData requestData = options.Build(); DiscordApiData returnData = await rest.Post($"webhooks/{webhookId}/{token}?wait={waitAndReturnMessage}", requestData, $"webhooks/{webhookId}/token").ConfigureAwait(false); return(waitAndReturnMessage ? new DiscordMessage(this, returnData) : null); }