Browse Source

Add Webhook API models, REST implementation, and Socket bridges.

pull/843/head
Alex Gravely 8 years ago
parent
commit
7229ce4986
25 changed files with 476 additions and 7 deletions
  1. +9
    -0
      src/Discord.Net.Core/Entities/Channels/ITextChannel.cs
  2. +5
    -0
      src/Discord.Net.Core/Entities/Guilds/IGuild.cs
  3. +0
    -1
      src/Discord.Net.Core/Entities/Users/IWebhookUser.cs
  4. +34
    -0
      src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs
  5. +27
    -0
      src/Discord.Net.Core/Entities/Webhooks/WebhookProperties.cs
  6. +2
    -0
      src/Discord.Net.Core/IDiscordClient.cs
  7. +25
    -0
      src/Discord.Net.Rest/API/Common/Webhook.cs
  8. +2
    -0
      src/Discord.Net.Rest/API/Rest/CreateWebhookMessageParams.cs
  9. +14
    -0
      src/Discord.Net.Rest/API/Rest/CreateWebhookParams.cs
  10. +14
    -0
      src/Discord.Net.Rest/API/Rest/ModifyWebhookParams.cs
  11. +3
    -0
      src/Discord.Net.Rest/BaseDiscordClient.cs
  12. +8
    -0
      src/Discord.Net.Rest/ClientHelper.cs
  13. +82
    -3
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  14. +6
    -0
      src/Discord.Net.Rest/DiscordRestClient.cs
  15. +25
    -0
      src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
  16. +15
    -0
      src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
  17. +14
    -0
      src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
  18. +11
    -0
      src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
  19. +89
    -0
      src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs
  20. +34
    -0
      src/Discord.Net.Rest/Entities/Webhooks/WebhookHelper.cs
  21. +15
    -1
      src/Discord.Net.Rpc/Entities/Channels/RpcTextChannel.cs
  22. +13
    -0
      src/Discord.Net.WebSocket/API/Gateway/WebhookUpdateEvent.cs
  23. +17
    -1
      src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
  24. +11
    -0
      src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
  25. +1
    -1
      src/Discord.Net.Webhook/DiscordWebhookClient.cs

+ 9
- 0
src/Discord.Net.Core/Entities/Channels/ITextChannel.cs View File

@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Discord
@@ -13,5 +15,12 @@ namespace Discord

/// <summary> Modifies this text channel. </summary>
Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null);

/// <summary> Creates a webhook in this text channel. </summary>
Task<IWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null);
/// <summary> Gets the webhook in this text channel with the provided id, or null if not found. </summary>
Task<IWebhook> GetWebhookAsync(ulong id, RequestOptions options = null);
/// <summary> Gets the webhooks for this text channel. </summary>
Task<IReadOnlyCollection<IWebhook>> GetWebhooksAsync(RequestOptions options = null);
}
}

+ 5
- 0
src/Discord.Net.Core/Entities/Guilds/IGuild.cs View File

@@ -117,5 +117,10 @@ namespace Discord
Task DownloadUsersAsync();
/// <summary> Removes all users from this guild if they have not logged on in a provided number of days or, if simulate is true, returns the number of users that would be removed. </summary>
Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null);

/// <summary> Gets the webhook in this guild with the provided id, or null if not found. </summary>
Task<IWebhook> GetWebhookAsync(ulong id, RequestOptions options = null);
/// <summary> Gets a collection of all webhooks for this guild. </summary>
Task<IReadOnlyCollection<IWebhook>> GetWebhooksAsync(RequestOptions options = null);
}
}

+ 0
- 1
src/Discord.Net.Core/Entities/Users/IWebhookUser.cs View File

