From 2700bfdce8acb75085c3551fabef3f1b0309e6a9 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sat, 29 Jun 2024 08:42:43 +0000 Subject: [PATCH] fix: Fixed .stickeradd, it now properly supports 300x300 image uploads. closes #434 --- .../Administration/Ticket/TicketCommands.cs | 28 ++--- src/NadekoBot/Modules/Utility/Utility.cs | 101 ++++++++++++------ .../data/strings/commands/commands.en-US.yml | 6 +- .../strings/responses/responses.en-US.json | 3 +- 4 files changed, 88 insertions(+), 50 deletions(-) diff --git a/src/NadekoBot/Modules/Administration/Ticket/TicketCommands.cs b/src/NadekoBot/Modules/Administration/Ticket/TicketCommands.cs index b7606077e..fba0a017a 100644 --- a/src/NadekoBot/Modules/Administration/Ticket/TicketCommands.cs +++ b/src/NadekoBot/Modules/Administration/Ticket/TicketCommands.cs @@ -1,14 +1,14 @@ -// namespace NadekoBot.Modules.Administration; -// -// public partial class Administration -// { -// [Group] -// public partial class TicketCommands : NadekoModule -// { -// [Cmd] -// public async Task Ticket() -// { -// -// } -// } -// } \ No newline at end of file +namespace NadekoBot.Modules.Administration; + +public partial class Administration +{ + [Group] + public partial class TicketCommands : NadekoModule + { + [Cmd] + public async Task Ticket() + { + + } + } +} \ No newline at end of file diff --git a/src/NadekoBot/Modules/Utility/Utility.cs b/src/NadekoBot/Modules/Utility/Utility.cs index 5a43cc904..edee3c3fd 100644 --- a/src/NadekoBot/Modules/Utility/Utility.cs +++ b/src/NadekoBot/Modules/Utility/Utility.cs @@ -459,47 +459,82 @@ public partial class Utility : NadekoModule public async Task StickerAdd(string name = null, string description = null, params string[] tags) { string format; - Stream stream; - - if (ctx.Message.Stickers.Count is 1 && ctx.Message.Stickers.First() is SocketSticker ss) - { - name ??= ss.Name; - description = ss.Description; - tags = tags is null or { Length: 0 } ? ss.Tags.ToArray() : tags; - format = FormatToExtension(ss.Format); - - using var http = _httpFactory.CreateClient(); - stream = await http.GetStreamAsync(ss.GetStickerUrl()); - } - else - { - await Response().Error(strs.sticker_error).SendAsync(); - return; - } + Stream stream = null; try { - if (tags.Length == 0) - tags = [name]; + if (ctx.Message.Stickers.Count is 1 && ctx.Message.Stickers.First() is SocketSticker ss) + { + name ??= ss.Name; + description = ss.Description; + tags = tags is null or { Length: 0 } ? ss.Tags.ToArray() : tags; + format = FormatToExtension(ss.Format); - await ctx.Guild.CreateStickerAsync( - name, - stream, - $"{name}.{format}", - tags, - string.IsNullOrWhiteSpace(description) ? "Missing description" : description - ); + using var http = _httpFactory.CreateClient(); + stream = await http.GetStreamAsync(ss.GetStickerUrl()); + } + else if (ctx.Message.Attachments.Count is 1 && name is not null) + { + if (tags.Length == 0) + tags = [name]; - await ctx.OkAsync(); - } - catch (Exception ex) - { - Log.Warning(ex, "Error occurred while adding a sticker: {Message}", ex.Message); - await Response().Error(strs.error_occured).SendAsync(); + if (ctx.Message.Attachments.Count != 1) + { + await Response().Error(strs.sticker_error).SendAsync(); + return; + } + + var attach = ctx.Message.Attachments.First(); + + + if (attach.Size > 512_000 || attach.Width != 300 || attach.Height != 300) + { + await Response().Error(strs.sticker_error).SendAsync(); + return; + } + + format = attach.Filename + .Split('.') + .Last() + .ToLowerInvariant(); + + if (string.IsNullOrWhiteSpace(format) || (format != "png" && format != "apng")) + { + await Response().Error(strs.sticker_error).SendAsync(); + return; + } + + using var http = _httpFactory.CreateClient(); + stream = await http.GetStreamAsync(attach.Url); + } + else + { + await Response().Error(strs.sticker_error).SendAsync(); + return; + } + + try + { + await ctx.Guild.CreateStickerAsync( + name, + stream, + $"{name}.{format}", + tags, + string.IsNullOrWhiteSpace(description) ? "Missing description" : description + ); + + await ctx.OkAsync(); + } + catch + (Exception ex) + { + Log.Warning(ex, "Error occurred while adding a sticker: {Message}", ex.Message); + await Response().Error(strs.error_occured).SendAsync(); + } } finally { - await stream.DisposeAsync(); + await (stream?.DisposeAsync() ?? ValueTask.CompletedTask); } } diff --git a/src/NadekoBot/data/strings/commands/commands.en-US.yml b/src/NadekoBot/data/strings/commands/commands.en-US.yml index a9a8fa734..c1fa73de1 100644 --- a/src/NadekoBot/data/strings/commands/commands.en-US.yml +++ b/src/NadekoBot/data/strings/commands/commands.en-US.yml @@ -2289,7 +2289,11 @@ emojiremove: - emotes: desc: "The list of emojis to be removed from the server." stickeradd: - desc: Adds the sticker from your message to this server. Send the sticker along with this command (in the same message). + desc: |- + Adds the sticker from your message to this server. + Send the sticker along with this command (in the same message). + Alternatively you can upload an image along with this command but you have to specify the name. + The image must be 300x300 in .png or .apng format and up to 512KB in size. ex: - '' - name "description" tag1 tag2 tagN diff --git a/src/NadekoBot/data/strings/responses/responses.en-US.json b/src/NadekoBot/data/strings/responses/responses.en-US.json index eabc96a27..ed48f2fe9 100644 --- a/src/NadekoBot/data/strings/responses/responses.en-US.json +++ b/src/NadekoBot/data/strings/responses/responses.en-US.json @@ -1067,8 +1067,7 @@ "xpshop_already_owned": "You already own this item.", "xpshop_item_not_found": "An item with that key doesn't exist.", "xpshop_website": "You can see the list of all Xp Shop items here: ", - "sticker_invalid_size": "Stickers must be exactly 300x300 pixels.", - "sticker_error": "You must either send a sticker along with this command, or upload a 300x300 .png or .apng image.", + "sticker_error": "You must either send a sticker along with this command, or upload a 300x300 .png or .apng image. Up to 512KB in size.", "sticker_missing_name": "Please specify a name for the sticker.", "thread_deleted": "Thread Deleted", "thread_created": "Thread Created",