Leaving that as a Webhook package only feature.pull/843/head
@@ -29,6 +29,6 @@ namespace Discord | |||||
IUser Creator { get; } | IUser Creator { get; } | ||||
/// <summary> Modifies this webhook. </summary> | /// <summary> Modifies this webhook. </summary> | ||||
Task ModifyAsync(Action<WebhookProperties> func, string webhookToken = null, RequestOptions options = null); | |||||
Task ModifyAsync(Action<WebhookProperties> func, RequestOptions options = null); | |||||
} | } | ||||
} | } |
@@ -9,7 +9,7 @@ namespace Discord.API.Rest | |||||
[JsonProperty("content")] | [JsonProperty("content")] | ||||
public string Content { get; } | public string Content { get; } | ||||
[JsonProperty("wait")] | [JsonProperty("wait")] | ||||
public bool ReturnCreatedMessage { get; set; } | |||||
public bool ReturnCreatedMessage { get; set; } = true; | |||||
[JsonProperty("nonce")] | [JsonProperty("nonce")] | ||||
public Optional<string> Nonce { get; set; } | public Optional<string> Nonce { get; set; } | ||||
@@ -472,13 +472,11 @@ namespace Discord.API | |||||
var ids = new BucketIds(channelId: channelId); | var ids = new BucketIds(channelId: channelId); | ||||
return await SendJsonAsync<Message>("POST", () => $"channels/{channelId}/messages", args, ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | return await SendJsonAsync<Message>("POST", () => $"channels/{channelId}/messages", args, ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | ||||
} | } | ||||
public async Task<Message> CreateWebhookMessageAsync(ulong webhookId, CreateWebhookMessageParams args, string webhookToken = null, RequestOptions options = null) | |||||
public async Task<Message> CreateWebhookMessageAsync(ulong webhookId, CreateWebhookMessageParams args, RequestOptions options = null) | |||||
{ | { | ||||
if (AuthTokenType != TokenType.Webhook && string.IsNullOrWhiteSpace(webhookToken)) | |||||
if (AuthTokenType != TokenType.Webhook) | |||||
throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token."); | throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token."); | ||||
webhookToken = webhookToken ?? AuthToken; | |||||
Preconditions.NotNull(args, nameof(args)); | Preconditions.NotNull(args, nameof(args)); | ||||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | ||||
if (!args.Embeds.IsSpecified || args.Embeds.Value == null || args.Embeds.Value.Length == 0) | if (!args.Embeds.IsSpecified || args.Embeds.Value == null || args.Embeds.Value.Length == 0) | ||||
@@ -489,9 +487,9 @@ namespace Discord.API | |||||
options = RequestOptions.CreateOrClone(options); | options = RequestOptions.CreateOrClone(options); | ||||
if (args.ReturnCreatedMessage) | if (args.ReturnCreatedMessage) | ||||
return await SendJsonAsync<Message>("POST", () => $"webhooks/{webhookId}/{webhookToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | |||||
return await SendJsonAsync<Message>("POST", () => $"webhooks/{webhookId}/{AuthToken}", 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); | |||||
await SendJsonAsync("POST", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); | |||||
return null; | return null; | ||||
} | } | ||||
public async Task<Message> UploadFileAsync(ulong channelId, UploadFileParams args, RequestOptions options = null) | public async Task<Message> UploadFileAsync(ulong channelId, UploadFileParams args, RequestOptions options = null) | ||||
@@ -1169,51 +1167,41 @@ namespace Discord.API | |||||
return await SendJsonAsync<Webhook>("POST", () => $"channels/{channelId}/webhooks", args, new BucketIds(), options: 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) | |||||
public async Task<Webhook> GetWebhookAsync(ulong webhookId, RequestOptions options = null) | |||||
{ | { | ||||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | ||||
options = RequestOptions.CreateOrClone(options); | options = RequestOptions.CreateOrClone(options); | ||||
if (!string.IsNullOrWhiteSpace(webhookToken)) | |||||
{ | |||||
webhookToken = "/" + webhookToken; | |||||
options.IgnoreState = true; | |||||
} | |||||
try | try | ||||
{ | { | ||||
return await SendAsync<Webhook>("GET", () => $"webhooks/{webhookId}{webhookToken}", new BucketIds(), options: options).ConfigureAwait(false); | |||||
if (AuthTokenType == TokenType.Webhook) | |||||
return await SendAsync<Webhook>("GET", () => $"webhooks/{webhookId}/{AuthToken}", new BucketIds(), options: options).ConfigureAwait(false); | |||||
else | |||||
return await SendAsync<Webhook>("GET", () => $"webhooks/{webhookId}", new BucketIds(), options: options).ConfigureAwait(false); | |||||
} | } | ||||
catch (HttpException ex) when (ex.HttpCode == HttpStatusCode.NotFound) { return null; } | 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) | |||||
public async Task<Webhook> ModifyWebhookAsync(ulong webhookId, ModifyWebhookParams args, RequestOptions options = null) | |||||
{ | { | ||||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | ||||
Preconditions.NotNull(args, nameof(args)); | Preconditions.NotNull(args, nameof(args)); | ||||
Preconditions.NotNullOrEmpty(args.Name, nameof(args.Name)); | Preconditions.NotNullOrEmpty(args.Name, nameof(args.Name)); | ||||
options = RequestOptions.CreateOrClone(options); | 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); | |||||
if (AuthTokenType == TokenType.Webhook) | |||||
return await SendJsonAsync<Webhook>("PATCH", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), options: options).ConfigureAwait(false); | |||||
else | |||||
return await SendJsonAsync<Webhook>("PATCH", () => $"webhooks/{webhookId}", args, new BucketIds(), options: options).ConfigureAwait(false); | |||||
} | } | ||||
public async Task DeleteWebhookAsync(ulong webhookId, string webhookToken = null, RequestOptions options = null) | |||||
public async Task DeleteWebhookAsync(ulong webhookId, RequestOptions options = null) | |||||
{ | { | ||||
Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | Preconditions.NotEqual(webhookId, 0, nameof(webhookId)); | ||||
options = RequestOptions.CreateOrClone(options); | 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); | |||||
if (AuthTokenType == TokenType.Webhook) | |||||
await SendAsync("DELETE", () => $"webhooks/{webhookId}/{AuthToken}", new BucketIds(), options: options).ConfigureAwait(false); | |||||
else | |||||
await SendAsync("DELETE", () => $"webhooks/{webhookId}", new BucketIds(), options: options).ConfigureAwait(false); | |||||
} | } | ||||
public async Task<IReadOnlyCollection<Webhook>> GetGuildWebhooksAsync(ulong guildId, RequestOptions options = null) | public async Task<IReadOnlyCollection<Webhook>> GetGuildWebhooksAsync(ulong guildId, RequestOptions options = null) | ||||
{ | { | ||||
@@ -8,8 +8,8 @@ namespace Discord.Rest | |||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")] | [DebuggerDisplay(@"{DebuggerDisplay,nq}")] | ||||
public class RestWebhook : RestEntity<ulong>, IWebhook, IUpdateable | public class RestWebhook : RestEntity<ulong>, IWebhook, IUpdateable | ||||
{ | { | ||||
internal IGuild Guild { get; } | |||||
internal ITextChannel Channel { get; } | |||||
internal IGuild Guild { get; private set; } | |||||
internal ITextChannel Channel { get; private set; } | |||||
public string Token { get; private set; } | public string Token { get; private set; } | ||||
public string Name { get; private set; } | public string Name { get; private set; } | ||||
public string AvatarId { get; private set; } | public string AvatarId { get; private set; } | ||||
@@ -57,21 +57,29 @@ namespace Discord.Rest | |||||
public async Task UpdateAsync(RequestOptions options = null) | public async Task UpdateAsync(RequestOptions options = null) | ||||
{ | { | ||||
var model = await Discord.ApiClient.GetWebhookAsync(Id, Token, options).ConfigureAwait(false); | |||||
var model = await Discord.ApiClient.GetWebhookAsync(Id, options).ConfigureAwait(false); | |||||
Update(model); | Update(model); | ||||
} | } | ||||
public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) | public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) | ||||
=> CDN.GetUserAvatarUrl(Id, AvatarId, size, format); | => CDN.GetUserAvatarUrl(Id, AvatarId, size, format); | ||||
public async Task ModifyAsync(Action<WebhookProperties> func, string webhookToken = null, RequestOptions options = null) | |||||
public async Task ModifyAsync(Action<WebhookProperties> func, RequestOptions options = null) | |||||
{ | { | ||||
var model = await WebhookHelper.ModifyAsync(this, Discord, func, webhookToken, options).ConfigureAwait(false); | |||||
var model = await WebhookHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false); | |||||
Update(model); | Update(model); | ||||
} | } | ||||
public Task DeleteAsync(string webhookToken = null, RequestOptions options = null) | |||||
=> WebhookHelper.DeleteAsync(this, Discord, webhookToken, options); | |||||
public Task DeleteAsync(RequestOptions options = null) | |||||
=> WebhookHelper.DeleteAsync(this, Discord, options); | |||||
public async Task<RestTextChannel> GetChannelAsync(RequestOptions options = null) | |||||
{ | |||||
Channel = await ClientHelper.GetChannelAsync(Discord, ChannelId, options) as RestTextChannel; | |||||
Guild = Channel.Guild; | |||||
return Channel as RestTextChannel; | |||||
} | |||||
public override string ToString() => Name; | public override string ToString() => Name; | ||||
private string DebuggerDisplay => $"{Name} ({Id})"; | private string DebuggerDisplay => $"{Name} ({Id})"; | ||||
@@ -81,9 +89,7 @@ namespace Discord.Rest | |||||
=> Guild ?? throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object."); | => Guild ?? throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object."); | ||||
ITextChannel IWebhook.Channel | ITextChannel IWebhook.Channel | ||||
=> Channel ?? throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object."); | => 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); | |||||
Task IWebhook.ModifyAsync(Action<WebhookProperties> func, RequestOptions options) | |||||
=> ModifyAsync(func, options); | |||||
} | } | ||||
} | } |
@@ -9,7 +9,7 @@ namespace Discord.Rest | |||||
internal static class WebhookHelper | internal static class WebhookHelper | ||||
{ | { | ||||
public static async Task<Model> ModifyAsync(IWebhook webhook, BaseDiscordClient client, | public static async Task<Model> ModifyAsync(IWebhook webhook, BaseDiscordClient client, | ||||
Action<WebhookProperties> func, string webhookToken, RequestOptions options) | |||||
Action<WebhookProperties> func, RequestOptions options) | |||||
{ | { | ||||
var args = new WebhookProperties(); | var args = new WebhookProperties(); | ||||
func(args); | func(args); | ||||
@@ -22,12 +22,11 @@ namespace Discord.Rest | |||||
if (!apiArgs.Avatar.IsSpecified && webhook.AvatarId != null) | if (!apiArgs.Avatar.IsSpecified && webhook.AvatarId != null) | ||||
apiArgs.Avatar = new ImageModel(webhook.AvatarId); | apiArgs.Avatar = new ImageModel(webhook.AvatarId); | ||||
return await client.ApiClient.ModifyWebhookAsync(webhook.Id, apiArgs, webhookToken, options).ConfigureAwait(false); | |||||
return await client.ApiClient.ModifyWebhookAsync(webhook.Id, apiArgs, options).ConfigureAwait(false); | |||||
} | } | ||||
public static async Task DeleteAsync(IWebhook webhook, BaseDiscordClient client, string webhookToken, | |||||
RequestOptions options) | |||||
public static async Task DeleteAsync(IWebhook webhook, BaseDiscordClient client, RequestOptions options) | |||||
{ | { | ||||
await client.ApiClient.DeleteWebhookAsync(webhook.Id, webhookToken, options).ConfigureAwait(false); | |||||
await client.ApiClient.DeleteWebhookAsync(webhook.Id, options).ConfigureAwait(false); | |||||
} | } | ||||
} | } | ||||
@@ -19,6 +19,9 @@ namespace Discord.Webhook | |||||
internal API.DiscordRestApiClient ApiClient { get; } | internal API.DiscordRestApiClient ApiClient { get; } | ||||
internal LogManager LogManager { get; } | internal LogManager LogManager { get; } | ||||
/// <summary> Creates a new Webhook discord client. </summary> | |||||
public DiscordWebhookClient(IWebhook webhook) | |||||
: this(webhook.Id, webhook.Token) { } | |||||
/// <summary> Creates a new Webhook discord client. </summary> | /// <summary> Creates a new Webhook discord client. </summary> | ||||
public DiscordWebhookClient(ulong webhookId, string webhookToken) | public DiscordWebhookClient(ulong webhookId, string webhookToken) | ||||
: this(webhookId, webhookToken, new DiscordRestConfig()) { } | : this(webhookId, webhookToken, new DiscordRestConfig()) { } | ||||