Browse Source

feature: Webhook message edit & delete functionality

pull/1753/head
Desmont 4 years ago
parent
commit
b0dedb63db
4 changed files with 84 additions and 0 deletions
  1. +22
    -0
      src/Discord.Net.Rest/API/Rest/EditWebhookMessageParams.cs
  2. +38
    -0
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  3. +9
    -0
      src/Discord.Net.Webhook/DiscordWebhookClient.cs
  4. +15
    -0
      src/Discord.Net.Webhook/WebhookClientHelper.cs

+ 22
- 0
src/Discord.Net.Rest/API/Rest/EditWebhookMessageParams.cs View File

@@ -0,0 +1,22 @@
#pragma warning disable CS1591
using Newtonsoft.Json;

namespace Discord.API.Rest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
internal class EditWebhookMessageParams
{
[JsonProperty("content")]
public string Content { get; }

[JsonProperty("embeds")]
public Optional<Embed[]> Embeds { get; set; }
[JsonProperty("allowed_mentions")]
public Optional<AllowedMentions> AllowedMentions { get; set; }

public EditWebhookMessageParams(string content)
{
Content = content;
}
}
}

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

@@ -523,6 +523,44 @@ namespace Discord.API
var ids = new BucketIds(webhookId: webhookId);
return await SendJsonAsync<Message>("POST", () => $"webhooks/{webhookId}/{AuthToken}?wait=true", args, ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
}

/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
/// <exception cref="InvalidOperationException">This operation may only be called with a <see cref="TokenType.Webhook"/> token.</exception>
public async Task EditWebhookMessageAsync(ulong webhookId, ulong messageId, EditWebhookMessageParams args, RequestOptions options = null)
{
if (AuthTokenType != TokenType.Webhook)
throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token.");

Preconditions.NotNull(args, nameof(args));
Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
Preconditions.NotEqual(messageId, 0, nameof(messageId));
if (!args.Embeds.IsSpecified || args.Embeds.Value == null || args.Embeds.Value.Length == 0)
Preconditions.NotNullOrEmpty(args.Content, nameof(args.Content));

if (args.Content?.Length > DiscordConfig.MaxMessageSize)
throw new ArgumentException(message: $"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", paramName: nameof(args.Content));
options = RequestOptions.CreateOrClone(options);

var ids = new BucketIds(webhookId: webhookId);
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>
public async Task DeleteWebhookMessageAsync(ulong webhookId, ulong messageId, RequestOptions options = null)
{
if (AuthTokenType != TokenType.Webhook)
throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token.");

Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
Preconditions.NotEqual(messageId, 0, nameof(messageId));

options = RequestOptions.CreateOrClone(options);

var ids = new BucketIds(webhookId: webhookId);
await SendAsync("DELETE", () => $"webhooks/{webhookId}/{AuthToken}/messages/{messageId}", ids, options: options).ConfigureAwait(false);
}

/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
public async Task<Message> UploadFileAsync(ulong channelId, UploadFileParams args, RequestOptions options = null)
{


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

@@ -91,6 +91,15 @@ namespace Discord.Webhook
string username = null, string avatarUrl = null, RequestOptions options = null, AllowedMentions allowedMentions = null)
=> WebhookClientHelper.SendMessageAsync(this, text, isTTS, embeds, username, avatarUrl, allowedMentions, options);

/// <summary> Edits a message posted using this webhook. </summary>
public Task EditMessageAsync(ulong messageId, string text = null, IEnumerable<Embed> embeds = null,
AllowedMentions allowedMentions = null, RequestOptions options = null)
=> WebhookClientHelper.EditMessageAsync(this, messageId, text, embeds, allowedMentions, options);

/// <summary> Deletes a message posted using this webhook. </summary>
public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null)
=> WebhookClientHelper.DeleteMessageAsync(this, messageId, options);

/// <summary> Sends a message to the channel for this webhook with an attachment. </summary>
/// <returns> Returns the ID of the created message. </returns>
public Task<ulong> SendFileAsync(string filePath, string text, bool isTTS = false,


+ 15
- 0
src/Discord.Net.Webhook/WebhookClientHelper.cs View File

@@ -36,6 +36,21 @@ namespace Discord.Webhook
var model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options: options).ConfigureAwait(false);
return model.Id;
}
public static async Task EditMessageAsync(DiscordWebhookClient client, ulong messageId, string text, IEnumerable<Embed> embeds,
AllowedMentions allowedMentions, RequestOptions options)
{
var args = new EditWebhookMessageParams(text);
if (embeds != null)
args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
if (allowedMentions != null)
args.AllowedMentions = allowedMentions.ToModel();

await client.ApiClient.EditWebhookMessageAsync(client.Webhook.Id, messageId, args, options: options).ConfigureAwait(false);
}
public static async Task DeleteMessageAsync(DiscordWebhookClient client, ulong messageId, RequestOptions options)
{
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,
IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler)
{


Loading…
Cancel
Save