Skip to content

Commit

Permalink
[Fix] Allow creating stickers with no description (#2628)
Browse files Browse the repository at this point in the history
* fix `CreateStickerAsync` methods

* Update src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs

Co-authored-by: Dmitry <dimson-n@users.noreply.github.com>

* fix some parameter names in precontion checks

---------

Co-authored-by: Dmitry <dimson-n@users.noreply.github.com>
  • Loading branch information
Misha-133 and dimson-n committed Mar 31, 2023
1 parent f9c8530 commit c950106
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the created sticker.
/// </returns>
Task<ICustomSticker> CreateStickerAsync(string name, string description, IEnumerable<string> tags, Image image, RequestOptions options = null);
Task<ICustomSticker> CreateStickerAsync(string name, Image image, IEnumerable<string> tags, string description = null, RequestOptions options = null);

/// <summary>
/// Creates a new sticker in this guild.
Expand All @@ -1117,7 +1117,7 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the created sticker.
/// </returns>
Task<ICustomSticker> CreateStickerAsync(string name, string description, IEnumerable<string> tags, string path, RequestOptions options = null);
Task<ICustomSticker> CreateStickerAsync(string name, string path, IEnumerable<string> tags, string description = null, RequestOptions options = null);

/// <summary>
/// Creates a new sticker in this guild.
Expand All @@ -1131,7 +1131,7 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the created sticker.
/// </returns>
Task<ICustomSticker> CreateStickerAsync(string name, string description, IEnumerable<string> tags, Stream stream, string filename, RequestOptions options = null);
Task<ICustomSticker> CreateStickerAsync(string name, Stream stream, string filename, IEnumerable<string> tags, string description = null, RequestOptions options = null);

/// <summary>
/// Gets a specific sticker within this guild.
Expand Down
6 changes: 5 additions & 1 deletion src/Discord.Net.Rest/API/Rest/CreateStickerParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ internal class CreateStickerParams
var d = new Dictionary<string, object>
{
["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)
{
Expand Down
42 changes: 30 additions & 12 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,49 +708,67 @@ public static async Task<GuildEmote> GetEmoteAsync(IGuild guild, BaseDiscordClie
public static Task DeleteEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options)
=> client.ApiClient.DeleteGuildEmoteAsync(guild.Id, id, options);

public static async Task<API.Sticker> CreateStickerAsync(BaseDiscordClient client, IGuild guild, string name, string description, IEnumerable<string> tags,
Image image, RequestOptions options = null)
public static async Task<API.Sticker> CreateStickerAsync(BaseDiscordClient client, IGuild guild, string name, Image image, IEnumerable<string> 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<API.Sticker> CreateStickerAsync(BaseDiscordClient client, IGuild guild, string name, string description, IEnumerable<string> tags,
Stream file, string filename, RequestOptions options = null)
public static async Task<API.Sticker> CreateStickerAsync(BaseDiscordClient client, IGuild guild, string name, Stream file, string filename, IEnumerable<string> 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
};

Expand Down
26 changes: 13 additions & 13 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,9 +1051,9 @@ public Task DeleteEmoteAsync(GuildEmote emote, RequestOptions options = null)
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the created sticker.
/// </returns>
public async Task<CustomSticker> CreateStickerAsync(string name, string description, IEnumerable<string> tags, Image image, RequestOptions options = null)
public async Task<CustomSticker> CreateStickerAsync(string name, Image image, IEnumerable<string> 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);
}
Expand All @@ -1068,11 +1068,11 @@ public async Task<CustomSticker> CreateStickerAsync(string name, string descript
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the created sticker.
/// </returns>
public Task<CustomSticker> CreateStickerAsync(string name, string description, IEnumerable<string> tags, string path,
public Task<CustomSticker> CreateStickerAsync(string name, string path, IEnumerable<string> 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);
}
/// <summary>
/// Creates a new sticker in this guild
Expand All @@ -1086,10 +1086,10 @@ public async Task<CustomSticker> CreateStickerAsync(string name, string descript
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the created sticker.
/// </returns>
public async Task<CustomSticker> CreateStickerAsync(string name, string description, IEnumerable<string> tags, Stream stream,
string filename, RequestOptions options = null)
public async Task<CustomSticker> CreateStickerAsync(string name, Stream stream, string filename, IEnumerable<string> 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);
}
Expand Down Expand Up @@ -1527,14 +1527,14 @@ async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions
async Task<IReadOnlyCollection<IApplicationCommand>> IGuild.GetApplicationCommandsAsync(bool withLocalizations, string locale, RequestOptions options)
=> await GetApplicationCommandsAsync(withLocalizations, locale, options).ConfigureAwait(false);
/// <inheritdoc />
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, string description, IEnumerable<string> tags, Image image, RequestOptions options)
=> await CreateStickerAsync(name, description, tags, image, options);
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, Image image, IEnumerable<string> tags, string description, RequestOptions options)
=> await CreateStickerAsync(name, image, tags, description, options);
/// <inheritdoc />
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, string description, IEnumerable<string> tags, Stream stream, string filename, RequestOptions options)
=> await CreateStickerAsync(name, description, tags, stream, filename, options);
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, Stream stream, string filename, IEnumerable<string> tags, string description, RequestOptions options)
=> await CreateStickerAsync(name, stream, filename, tags, description, options);
/// <inheritdoc />
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, string description, IEnumerable<string> tags, string path, RequestOptions options)
=> await CreateStickerAsync(name, description, tags, path, options);
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, string path, IEnumerable<string> tags, string description, RequestOptions options)
=> await CreateStickerAsync(name, path, tags, description, options);
/// <inheritdoc />
async Task<ICustomSticker> IGuild.GetStickerAsync(ulong id, CacheMode mode, RequestOptions options)
{
Expand Down
27 changes: 13 additions & 14 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1540,10 +1540,10 @@ public SocketCustomSticker GetSticker(ulong id)
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the created sticker.
/// </returns>
public async Task<SocketCustomSticker> CreateStickerAsync(string name, string description, IEnumerable<string> tags, Image image,
public async Task<SocketCustomSticker> CreateStickerAsync(string name, Image image, IEnumerable<string> 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);
}
Expand All @@ -1558,11 +1558,11 @@ public SocketCustomSticker GetSticker(ulong id)
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the created sticker.
/// </returns>
public Task<SocketCustomSticker> CreateStickerAsync(string name, string description, IEnumerable<string> tags, string path,
public Task<SocketCustomSticker> CreateStickerAsync(string name, string path, IEnumerable<string> 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);
}
/// <summary>
/// Creates a new sticker in this guild
Expand All @@ -1576,10 +1576,10 @@ public SocketCustomSticker GetSticker(ulong id)
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the created sticker.
/// </returns>
public async Task<SocketCustomSticker> CreateStickerAsync(string name, string description, IEnumerable<string> tags, Stream stream,
string filename, RequestOptions options = null)
public async Task<SocketCustomSticker> CreateStickerAsync(string name, Stream stream, string filename, IEnumerable<string> 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);
}
Expand Down Expand Up @@ -2111,15 +2111,14 @@ async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions
/// <inheritdoc />
async Task<IReadOnlyCollection<IApplicationCommand>> IGuild.GetApplicationCommandsAsync(bool withLocalizations, string locale, RequestOptions options)
=> await GetApplicationCommandsAsync(withLocalizations, locale, options).ConfigureAwait(false);
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, Image image, IEnumerable<string> tags, string description, RequestOptions options)
=> await CreateStickerAsync(name, image, tags, description, options);
/// <inheritdoc />
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, string description, IEnumerable<string> tags, Image image, RequestOptions options)
=> await CreateStickerAsync(name, description, tags, image, options);
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, Stream stream, string filename, IEnumerable<string> tags, string description, RequestOptions options)
=> await CreateStickerAsync(name, stream, filename, tags, description, options);
/// <inheritdoc />
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, string description, IEnumerable<string> tags, Stream stream, string filename, RequestOptions options)
=> await CreateStickerAsync(name, description, tags, stream, filename, options);
/// <inheritdoc />
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, string description, IEnumerable<string> tags, string path, RequestOptions options)
=> await CreateStickerAsync(name, description, tags, path, options);
async Task<ICustomSticker> IGuild.CreateStickerAsync(string name, string path, IEnumerable<string> tags, string description, RequestOptions options)
=> await CreateStickerAsync(name, path, tags, description, options);
/// <inheritdoc />
async Task<ICustomSticker> IGuild.GetStickerAsync(ulong id, CacheMode mode, RequestOptions options)
=> await GetStickerAsync(id, mode, options);
Expand Down

0 comments on commit c950106

Please sign in to comment.