Browse Source

Change the Webhook message edit functionality to use a object delegate method instead providing the all parameters

pull/1753/head
Desmont 4 years ago
parent
commit
94df87df73
4 changed files with 75 additions and 19 deletions
  1. +0
    -1
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  2. +3
    -9
      src/Discord.Net.Webhook/DiscordWebhookClient.cs
  3. +26
    -0
      src/Discord.Net.Webhook/Entities/Messages/WebhookMessageProperties.cs
  4. +46
    -9
      src/Discord.Net.Webhook/WebhookClientHelper.cs

+ 0
- 1
src/Discord.Net.Rest/DiscordRestApiClient.cs View File

@@ -543,7 +543,6 @@ namespace Discord.API


var ids = new BucketIds(webhookId: webhookId); var ids = new BucketIds(webhookId: webhookId);
await SendJsonAsync<Message>("PATCH", () => $"webhooks/{webhookId}/{AuthToken}/messages/{messageId}", args, ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); await SendJsonAsync<Message>("PATCH", () => $"webhooks/{webhookId}/{AuthToken}/messages/{messageId}", args, ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);

} }


/// <exception cref="InvalidOperationException">This operation may only be called with a <see cref="TokenType.Webhook"/> token.</exception> /// <exception cref="InvalidOperationException">This operation may only be called with a <see cref="TokenType.Webhook"/> token.</exception>


+ 3
- 9
src/Discord.Net.Webhook/DiscordWebhookClient.cs View File

@@ -98,19 +98,13 @@ namespace Discord.Webhook
/// This method can only modify messages that were sent using the same webhook. /// This method can only modify messages that were sent using the same webhook.
/// </remarks> /// </remarks>
/// <param name="messageId">ID of the modified message.</param> /// <param name="messageId">ID of the modified message.</param>
/// <param name="text">The message to be sent.</param>
/// <param name="embeds">The <see cref="Discord.EmbedType.Rich" /> <see cref="Embed" /> enumeration to be sent.</param>
/// <param name="allowedMentions">
/// Specifies if notifications are sent for mentioned users and roles in the message <paramref name="text"/>.
/// If <c>null</c>, all mentioned roles and users will be notified.
/// </param>
/// <param name="func">A delegate containing the properties to modify the message with.</param>
/// <param name="options">The options to be used when sending the request.</param> /// <param name="options">The options to be used when sending the request.</param>
/// <returns> /// <returns>
/// A task that represents the asynchronous modification operation. /// A task that represents the asynchronous modification operation.
/// </returns> /// </returns>
public Task ModifyMessageAsync(ulong messageId, string text = null, IEnumerable<Embed> embeds = null,
AllowedMentions allowedMentions = null, RequestOptions options = null)
=> WebhookClientHelper.ModifyMessageAsync(this, messageId, text, embeds, allowedMentions, options);
public Task ModifyMessageAsync(ulong messageId, Action<WebhookMessageProperties> func, RequestOptions options = null)
=> WebhookClientHelper.ModifyMessageAsync(this, messageId, func, options);


/// <summary> /// <summary>
/// Deletes a message posted using this webhook. /// Deletes a message posted using this webhook.


+ 26
- 0
src/Discord.Net.Webhook/Entities/Messages/WebhookMessageProperties.cs View File

@@ -0,0 +1,26 @@
using System.Collections.Generic;

namespace Discord.Webhook
{
/// <summary>
/// Properties that are used to modify an Webhook message with the specified changes.
/// </summary>
public class WebhookMessageProperties
{
/// <summary>
/// Gets or sets the content of the message.
/// </summary>
/// <remarks>
/// This must be less than the constant defined by <see cref="DiscordConfig.MaxMessageSize"/>.
/// </remarks>
public Optional<string> Content { get; set; }
/// <summary>
/// Gets or sets the embed array that the message should display.
/// </summary>
public Optional<IEnumerable<Embed>> Embeds { get; set; }
/// <summary>
/// Gets or sets the allowed mentions of the message.
/// </summary>
public Optional<AllowedMentions> AllowedMentions { get; set; }
}
}

+ 46
- 9
src/Discord.Net.Webhook/WebhookClientHelper.cs View File

@@ -36,22 +36,59 @@ namespace Discord.Webhook
var model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options: options).ConfigureAwait(false); var model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options: options).ConfigureAwait(false);
return model.Id; return model.Id;
} }
public static async Task ModifyMessageAsync(DiscordWebhookClient client, ulong messageId, string text, IEnumerable<Embed> embeds,
AllowedMentions allowedMentions, RequestOptions options)
public static async Task ModifyMessageAsync(DiscordWebhookClient client, ulong messageId,
Action<WebhookMessageProperties> func, RequestOptions options)
{ {
var args = new ModifyWebhookMessageParams { Content = text };
if (embeds != null)
args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
if (allowedMentions != null)
args.AllowedMentions = allowedMentions.ToModel();
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>()
};


await client.ApiClient.ModifyWebhookMessageAsync(client.Webhook.Id, messageId, args, options: options).ConfigureAwait(false);
await client.ApiClient.ModifyWebhookMessageAsync(client.Webhook.Id, messageId, apiArgs, options)
.ConfigureAwait(false);
} }
public static async Task DeleteMessageAsync(DiscordWebhookClient client, ulong messageId, RequestOptions options) public static async Task DeleteMessageAsync(DiscordWebhookClient client, ulong messageId, RequestOptions options)
{ {
await client.ApiClient.DeleteWebhookMessageAsync(client.Webhook.Id, messageId, options).ConfigureAwait(false); await client.ApiClient.DeleteWebhookMessageAsync(client.Webhook.Id, messageId, options).ConfigureAwait(false);
} }
public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, string filePath, string text, bool isTTS,
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) IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler)
{ {
string filename = Path.GetFileName(filePath); string filename = Path.GetFileName(filePath);


Loading…
Cancel
Save