diff --git a/src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs b/src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs index 4a8bb309b..3e91e6875 100644 --- a/src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs +++ b/src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs @@ -29,6 +29,6 @@ namespace Discord IUser Creator { get; } /// Modifies this webhook. - Task ModifyAsync(Action func, string webhookToken = null, RequestOptions options = null); + Task ModifyAsync(Action func, RequestOptions options = null); } } diff --git a/src/Discord.Net.Rest/API/Rest/CreateWebhookMessageParams.cs b/src/Discord.Net.Rest/API/Rest/CreateWebhookMessageParams.cs index 279f538bd..b9c77279f 100644 --- a/src/Discord.Net.Rest/API/Rest/CreateWebhookMessageParams.cs +++ b/src/Discord.Net.Rest/API/Rest/CreateWebhookMessageParams.cs @@ -9,7 +9,7 @@ namespace Discord.API.Rest [JsonProperty("content")] public string Content { get; } [JsonProperty("wait")] - public bool ReturnCreatedMessage { get; set; } + public bool ReturnCreatedMessage { get; set; } = true; [JsonProperty("nonce")] public Optional Nonce { get; set; } diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs index e34ce2603..07c20794a 100644 --- a/src/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs @@ -472,13 +472,11 @@ namespace Discord.API var ids = new BucketIds(channelId: channelId); return await SendJsonAsync("POST", () => $"channels/{channelId}/messages", args, ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); } - public async Task CreateWebhookMessageAsync(ulong webhookId, CreateWebhookMessageParams args, string webhookToken = null, RequestOptions options = null) + public async Task 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."); - 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) @@ -489,9 +487,9 @@ namespace Discord.API options = RequestOptions.CreateOrClone(options); if (args.ReturnCreatedMessage) - return await SendJsonAsync("POST", () => $"webhooks/{webhookId}/{webhookToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false); + return await SendJsonAsync("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; } public async Task UploadFileAsync(ulong channelId, UploadFileParams args, RequestOptions options = null) @@ -1169,51 +1167,41 @@ namespace Discord.API return await SendJsonAsync("POST", () => $"channels/{channelId}/webhooks", args, new BucketIds(), options: options); } - public async Task GetWebhookAsync(ulong webhookId, string webhookToken = null, RequestOptions options = null) + public async Task GetWebhookAsync(ulong webhookId, 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("GET", () => $"webhooks/{webhookId}{webhookToken}", new BucketIds(), options: options).ConfigureAwait(false); + if (AuthTokenType == TokenType.Webhook) + return await SendAsync("GET", () => $"webhooks/{webhookId}/{AuthToken}", new BucketIds(), options: options).ConfigureAwait(false); + else + return await SendAsync("GET", () => $"webhooks/{webhookId}", new BucketIds(), options: options).ConfigureAwait(false); } catch (HttpException ex) when (ex.HttpCode == HttpStatusCode.NotFound) { return null; } } - public async Task ModifyWebhookAsync(ulong webhookId, ModifyWebhookParams args, string webhookToken = null, RequestOptions options = null) + public async Task ModifyWebhookAsync(ulong webhookId, ModifyWebhookParams args, 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("PATCH", () => $"webhooks/{webhookId}{webhookToken}", args, new BucketIds(), options: options).ConfigureAwait(false); + + if (AuthTokenType == TokenType.Webhook) + return await SendJsonAsync("PATCH", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), options: options).ConfigureAwait(false); + else + return await SendJsonAsync("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)); 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> GetGuildWebhooksAsync(ulong guildId, RequestOptions options = null) { diff --git a/src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs b/src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs index c93479c89..6fe277d23 100644 --- a/src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs +++ b/src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs @@ -8,8 +8,8 @@ namespace Discord.Rest [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class RestWebhook : RestEntity, 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 Name { get; private set; } public string AvatarId { get; private set; } @@ -57,21 +57,29 @@ namespace Discord.Rest 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); } public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) => CDN.GetUserAvatarUrl(Id, AvatarId, size, format); - public async Task ModifyAsync(Action func, string webhookToken = null, RequestOptions options = null) + public async Task ModifyAsync(Action 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); } - 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 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; 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."); 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 func, string webhookToken, RequestOptions options) - => ModifyAsync(func, webhookToken, options); - Task IDeletable.DeleteAsync(RequestOptions options) - => DeleteAsync(Token, options); + Task IWebhook.ModifyAsync(Action func, RequestOptions options) + => ModifyAsync(func, options); } } diff --git a/src/Discord.Net.Rest/Entities/Webhooks/WebhookHelper.cs b/src/Discord.Net.Rest/Entities/Webhooks/WebhookHelper.cs index 0bd518dab..1c1481661 100644 --- a/src/Discord.Net.Rest/Entities/Webhooks/WebhookHelper.cs +++ b/src/Discord.Net.Rest/Entities/Webhooks/WebhookHelper.cs @@ -9,7 +9,7 @@ namespace Discord.Rest internal static class WebhookHelper { public static async Task ModifyAsync(IWebhook webhook, BaseDiscordClient client, - Action func, string webhookToken, RequestOptions options) + Action func, RequestOptions options) { var args = new WebhookProperties(); func(args); @@ -22,12 +22,11 @@ namespace Discord.Rest 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); + 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); } } diff --git a/src/Discord.Net.Webhook/DiscordWebhookClient.cs b/src/Discord.Net.Webhook/DiscordWebhookClient.cs index beae73461..649e79306 100644 --- a/src/Discord.Net.Webhook/DiscordWebhookClient.cs +++ b/src/Discord.Net.Webhook/DiscordWebhookClient.cs @@ -19,6 +19,9 @@ namespace Discord.Webhook internal API.DiscordRestApiClient ApiClient { get; } internal LogManager LogManager { get; } + /// Creates a new Webhook discord client. + public DiscordWebhookClient(IWebhook webhook) + : this(webhook.Id, webhook.Token) { } /// Creates a new Webhook discord client. public DiscordWebhookClient(ulong webhookId, string webhookToken) : this(webhookId, webhookToken, new DiscordRestConfig()) { }