mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-04 00:34:26 -05:00 
			
		
		
		
	fix: Fixed .stickeradd, it now properly supports 300x300 image uploads. closes #434
This commit is contained in:
		@@ -1,14 +1,14 @@
 | 
			
		||||
// namespace NadekoBot.Modules.Administration;
 | 
			
		||||
//
 | 
			
		||||
// public partial class Administration
 | 
			
		||||
// {
 | 
			
		||||
//     [Group]
 | 
			
		||||
//     public partial class TicketCommands : NadekoModule
 | 
			
		||||
//     {
 | 
			
		||||
//         [Cmd]
 | 
			
		||||
//         public async Task Ticket()
 | 
			
		||||
//         {
 | 
			
		||||
//             
 | 
			
		||||
//         }
 | 
			
		||||
//     }
 | 
			
		||||
// }
 | 
			
		||||
namespace NadekoBot.Modules.Administration;
 | 
			
		||||
 | 
			
		||||
public partial class Administration
 | 
			
		||||
{
 | 
			
		||||
    [Group]
 | 
			
		||||
    public partial class TicketCommands : NadekoModule
 | 
			
		||||
    {
 | 
			
		||||
        [Cmd]
 | 
			
		||||
        public async Task Ticket()
 | 
			
		||||
        {
 | 
			
		||||
            
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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: <https://xpshop.nadeko.bot>",
 | 
			
		||||
  "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",
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user