Updated Utility module

This commit is contained in:
Toastie 2024-07-08 00:36:45 +12:00
parent 0787490aea
commit f2618793b3
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
4 changed files with 71 additions and 36 deletions

View file

@ -251,7 +251,7 @@ public sealed class AiAssistantService
return false; return false;
await _cbs.RunChatterBot(sg, msg, channel, sess, query); await _cbs.RunChatterBot(sg, msg, channel, sess, query);
return false; return true;
} }
var commandString = GetCommandString(model); var commandString = GetCommandString(model);

View file

@ -8,7 +8,7 @@ public partial class Utility
private readonly IEnumerable<IConfigService> _settingServices; private readonly IEnumerable<IConfigService> _settingServices;
public ConfigCommands(IEnumerable<IConfigService> settingServices) public ConfigCommands(IEnumerable<IConfigService> settingServices)
=> _settingServices = settingServices.Where(x => x.Name != "medusa"); => _settingServices = settingServices.Where(x => x.Name != "marmalade");
[Cmd] [Cmd]
[OwnerOnly] [OwnerOnly]

View file

@ -459,8 +459,10 @@ public partial class Utility : EllieModule
public async Task StickerAdd(string name = null, string description = null, params string[] tags) public async Task StickerAdd(string name = null, string description = null, params string[] tags)
{ {
string format; string format;
Stream stream; Stream stream = null;
try
{
if (ctx.Message.Stickers.Count is 1 && ctx.Message.Stickers.First() is SocketSticker ss) if (ctx.Message.Stickers.Count is 1 && ctx.Message.Stickers.First() is SocketSticker ss)
{ {
name ??= ss.Name; name ??= ss.Name;
@ -471,6 +473,40 @@ public partial class Utility : EllieModule
using var http = _httpFactory.CreateClient(); using var http = _httpFactory.CreateClient();
stream = await http.GetStreamAsync(ss.GetStickerUrl()); stream = await http.GetStreamAsync(ss.GetStickerUrl());
} }
else if (ctx.Message.Attachments.Count is 1 && name is not null)
{
if (tags.Length == 0)
tags = [name];
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 else
{ {
await Response().Error(strs.sticker_error).SendAsync(); await Response().Error(strs.sticker_error).SendAsync();
@ -479,9 +515,6 @@ public partial class Utility : EllieModule
try try
{ {
if (tags.Length == 0)
tags = [name];
await ctx.Guild.CreateStickerAsync( await ctx.Guild.CreateStickerAsync(
name, name,
stream, stream,
@ -492,14 +525,16 @@ public partial class Utility : EllieModule
await ctx.OkAsync(); await ctx.OkAsync();
} }
catch (Exception ex) catch
(Exception ex)
{ {
Log.Warning(ex, "Error occurred while adding a sticker: {Message}", ex.Message); Log.Warning(ex, "Error occurred while adding a sticker: {Message}", ex.Message);
await Response().Error(strs.error_occured).SendAsync(); await Response().Error(strs.error_occured).SendAsync();
} }
}
finally finally
{ {
await stream.DisposeAsync(); await (stream?.DisposeAsync() ?? ValueTask.CompletedTask);
} }
} }