Browse Source

Application webhooks (#86)

* Added webhook components for hooks having an application ID.
pull/1923/head
drobbins329 GitHub 3 years ago
parent
commit
860fd82465
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 43 additions and 6 deletions
  1. +5
    -0
      src/Discord.Net.Core/Discord.Net.Core.xml
  2. +6
    -1
      src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs
  3. +2
    -0
      src/Discord.Net.Rest/API/Common/Webhook.cs
  4. +2
    -0
      src/Discord.Net.Rest/API/Rest/ModifyWebhookMessageParams.cs
  5. +3
    -0
      src/Discord.Net.Rest/Discord.Net.Rest.xml
  6. +4
    -0
      src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs
  7. +1
    -0
      src/Discord.Net.Rest/Extensions/EntityExtensions.cs
  8. +6
    -1
      src/Discord.Net.Webhook/Discord.Net.Webhook.xml
  9. +2
    -2
      src/Discord.Net.Webhook/DiscordWebhookClient.cs
  10. +4
    -0
      src/Discord.Net.Webhook/Entities/Messages/WebhookMessageProperties.cs
  11. +3
    -0
      src/Discord.Net.Webhook/Entities/Webhooks/RestInternalWebhook.cs
  12. +5
    -2
      src/Discord.Net.Webhook/WebhookClientHelper.cs

+ 5
- 0
src/Discord.Net.Core/Discord.Net.Core.xml View File

@@ -10089,6 +10089,11 @@
Gets the user that created this webhook.
</summary>
</member>
<member name="P:Discord.IWebhook.ApplicationId">
<summary>
Gets the ID of the application owning this webhook.
</summary>
</member>
<member name="M:Discord.IWebhook.ModifyAsync(System.Action{Discord.WebhookProperties},Discord.RequestOptions)">
<summary>
Modifies this webhook.


+ 6
- 1
src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;

namespace Discord
@@ -49,6 +49,11 @@ namespace Discord
/// </summary>
IUser Creator { get; }

/// <summary>
/// Gets the ID of the application owning this webhook.
/// </summary>
ulong? ApplicationId { get; }

/// <summary>
/// Modifies this webhook.
/// </summary>


+ 2
- 0
src/Discord.Net.Rest/API/Common/Webhook.cs View File

@@ -21,5 +21,7 @@ namespace Discord.API

[JsonProperty("user")]
public Optional<User> Creator { get; set; }
[JsonProperty("application_id")]
public Optional<ulong> ApplicationId { get; set; }
}
}

+ 2
- 0
src/Discord.Net.Rest/API/Rest/ModifyWebhookMessageParams.cs View File

@@ -12,5 +12,7 @@ namespace Discord.API.Rest
public Optional<Embed[]> Embeds { get; set; }
[JsonProperty("allowed_mentions")]
public Optional<AllowedMentions> AllowedMentions { get; set; }
[JsonProperty("components")]
public Optional<API.ActionRowComponent[]> Components { get; set; }
}
}

+ 3
- 0
src/Discord.Net.Rest/Discord.Net.Rest.xml View File

@@ -4766,6 +4766,9 @@
<member name="P:Discord.Rest.RestWebhook.Creator">
<inheritdoc />
</member>
<member name="P:Discord.Rest.RestWebhook.ApplicationId">
<inheritdoc />
</member>
<member name="P:Discord.Rest.RestWebhook.CreatedAt">
<inheritdoc />
</member>


+ 4
- 0
src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs View File

@@ -24,6 +24,8 @@ namespace Discord.Rest
public ulong? GuildId { get; private set; }
/// <inheritdoc />
public IUser Creator { get; private set; }
/// <inheritdoc />
public ulong? ApplicationId { get; private set; }

/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
@@ -66,6 +68,8 @@ namespace Discord.Rest
GuildId = model.GuildId.Value;
if (model.Name.IsSpecified)
Name = model.Name.Value;
if (model.ApplicationId.IsSpecified)
ApplicationId = model.ApplicationId.Value;
}

/// <inheritdoc />


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

@@ -68,6 +68,7 @@ namespace Discord.Rest
model.Video = entity.Video.Value.ToModel();
return model;
}