@@ -1,6 +1,5 @@
namespace Discord
{
//TODO: Add webhook endpoints
public interface IWebhookUser : IGuildUser
{
ulong WebhookId { get; }


+ 34
- 0
src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs View File

@@ -0,0 +1,34 @@
using System;
using System.Threading.Tasks;

namespace Discord
{
public interface IWebhook : IDeletable, ISnowflakeEntity
{
/// <summary> Gets the token of this webhook. </summary>
string Token { get; }

/// <summary> Gets the default name of this webhook. </summary>
string Name { get; }
/// <summary> Gets the id of this webhook's default avatar. </summary>
string AvatarId { get; }
/// <summary> Gets the url to this webhook's default avatar. </summary>
string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);

/// <summary> Gets the channel for this webhook. </summary>
ITextChannel Channel { get; }
/// <summary> Gets the id of the channel for this webhook. </summary>
ulong ChannelId { get; }

/// <summary> Gets the guild owning this webhook. </summary>
IGuild Guild { get; }
/// <summary> Gets the id of the guild owning this webhook. </summary>
ulong GuildId { get; }

/// <summary> Gets the user that created this webhook. </summary>
IUser Creator { get; }

/// <summary> Modifies this webhook. </summary>
Task ModifyAsync(Action<WebhookProperties> func, string webhookToken = null, RequestOptions options = null);
}
}

+ 27
- 0
src/Discord.Net.Core/Entities/Webhooks/WebhookProperties.cs View File

@@ -0,0 +1,27 @@
namespace Discord
{
/// <summary>
/// Modify an <see cref="IWebhook"/> with the specified parameters.
/// </summary>
/// <example>
/// <code language="c#">
/// await webhook.ModifyAsync(x =>
/// {
/// x.Name = "Bob";
/// x.Avatar = new Image("avatar.jpg");
/// });
/// </code>
/// </example>
/// <seealso cref="IWebhook"/>
public class WebhookProperties
{
/// <summary>
/// The default name of the webhook.
/// </summary>
public Optional<string> Name { get; set; }
/// <summary>
/// The default avatar of the webhook.
/// </summary>
public Optional<Image?> Image { get; set; }
}
}

+ 2
- 0
src/Discord.Net.Core/IDiscordClient.cs View File

@@ -34,5 +34,7 @@ namespace Discord

Task<IReadOnlyCollection<IVoiceRegion>> GetVoiceRegionsAsync(RequestOptions options = null);
Task<IVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null);

Task<IWebhook> GetWebhookAsync(ulong id, string webhookToken = null, RequestOptions options = null);
}
}

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

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

namespace Discord.API
{
internal class Webhook
{
[JsonProperty("id")]
public ulong Id { get; set; }
[JsonProperty("channel_id")]
public ulong ChannelId { get; set; }
[JsonProperty("token")]
public string Token { get; set; }

[JsonProperty("name")]
public Optional<string> Name { get; set; }
[JsonProperty("avatar")]
public Optional<string> Avatar { get; set; }
[JsonProperty("guild_id")]
public Optional<ulong> GuildId { get; set; }

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

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

@@ -8,6 +8,8 @@ namespace Discord.API.Rest
{
[JsonProperty("content")]
public string Content { get; }
[JsonProperty("wait")]
public bool ReturnCreatedMessage { get; set; }

[JsonProperty("nonce")]
public Optional<string> Nonce { get; set; }


+ 14
- 0
src/Discord.Net.Rest/API/Rest/CreateWebhookParams.cs View File

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

namespace Discord.API.Rest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
internal class CreateWebhookParams
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("avatar")]
public Optional<Image?> Avatar { get; set; }
}
}

+ 14
- 0
src/Discord.Net.Rest/API/Rest/ModifyWebhookParams.cs View File

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

namespace Discord.API.Rest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
internal class ModifyWebhookParams
{
[JsonProperty("name")]
public Optional<string> Name { get; set; }
[JsonProperty("avatar")]
public Optional<Image?> Avatar { get; set; }
}
}

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

@@ -164,6 +164,9 @@ namespace Discord.Rest
Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options)
=> Task.FromResult<IVoiceRegion>(null);

Task<IWebhook> IDiscordClient.GetWebhookAsync(ulong id, string webhookToken, RequestOptions options)
=> Task.FromResult<IWebhook>(null);

Task IDiscordClient.StartAsync()
=> Task.Delay(0);
Task IDiscordClient.StopAsync()


+ 8
- 0
src/Discord.Net.Rest/ClientHelper.cs View File

@@ -144,6 +144,14 @@ namespace Discord.Rest
return null;
}

public static async Task<RestWebhook> GetWebhookAsync(BaseDiscordClient client, ulong id, string webhookToken, RequestOptions options)
{
var model = await client.ApiClient.GetWebhookAsync(id, webhookToken);
if (model != null)
return RestWebhook.Create(client, (IGuild)null, model);
return null;
}

public static async Task<IReadOnlyCollection<RestVoiceRegion>> GetVoiceRegionsAsync(BaseDiscordClient client, RequestOptions options)
{
var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false);


+ 82
- 3
src/Discord.Net.Rest/DiscordRestApiClient.cs View File

