From c950106fed1ce50f9ab95ca0c4079ebfcecffb54 Mon Sep 17 00:00:00 2001 From: Misha133 <61027276+Misha-133@users.noreply.github.com> Date: Fri, 31 Mar 2023 14:24:55 +0300 Subject: [PATCH] [Fix] Allow creating stickers with no description (#2628) * fix `CreateStickerAsync` methods * Update src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs Co-authored-by: Dmitry * fix some parameter names in precontion checks --------- Co-authored-by: Dmitry --- .../Entities/Guilds/IGuild.cs | 6 +-- .../API/Rest/CreateStickerParams.cs | 6 ++- .../Entities/Guilds/GuildHelper.cs | 42 +++++++++++++------ .../Entities/Guilds/RestGuild.cs | 26 ++++++------ .../Entities/Guilds/SocketGuild.cs | 27 ++++++------ 5 files changed, 64 insertions(+), 43 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs index dbee0835..66641b8e 100644 --- a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs +++ b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs @@ -1104,7 +1104,7 @@ namespace Discord /// /// A task that represents the asynchronous creation operation. The task result contains the created sticker. /// - Task CreateStickerAsync(string name, string description, IEnumerable tags, Image image, RequestOptions options = null); + Task CreateStickerAsync(string name, Image image, IEnumerable tags, string description = null, RequestOptions options = null); /// /// Creates a new sticker in this guild. @@ -1117,7 +1117,7 @@ namespace Discord /// /// A task that represents the asynchronous creation operation. The task result contains the created sticker. /// - Task CreateStickerAsync(string name, string description, IEnumerable tags, string path, RequestOptions options = null); + Task CreateStickerAsync(string name, string path, IEnumerable tags, string description = null, RequestOptions options = null); /// /// Creates a new sticker in this guild. @@ -1131,7 +1131,7 @@ namespace Discord /// /// A task that represents the asynchronous creation operation. The task result contains the created sticker. /// - Task CreateStickerAsync(string name, string description, IEnumerable tags, Stream stream, string filename, RequestOptions options = null); + Task CreateStickerAsync(string name, Stream stream, string filename, IEnumerable tags, string description = null, RequestOptions options = null); /// /// Gets a specific sticker within this guild. diff --git a/src/Discord.Net.Rest/API/Rest/CreateStickerParams.cs b/src/Discord.Net.Rest/API/Rest/CreateStickerParams.cs index a0871bc6..5ad8abed 100644 --- a/src/Discord.Net.Rest/API/Rest/CreateStickerParams.cs +++ b/src/Discord.Net.Rest/API/Rest/CreateStickerParams.cs @@ -17,10 +17,14 @@ namespace Discord.API.Rest var d = new Dictionary { ["name"] = $"{Name}", - ["description"] = Description, ["tags"] = Tags }; + if (Description is not null) + d["description"] = Description; + else + d["description"] = string.Empty; + string contentType; if (File is FileStream fileStream) { diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs index 32357f1c..0a7d2a07 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs @@ -708,49 +708,67 @@ namespace Discord.Rest public static Task DeleteEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options) => client.ApiClient.DeleteGuildEmoteAsync(guild.Id, id, options); - public static async Task CreateStickerAsync(BaseDiscordClient client, IGuild guild, string name, string description, IEnumerable tags, - Image image, RequestOptions options = null) + public static async Task CreateStickerAsync(BaseDiscordClient client, IGuild guild, string name, Image image, IEnumerable tags, + string description = null, RequestOptions options = null) { Preconditions.NotNull(name, nameof(name)); - Preconditions.NotNull(description, nameof(description)); + + if (description is not null) + { + Preconditions.AtLeast(description.Length, 2, nameof(description)); + Preconditions.AtMost(description.Length, 100, nameof(description)); + } + + var tagString = string.Join(", ", tags); + + Preconditions.AtLeast(tagString.Length, 1, nameof(tags)); + Preconditions.AtMost(tagString.Length, 200, nameof(tags)); + Preconditions.AtLeast(name.Length, 2, nameof(name)); - Preconditions.AtLeast(description.Length, 2, nameof(description)); Preconditions.AtMost(name.Length, 30, nameof(name)); - Preconditions.AtMost(description.Length, 100, nameof(name)); var apiArgs = new CreateStickerParams() { Name = name, Description = description, File = image.Stream, - Tags = string.Join(", ", tags) + Tags = tagString }; return await client.ApiClient.CreateGuildStickerAsync(apiArgs, guild.Id, options).ConfigureAwait(false); } - public static async Task CreateStickerAsync(BaseDiscordClient client, IGuild guild, string name, string description, IEnumerable tags, - Stream file, string filename, RequestOptions options = null) + public static async Task CreateStickerAsync(BaseDiscordClient client, IGuild guild, string name, Stream file, string filename, IEnumerable tags, + string description = null, RequestOptions options = null) { Preconditions.NotNull(name, nameof(name)); - Preconditions.NotNull(description, nameof(description)); Preconditions.NotNull(file, nameof(file)); Preconditions.NotNull(filename, nameof(filename)); Preconditions.AtLeast(name.Length, 2, nameof(name)); - Preconditions.AtLeast(description.Length, 2, nameof(description)); Preconditions.AtMost(name.Length, 30, nameof(name)); - Preconditions.AtMost(description.Length, 100, nameof(name)); + + + if (description is not null) + { + Preconditions.AtLeast(description.Length, 2, nameof(description)); + Preconditions.AtMost(description.Length, 100, nameof(description)); + } + + var tagString = string.Join(", ", tags); + + Preconditions.AtLeast(tagString.Length, 1, nameof(tags)); + Preconditions.AtMost(tagString.Length, 200, nameof(tags)); var apiArgs = new CreateStickerParams() { Name = name, Description = description, File = file, - Tags = string.Join(", ", tags), + Tags = tagString, FileName = filename }; diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs index e35f1bc9..c534af73 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs @@ -1051,9 +1051,9 @@ namespace Discord.Rest /// /// A task that represents the asynchronous creation operation. The task result contains the created sticker. /// - public async Task CreateStickerAsync(string name, string description, IEnumerable tags, Image image, RequestOptions options = null) + public async Task CreateStickerAsync(string name, Image image, IEnumerable tags, string description = null, RequestOptions options = null) { - var model = await GuildHelper.CreateStickerAsync(Discord, this, name, description, tags, image, options).ConfigureAwait(false); + var model = await GuildHelper.CreateStickerAsync(Discord, this, name, image, tags, description, options).ConfigureAwait(false); return CustomSticker.Create(Discord, model, this, model.User.IsSpecified ? model.User.Value.Id : null); } @@ -1068,11 +1068,11 @@ namespace Discord.Rest /// /// A task that represents the asynchronous creation operation. The task result contains the created sticker. /// - public Task CreateStickerAsync(string name, string description, IEnumerable tags, string path, + public Task CreateStickerAsync(string name, string path, IEnumerable tags, string description = null, RequestOptions options = null) { var fs = File.OpenRead(path); - return CreateStickerAsync(name, description, tags, fs, Path.GetFileName(fs.Name), options); + return CreateStickerAsync(name, fs, Path.GetFileName(fs.Name), tags, description,options); } /// /// Creates a new sticker in this guild @@ -1086,10 +1086,10 @@ namespace Discord.Rest /// /// A task that represents the asynchronous creation operation. The task result contains the created sticker. /// - public async Task CreateStickerAsync(string name, string description, IEnumerable tags, Stream stream, - string filename, RequestOptions options = null) + public async Task CreateStickerAsync(string name, Stream stream, string filename, IEnumerable tags, + string description = null, RequestOptions options = null) { - var model = await GuildHelper.CreateStickerAsync(Discord, this, name, description, tags, stream, filename, options).ConfigureAwait(false); + var model = await GuildHelper.CreateStickerAsync(Discord, this, name, stream, filename, tags, description, options).ConfigureAwait(false); return CustomSticker.Create(Discord, model, this, model.User.IsSpecified ? model.User.Value.Id : null); } @@ -1527,14 +1527,14 @@ namespace Discord.Rest async Task> IGuild.GetApplicationCommandsAsync(bool withLocalizations, string locale, RequestOptions options) => await GetApplicationCommandsAsync(withLocalizations, locale, options).ConfigureAwait(false); /// - async Task IGuild.CreateStickerAsync(string name, string description, IEnumerable tags, Image image, RequestOptions options) - => await CreateStickerAsync(name, description, tags, image, options); + async Task IGuild.CreateStickerAsync(string name, Image image, IEnumerable tags, string description, RequestOptions options) + => await CreateStickerAsync(name, image, tags, description, options); /// - async Task IGuild.CreateStickerAsync(string name, string description, IEnumerable tags, Stream stream, string filename, RequestOptions options) - => await CreateStickerAsync(name, description, tags, stream, filename, options); + async Task IGuild.CreateStickerAsync(string name, Stream stream, string filename, IEnumerable tags, string description, RequestOptions options) + => await CreateStickerAsync(name, stream, filename, tags, description, options); /// - async Task IGuild.CreateStickerAsync(string name, string description, IEnumerable tags, string path, RequestOptions options) - => await CreateStickerAsync(name, description, tags, path, options); + async Task IGuild.CreateStickerAsync(string name, string path, IEnumerable tags, string description, RequestOptions options) + => await CreateStickerAsync(name, path, tags, description, options); /// async Task IGuild.GetStickerAsync(ulong id, CacheMode mode, RequestOptions options) { diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index 9f5cf4d1..ab1d5fcc 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -1540,10 +1540,10 @@ namespace Discord.WebSocket /// /// A task that represents the asynchronous creation operation. The task result contains the created sticker. /// - public async Task CreateStickerAsync(string name, string description, IEnumerable tags, Image image, + public async Task CreateStickerAsync(string name, Image image, IEnumerable tags, string description = null, RequestOptions options = null) { - var model = await GuildHelper.CreateStickerAsync(Discord, this, name, description, tags, image, options).ConfigureAwait(false); + var model = await GuildHelper.CreateStickerAsync(Discord, this, name, image, tags, description, options).ConfigureAwait(false); return AddOrUpdateSticker(model); } @@ -1558,11 +1558,11 @@ namespace Discord.WebSocket /// /// A task that represents the asynchronous creation operation. The task result contains the created sticker. /// - public Task CreateStickerAsync(string name, string description, IEnumerable tags, string path, + public Task CreateStickerAsync(string name, string path, IEnumerable tags, string description = null, RequestOptions options = null) { var fs = File.OpenRead(path); - return CreateStickerAsync(name, description, tags, fs, Path.GetFileName(fs.Name), options); + return CreateStickerAsync(name, fs, Path.GetFileName(fs.Name), tags, description, options); } /// /// Creates a new sticker in this guild @@ -1576,10 +1576,10 @@ namespace Discord.WebSocket /// /// A task that represents the asynchronous creation operation. The task result contains the created sticker. /// - public async Task CreateStickerAsync(string name, string description, IEnumerable tags, Stream stream, - string filename, RequestOptions options = null) + public async Task CreateStickerAsync(string name, Stream stream, string filename, IEnumerable tags, string description = null, + RequestOptions options = null) { - var model = await GuildHelper.CreateStickerAsync(Discord, this, name, description, tags, stream, filename, options).ConfigureAwait(false); + var model = await GuildHelper.CreateStickerAsync(Discord, this, name, stream, filename, tags, description, options).ConfigureAwait(false); return AddOrUpdateSticker(model); } @@ -2111,15 +2111,14 @@ namespace Discord.WebSocket /// async Task> IGuild.GetApplicationCommandsAsync(bool withLocalizations, string locale, RequestOptions options) => await GetApplicationCommandsAsync(withLocalizations, locale, options).ConfigureAwait(false); + async Task IGuild.CreateStickerAsync(string name, Image image, IEnumerable tags, string description, RequestOptions options) + => await CreateStickerAsync(name, image, tags, description, options); /// - async Task IGuild.CreateStickerAsync(string name, string description, IEnumerable tags, Image image, RequestOptions options) - => await CreateStickerAsync(name, description, tags, image, options); + async Task IGuild.CreateStickerAsync(string name, Stream stream, string filename, IEnumerable tags, string description, RequestOptions options) + => await CreateStickerAsync(name, stream, filename, tags, description, options); /// - async Task IGuild.CreateStickerAsync(string name, string description, IEnumerable tags, Stream stream, string filename, RequestOptions options) - => await CreateStickerAsync(name, description, tags, stream, filename, options); - /// - async Task IGuild.CreateStickerAsync(string name, string description, IEnumerable tags, string path, RequestOptions options) - => await CreateStickerAsync(name, description, tags, path, options); + async Task IGuild.CreateStickerAsync(string name, string path, IEnumerable tags, string description, RequestOptions options) + => await CreateStickerAsync(name, path, tags, description, options); /// async Task IGuild.GetStickerAsync(ulong id, CacheMode mode, RequestOptions options) => await GetStickerAsync(id, mode, options);