public static API.AllowedMentions ToModel(this AllowedMentions entity)
{
return new API.AllowedMentions()


+ 6
- 1
src/Discord.Net.Webhook/Discord.Net.Webhook.xml View File

@@ -31,7 +31,7 @@
<exception cref="T:System.ArgumentException">Thrown if the <paramref name="webhookUrl"/> is an invalid format.</exception>
<exception cref="T:System.ArgumentNullException">Thrown if the <paramref name="webhookUrl"/> is null or whitespace.</exception>
</member>
<member name="M:Discord.Webhook.DiscordWebhookClient.SendMessageAsync(System.String,System.Boolean,System.Collections.Generic.IEnumerable{Discord.Embed},System.String,System.String,Discord.RequestOptions,Discord.AllowedMentions)">
<member name="M:Discord.Webhook.DiscordWebhookClient.SendMessageAsync(System.String,System.Boolean,System.Collections.Generic.IEnumerable{Discord.Embed},System.String,System.String,Discord.RequestOptions,Discord.AllowedMentions,Discord.MessageComponent)">
<summary> Sends a message to the channel for this webhook. </summary>
<returns> Returns the ID of the created message. </returns>
</member>
@@ -99,6 +99,11 @@
Gets or sets the allowed mentions of the message.
</summary>
</member>
<member name="P:Discord.Webhook.WebhookMessageProperties.Components">
<summary>
Gets or sets the components that the message should display.
</summary>
</member>
<member name="M:Discord.Webhook.WebhookClientHelper.GetWebhookAsync(Discord.Webhook.DiscordWebhookClient,System.UInt64)">
<exception cref="T:System.InvalidOperationException">Could not find a webhook with the supplied credentials.</exception>
</member>


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

@@ -88,8 +88,8 @@ namespace Discord.Webhook
/// <summary> Sends a message to the channel for this webhook. </summary>
/// <returns> Returns the ID of the created message. </returns>
public Task<ulong> SendMessageAsync(string text = null, bool isTTS = false, IEnumerable<Embed> embeds = null,
string username = null, string avatarUrl = null, RequestOptions options = null, AllowedMentions allowedMentions = null)
=> WebhookClientHelper.SendMessageAsync(this, text, isTTS, embeds, username, avatarUrl, allowedMentions, options);
string username = null, string avatarUrl = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageComponent component = null)
=> WebhookClientHelper.SendMessageAsync(this, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, component);

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


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

@@ -22,5 +22,9 @@ namespace Discord.Webhook
/// Gets or sets the allowed mentions of the message.
/// </summary>
public Optional<AllowedMentions> AllowedMentions { get; set; }
/// <summary>
/// Gets or sets the components that the message should display.
/// </summary>
public Optional<MessageComponent> Components { get; set; }
}
}

+ 3
- 0
src/Discord.Net.Webhook/Entities/Webhooks/RestInternalWebhook.cs View File

@@ -17,6 +17,7 @@ namespace Discord.Webhook
public string Name { get; private set; }
public string AvatarId { get; private set; }
public ulong? GuildId { get; private set; }
public ulong? ApplicationId { get; private set; }

public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);

@@ -44,6 +45,8 @@ namespace Discord.Webhook
GuildId = model.GuildId.Value;
if (model.Name.IsSpecified)
Name = model.Name.Value;
if (model.ApplicationId.IsSpecified)
ApplicationId = model.ApplicationId.Value;
}

public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)


+ 5
- 2
src/Discord.Net.Webhook/WebhookClientHelper.cs View File

@@ -21,7 +21,7 @@ namespace Discord.Webhook
return RestInternalWebhook.Create(client, model);
}
public static async Task<ulong> SendMessageAsync(DiscordWebhookClient client,
string text, bool isTTS, IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options)
string text, bool isTTS, IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, MessageComponent component)
{
var args = new CreateWebhookMessageParams
{
@@ -37,6 +37,8 @@ namespace Discord.Webhook
args.AvatarUrl = avatarUrl;
if (allowedMentions != null)
args.AllowedMentions = allowedMentions.ToModel();
if (component != null)
args.Components = component?.Components.Select(x => new API.ActionRowComponent(x)).ToArray();

var model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options: options).ConfigureAwait(false);
return model.Id;
@@ -83,7 +85,8 @@ namespace Discord.Webhook
: Optional.Create<API.Embed[]>(),
AllowedMentions = args.AllowedMentions.IsSpecified
? args.AllowedMentions.Value.ToModel()
: Optional.Create<API.AllowedMentions>()
: Optional.Create<API.AllowedMentions>(),
Components = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() : Optional<API.ActionRowComponent[]>.Unspecified,
};

await client.ApiClient.ModifyWebhookMessageAsync(client.Webhook.Id, messageId, apiArgs, options)


Loading…
Cancel
Save