@@ -473,11 +473,13 @@ namespace Discord.API
var ids = new BucketIds(channelId: channelId);
return await SendJsonAsync<Message>("POST", () => $"channels/{channelId}/messages", args, ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
}
public async Task CreateWebhookMessageAsync(ulong webhookId, CreateWebhookMessageParams args, RequestOptions options = null)
public async Task<Message> CreateWebhookMessageAsync(ulong webhookId, CreateWebhookMessageParams args, string webhookToken = null, RequestOptions options = null)
{
if (AuthTokenType != TokenType.Webhook)
if (AuthTokenType != TokenType.Webhook && string.IsNullOrWhiteSpace(webhookToken))
throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token.");

webhookToken = webhookToken ?? AuthToken;

Preconditions.NotNull(args, nameof(args));
Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
if (!args.Embeds.IsSpecified || args.Embeds.Value == null || args.Embeds.Value.Length == 0)
@@ -487,7 +489,11 @@ namespace Discord.API
throw new ArgumentException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content));
options = RequestOptions.CreateOrClone(options);

await SendJsonAsync("POST", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
if (args.ReturnCreatedMessage)
return await SendJsonAsync<Message>("POST", () => $"webhooks/{webhookId}/{webhookToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);

await SendJsonAsync("POST", () => $"webhooks/{webhookId}/{webhookToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
return null;
}
public async Task<Message> UploadFileAsync(ulong channelId, UploadFileParams args, RequestOptions options = null)
{
@@ -1154,6 +1160,79 @@ namespace Discord.API
return await SendAsync<IReadOnlyCollection<VoiceRegion>>("GET", () => $"guilds/{guildId}/regions", ids, options: options).ConfigureAwait(false);
}

//Webhooks
public async Task<Webhook> CreateWebhookAsync(ulong channelId, CreateWebhookParams args, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));
Preconditions.NotNull(args, nameof(args));
Preconditions.NotNull(args.Name, nameof(args.Name));
options = RequestOptions.CreateOrClone(options);

return await SendJsonAsync<Webhook>("POST", () => $"channels/{channelId}/webhooks", args, new BucketIds(), options: options);
}
public async Task<Webhook> GetWebhookAsync(ulong webhookId, string webhookToken = null, RequestOptions options = null)
{
Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
options = RequestOptions.CreateOrClone(options);

if (!string.IsNullOrWhiteSpace(webhookToken))
{
webhookToken = "/" + webhookToken;
options.IgnoreState = true;
}

try
{
return await SendAsync<Webhook>("GET", () => $"webhooks/{webhookId}{webhookToken}", new BucketIds(), options: options).ConfigureAwait(false);
}
catch (HttpException ex) when (ex.HttpCode == HttpStatusCode.NotFound) { return null; }
}
public async Task<Webhook> ModifyWebhookAsync(ulong webhookId, ModifyWebhookParams args, string webhookToken = null, RequestOptions options = null)
{
Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
Preconditions.NotNull(args, nameof(args));
Preconditions.NotNullOrEmpty(args.Name, nameof(args.Name));
options = RequestOptions.CreateOrClone(options);

if (!string.IsNullOrWhiteSpace(webhookToken))
{
webhookToken = "/" + webhookToken;
options.IgnoreState = true;
}

return await SendJsonAsync<Webhook>("PATCH", () => $"webhooks/{webhookId}{webhookToken}", args, new BucketIds(), options: options).ConfigureAwait(false);
}
public async Task DeleteWebhookAsync(ulong webhookId, string webhookToken = null, RequestOptions options = null)
{
Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
options = RequestOptions.CreateOrClone(options);
options.IgnoreState = true;

if (!string.IsNullOrWhiteSpace(webhookToken))
{
webhookToken = "/" + webhookToken;
options.IgnoreState = true;
}

await SendAsync("DELETE", () => $"webhooks/{webhookId}{webhookToken}", new BucketIds(), options: options).ConfigureAwait(false);
}
public async Task<IReadOnlyCollection<Webhook>> GetGuildWebhooksAsync(ulong guildId, RequestOptions options = null)
{
Preconditions.NotEqual(guildId, 0, nameof(guildId));
options = RequestOptions.CreateOrClone(options);

var ids = new BucketIds(guildId: guildId);
return await SendAsync<IReadOnlyCollection<Webhook>>("GET", () => $"guilds/{guildId}/webhooks", ids, options: options).ConfigureAwait(false);
}
public async Task<IReadOnlyCollection<Webhook>> GetChannelWebhooksAsync(ulong channelId, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));
options = RequestOptions.CreateOrClone(options);

var ids = new BucketIds(channelId: channelId);
return await SendAsync<IReadOnlyCollection<Webhook>>("GET", () => $"channels/{channelId}/webhooks", ids, options: options).ConfigureAwait(false);
}

//Helpers
protected void CheckState()
{


+ 6
- 0
src/Discord.Net.Rest/DiscordRestClient.cs View File

@@ -91,6 +91,9 @@ namespace Discord.Rest
/// <inheritdoc />
public Task<RestVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null)
=> ClientHelper.GetVoiceRegionAsync(this, id, options);
/// <inheritdoc />
public Task<RestWebhook> GetWebhookAsync(ulong id, string webhookToken = null, RequestOptions options = null)
=> ClientHelper.GetWebhookAsync(this, id, webhookToken, options);

//IDiscordClient
async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options)
@@ -160,5 +163,8 @@ namespace Discord.Rest
=> await GetVoiceRegionsAsync(options).ConfigureAwait(false);
async Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options)
=> await GetVoiceRegionAsync(id, options).ConfigureAwait(false);

async Task<IWebhook> IDiscordClient.GetWebhookAsync(ulong id, string webhookToken, RequestOptions options)
=> await GetWebhookAsync(id, webhookToken, options);
}
}

+ 25
- 0
src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.Channel;
using UserModel = Discord.API.User;
using WebhookModel = Discord.API.Webhook;

namespace Discord.Rest
{
@@ -280,6 +281,30 @@ namespace Discord.Rest
RequestOptions options)
=> new TypingNotifier(client, channel, options);

//Webhooks
public static async Task<RestWebhook> CreateWebhookAsync(ITextChannel channel, BaseDiscordClient client, string name, Stream avatar, RequestOptions options)
{
var args = new CreateWebhookParams { Name = name };
if (avatar != null)
args.Avatar = new API.Image(avatar);

var model = await client.ApiClient.CreateWebhookAsync(channel.Id, args, options).ConfigureAwait(false);
return RestWebhook.Create(client, channel, model);
}
public static async Task<RestWebhook> GetWebhookAsync(ITextChannel channel, BaseDiscordClient client, ulong id, RequestOptions options)
{
var model = await client.ApiClient.GetWebhookAsync(id, options: options).ConfigureAwait(false);
if (model == null)
return null;
return RestWebhook.Create(client, channel, model);
}
public static async Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(ITextChannel channel, BaseDiscordClient client, RequestOptions options)
{
var models = await client.ApiClient.GetChannelWebhooksAsync(channel.Id, options).ConfigureAwait(false);
return models.Select(x => RestWebhook.Create(client, channel, x))
.ToImmutableArray();
}

//Helpers
private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model, ulong? webhookId)
{


+ 15
- 0
src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs View File

@@ -77,8 +77,23 @@ namespace Discord.Rest
public IDisposable EnterTypingState(RequestOptions options = null)
=> ChannelHelper.EnterTypingState(this, Discord, options);

public Task<RestWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null)
=> ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options);
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
=> ChannelHelper.GetWebhookAsync(this, Discord, id, options);
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
=> ChannelHelper.GetWebhooksAsync(this, Discord, options);

private string DebuggerDisplay => $"{Name} ({Id}, Text)";

//ITextChannel
async Task<IWebhook> ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
=> await CreateWebhookAsync(name, avatar, options);
async Task<IWebhook> ITextChannel.GetWebhookAsync(ulong id, RequestOptions options)
=> await GetWebhookAsync(id, options);
async Task<IReadOnlyCollection<IWebhook>> ITextChannel.GetWebhooksAsync(RequestOptions options)
=> await GetWebhooksAsync(options);

//IMessageChannel
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
{


+ 14
- 0
src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs View File

@@ -253,5 +253,19 @@ namespace Discord.Rest
model = await client.ApiClient.BeginGuildPruneAsync(guild.Id, args, options).ConfigureAwait(false);
return model.Pruned;
}

//Webhooks
public static async Task<RestWebhook> GetWebhookAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options)
{
var model = await client.ApiClient.GetWebhookAsync(id, options: options).ConfigureAwait(false);
if (model == null)
return null;
return RestWebhook.Create(client, guild, model);
}
public static async Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(IGuild guild, BaseDiscordClient client, RequestOptions options)
{
var models = await client.ApiClient.GetGuildWebhooksAsync(guild.Id, options).ConfigureAwait(false);
return models.Select(x => RestWebhook.Create(client, guild, x)).ToImmutableArray();
}
}
}

+ 11
- 0
src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs View File

@@ -257,6 +257,12 @@ namespace Discord.Rest
public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null)
=> GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options);

//Webhooks
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
=> GuildHelper.GetWebhookAsync(this, Discord, id, options);
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
=> GuildHelper.GetWebhooksAsync(this, Discord, options);

public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";

@@ -386,5 +392,10 @@ namespace Discord.Rest
return ImmutableArray.Create<IGuildUser>();
}
Task IGuild.DownloadUsersAsync() { throw new NotSupportedException(); }

async Task<IWebhook> IGuild.GetWebhookAsync(ulong id, RequestOptions options)
=> await GetWebhookAsync(id, options);
async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions options)
=> await GetWebhooksAsync(options);
}
}

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

@@ -0,0 +1,89 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.Webhook;

namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestWebhook : RestEntity<ulong>, IWebhook, IUpdateable
{
internal IGuild Guild { get; }
internal ITextChannel Channel { get; }
public string Token { get; private set; }
public string Name { get; private set; }
public string AvatarId { get; private set; }
public ulong ChannelId { get; private set; }
public ulong GuildId { get; private set; }
public IUser Creator { get; private set; }

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

internal RestWebhook(BaseDiscordClient discord, IGuild guild, ulong id)
: base(discord, id)
{
Guild = guild;
}
internal RestWebhook(BaseDiscordClient discord, ITextChannel channel, ulong id)
: this(discord, channel.Guild, id)
{
Channel = channel;
}
internal static RestWebhook Create(BaseDiscordClient discord, IGuild guild, Model model)
{
var entity = new RestWebhook(discord, guild, model.Id);
entity.Update(model);
return entity;
}
internal static RestWebhook Create(BaseDiscordClient discord, ITextChannel channel, Model model)
{
var entity = new RestWebhook(discord, channel, model.Id);
entity.Update(model);
return entity;
}
internal void Update(Model model)
{
Token = model.Token;
ChannelId = model.ChannelId;
if (model.Avatar.IsSpecified)
AvatarId = model.Avatar.Value;
if (model.Creator.IsSpecified)
Creator = RestUser.Create(Discord, model.Creator.Value);
if (model.GuildId.IsSpecified)
GuildId = model.GuildId.Value;
if (model.Name.IsSpecified)
Name = model.Name.Value;
}

public async Task UpdateAsync(RequestOptions options = null)
{
var model = await Discord.ApiClient.GetWebhookAsync(Id, Token, options).ConfigureAwait(false);
Update(model);
}

public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetUserAvatarUrl(Id, AvatarId, size, format);

public async Task ModifyAsync(Action<WebhookProperties> func, string webhookToken = null, RequestOptions options = null)
{
var model = await WebhookHelper.ModifyAsync(this, Discord, func, webhookToken, options).ConfigureAwait(false);
Update(model);
}

public Task DeleteAsync(string webhookToken = null, RequestOptions options = null)
=> WebhookHelper.DeleteAsync(this, Discord, webhookToken, options);

public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";

//IWebhook
IGuild IWebhook.Guild
=> Guild ?? throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
ITextChannel IWebhook.Channel
=> Channel ?? throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
Task IWebhook.ModifyAsync(Action<WebhookProperties> func, string webhookToken, RequestOptions options)
=> ModifyAsync(func, webhookToken, options);
Task IDeletable.DeleteAsync(RequestOptions options)
=> DeleteAsync(Token, options);
}
}

+ 34
- 0
src/Discord.Net.Rest/Entities/Webhooks/WebhookHelper.cs View File

@@ -0,0 +1,34 @@
using Discord.API.Rest;
using System;
using System.Threading.Tasks;
using ImageModel = Discord.API.Image;
using Model = Discord.API.Webhook;

namespace Discord.Rest
{
internal static class WebhookHelper
{
public static async Task<Model> ModifyAsync(IWebhook webhook, BaseDiscordClient client,
Action<WebhookProperties> func, string webhookToken, RequestOptions options)
{
var args = new WebhookProperties();
func(args);
var apiArgs = new ModifyWebhookParams
{
Avatar = args.Image.IsSpecified ? args.Image.Value?.ToModel() : Optional.Create<ImageModel?>(),
Name = args.Name
};

if (!apiArgs.Avatar.IsSpecified && webhook.AvatarId != null)
apiArgs.Avatar = new ImageModel(webhook.AvatarId);

return await client.ApiClient.ModifyWebhookAsync(webhook.Id, apiArgs, webhookToken, options).ConfigureAwait(false);
}
public static async Task DeleteAsync(IWebhook webhook, BaseDiscordClient client, string webhookToken,
RequestOptions options)
{
await client.ApiClient.DeleteWebhookAsync(webhook.Id, webhookToken, options).ConfigureAwait(false);
}

}
}

+ 15
- 1
src/Discord.Net.Rpc/Entities/Channels/RpcTextChannel.cs View File

@@ -68,11 +68,25 @@ namespace Discord.Rpc
=> ChannelHelper.TriggerTypingAsync(this, Discord, options);
public IDisposable EnterTypingState(RequestOptions options = null)
=> ChannelHelper.EnterTypingState(this, Discord, options);

//Webhooks
public Task<RestWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null)
=> ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options);
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
=> ChannelHelper.GetWebhookAsync(this, Discord, id, options);
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
=> ChannelHelper.GetWebhooksAsync(this, Discord, options);

private string DebuggerDisplay => $"{Name} ({Id}, Text)";
//ITextChannel
string ITextChannel.Topic { get { throw new NotSupportedException(); } }
async Task<IWebhook> ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
=> await CreateWebhookAsync(name, avatar, options);
async Task<IWebhook> ITextChannel.GetWebhookAsync(ulong id, RequestOptions options)
=> await GetWebhookAsync(id, options);
async Task<IReadOnlyCollection<IWebhook>> ITextChannel.GetWebhooksAsync(RequestOptions options)
=> await GetWebhooksAsync(options);

//IMessageChannel
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)


+ 13
- 0
src/Discord.Net.WebSocket/API/Gateway/WebhookUpdateEvent.cs View File

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

namespace Discord.API.Gateway
{
internal class WebhookUpdateEvent
{
[JsonProperty("guild_id")]
public ulong GuildId { get; set; }
[JsonProperty("channel_id")]
public ulong ChannelId { get; set; }
}
}

+ 17
- 1
src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs View File

@@ -112,10 +112,26 @@ namespace Discord.WebSocket
}
return null;
}

//Webhooks
public Task<RestWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null)
=> ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options);
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
=> ChannelHelper.GetWebhookAsync(this, Discord, id, options);
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
=> ChannelHelper.GetWebhooksAsync(this, Discord, options);

private string DebuggerDisplay => $"{Name} ({Id}, Text)";
internal new SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel;

//ITextChannel
async Task<IWebhook> ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
=> await CreateWebhookAsync(name, avatar, options);
async Task<IWebhook> ITextChannel.GetWebhookAsync(ulong id, RequestOptions options)
=> await GetWebhookAsync(id, options);
async Task<IReadOnlyCollection<IWebhook>> ITextChannel.GetWebhooksAsync(RequestOptions options)
=> await GetWebhooksAsync(options);

//IGuildChannel
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IGuildUser>(GetUser(id));


+ 11
- 0
src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs View File

@@ -433,6 +433,12 @@ namespace Discord.WebSocket
_downloaderPromise.TrySetResultAsync(true);
}

//Webhooks
public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
=> GuildHelper.GetWebhookAsync(this, Discord, id, options);
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
=> GuildHelper.GetWebhooksAsync(this, Discord, options);

//Voice States
internal async Task<SocketVoiceState> AddOrUpdateVoiceStateAsync(ClientState state, VoiceStateModel model)
{
@@ -672,5 +678,10 @@ namespace Discord.WebSocket
Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IGuildUser>(Owner);
Task IGuild.DownloadUsersAsync() { throw new NotSupportedException(); }

async Task<IWebhook> IGuild.GetWebhookAsync(ulong id, RequestOptions options)
=> await GetWebhookAsync(id, options);
async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions options)
=> await GetWebhooksAsync(options);
}
}

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

@@ -56,7 +56,7 @@ namespace Discord.Webhook
args.Username = username;
if (avatarUrl != null)
args.AvatarUrl = avatarUrl;
await ApiClient.CreateWebhookMessageAsync(_webhookId, args, options).ConfigureAwait(false);
await ApiClient.CreateWebhookMessageAsync(_webhookId, args, options: options).ConfigureAwait(false);
}

#if FILESYSTEM


Loading…
Cancel
Save