From 4f0692db5916c1008ce734c8be682610aa4383e9 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Wed, 8 May 2024 01:13:58 +0000 Subject: [PATCH] dev: Some minor renaming to match the strings add: audit logs for several commands docs: Updated changelog fix: xp card user avatar wasn't showing for some users --- CHANGELOG.md | 3 ++ .../Modules/Administration/Administration.cs | 5 ++- .../Role/ReactionRolesService.cs | 27 ++++++++--- .../Administration/Role/RoleCommands.cs | 5 ++- .../Blacklist/BlacklistCommands.cs | 9 ++-- .../Searches/Translate/TranslatorCommands.cs | 12 ++--- .../Utility/StreamRole/StreamRoleService.cs | 2 +- src/NadekoBot/Modules/Xp/XpService.cs | 45 ++++++++++--------- .../Services/Impl/RedisBotStringsProvider.cs | 2 - .../_common/Attributes/CmdAttribute.cs | 3 -- .../_common/TypeReaders/ModuleTypeReader.cs | 2 - .../_common/_Extensions/UserExtensions.cs | 10 ++--- 12 files changed, 73 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7e928b1b..4b9814943 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog. - Added seconds/sec/s to .convert command - Added `.prunecancel` to cancel an active prune - Added progress reporting when using `.prune`. The bot will periodically update on how many messages have been deleted +- Audit log reason will be automatically added when using `.setrole`, reaction role and `.dtch` commands ### Changed @@ -58,6 +59,8 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog. - Fixed a bug in .invitelist not paginating correctly - `.serverinfo` will now correctly work for other shards - `.send` will now correctly work for other shards +- `.translate` command will no longer fail if the user capitalizes the language name +- Fixed xp card user avatar not showing for some users ### Removed diff --git a/src/NadekoBot/Modules/Administration/Administration.cs b/src/NadekoBot/Modules/Administration/Administration.cs index 97511fc3d..8dc3d20b4 100644 --- a/src/NadekoBot/Modules/Administration/Administration.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -207,7 +207,10 @@ public partial class Administration : NadekoModule [BotPerm(GuildPerm.ManageChannels)] public async Task DelTxtChanl([Leftover] ITextChannel toDelete) { - await toDelete.DeleteAsync(); + await toDelete.DeleteAsync(new RequestOptions() + { + AuditLogReason = $"Deleted by {ctx.User.Username}" + }); await Response().Confirm(strs.deltextchan(Format.Bold(toDelete.Name))).SendAsync(); } diff --git a/src/NadekoBot/Modules/Administration/Role/ReactionRolesService.cs b/src/NadekoBot/Modules/Administration/Role/ReactionRolesService.cs index 557321519..7a71f85ba 100644 --- a/src/NadekoBot/Modules/Administration/Role/ReactionRolesService.cs +++ b/src/NadekoBot/Modules/Administration/Role/ReactionRolesService.cs @@ -103,7 +103,10 @@ public sealed class ReactionRolesService : IReadyExecutor, INService, IReactionR { if (user.RoleIds.Contains(role.Id)) { - await user.RemoveRoleAsync(role.Id); + await user.RemoveRoleAsync(role.Id, new RequestOptions() + { + AuditLogReason = $"Reaction role" + }); } } finally @@ -176,11 +179,22 @@ public sealed class ReactionRolesService : IReadyExecutor, INService, IReactionR var exclusive = reros .Where(x => x.Group == rero.Group && x.RoleId != role.Id) .Select(x => x.RoleId) - .Distinct(); + .Distinct() + .ToArray(); - try { await user.RemoveRolesAsync(exclusive); } - catch { } + if (exclusive.Any()) + { + try + { + await user.RemoveRolesAsync(exclusive, + new RequestOptions() + { + AuditLogReason = "Reaction role exclusive group" + }); + } + catch { } + } // remove user's previous reaction try @@ -203,7 +217,10 @@ public sealed class ReactionRolesService : IReadyExecutor, INService, IReactionR } } - await user.AddRoleAsync(role.Id); + await user.AddRoleAsync(role.Id, new() + { + AuditLogReason = "Reaction role" + }); } } finally diff --git a/src/NadekoBot/Modules/Administration/Role/RoleCommands.cs b/src/NadekoBot/Modules/Administration/Role/RoleCommands.cs index b6d9a1200..fadc5d9c2 100644 --- a/src/NadekoBot/Modules/Administration/Role/RoleCommands.cs +++ b/src/NadekoBot/Modules/Administration/Role/RoleCommands.cs @@ -34,7 +34,10 @@ public partial class Administration return; try { - await targetUser.AddRoleAsync(roleToAdd); + await targetUser.AddRoleAsync(roleToAdd, new RequestOptions() + { + AuditLogReason = $"Added by [{ctx.User.Username}]" + }); await Response().Confirm(strs.setrole(Format.Bold(roleToAdd.Name), Format.Bold(targetUser.ToString()))).SendAsync(); diff --git a/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistCommands.cs b/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistCommands.cs index 02b80a5e6..1e62bdc6f 100644 --- a/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistCommands.cs +++ b/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistCommands.cs @@ -21,11 +21,11 @@ public partial class Permissions var list = _service.GetBlacklist(); var allItems = await list.Where(x => x.Type == type) - .Select(async i => + .Select(i => { try { - return i.Type switch + return Task.FromResult(i.Type switch { BlacklistType.Channel => Format.Code(i.ItemId.ToString()) + " " @@ -40,14 +40,15 @@ public partial class Permissions + " " + (_client.GetGuild(i.ItemId)?.ToString() ?? ""), _ => Format.Code(i.ItemId.ToString()) - }; + }); } catch { Log.Warning("Can't get {BlacklistType} [{BlacklistItemId}]", i.Type, i.ItemId); - return Format.Code(i.ItemId.ToString()); + + return Task.FromResult(Format.Code(i.ItemId.ToString())); } }) .WhenAll(); diff --git a/src/NadekoBot/Modules/Searches/Translate/TranslatorCommands.cs b/src/NadekoBot/Modules/Searches/Translate/TranslatorCommands.cs index 8466fe159..ca457ccc5 100644 --- a/src/NadekoBot/Modules/Searches/Translate/TranslatorCommands.cs +++ b/src/NadekoBot/Modules/Searches/Translate/TranslatorCommands.cs @@ -13,14 +13,14 @@ public partial class Searches } [Cmd] - public async Task Translate(string from, string to, [Leftover] string text = null) + public async Task Translate(string fromLang, string toLang, [Leftover] string text = null) { try { await ctx.Channel.TriggerTypingAsync(); - var translation = await _service.Translate(from, to, text); + var translation = await _service.Translate(fromLang, toLang, text); - var embed = _sender.CreateEmbed().WithOkColor().AddField(from, text).AddField(to, translation); + var embed = _sender.CreateEmbed().WithOkColor().AddField(fromLang, text).AddField(toLang, translation); await Response().Embed(embed).SendAsync(); } @@ -55,9 +55,9 @@ public partial class Searches [Cmd] [RequireContext(ContextType.Guild)] - public async Task AutoTransLang(string from, string to) + public async Task AutoTransLang(string fromLang, string toLang) { - var succ = await _service.RegisterUserAsync(ctx.User.Id, ctx.Channel.Id, from.ToLower(), to.ToLower()); + var succ = await _service.RegisterUserAsync(ctx.User.Id, ctx.Channel.Id, fromLang.ToLower(), toLang.ToLower()); if (succ is null) { @@ -71,7 +71,7 @@ public partial class Searches return; } - await Response().Confirm(strs.atl_set(from, to)).SendAsync(); + await Response().Confirm(strs.atl_set(fromLang, toLang)).SendAsync(); } [Cmd] diff --git a/src/NadekoBot/Modules/Utility/StreamRole/StreamRoleService.cs b/src/NadekoBot/Modules/Utility/StreamRole/StreamRoleService.cs index adee9f06c..e636f1ea0 100644 --- a/src/NadekoBot/Modules/Utility/StreamRole/StreamRoleService.cs +++ b/src/NadekoBot/Modules/Utility/StreamRole/StreamRoleService.cs @@ -60,7 +60,7 @@ public class StreamRoleService : IReadyExecutor, INService /// Guild /// Add or rem action /// User's Id - /// User's name#discrim + /// User's name /// Whether the operation was successful public async Task ApplyListAction( StreamRoleListType listType, diff --git a/src/NadekoBot/Modules/Xp/XpService.cs b/src/NadekoBot/Modules/Xp/XpService.cs index fbddcd3be..30ddb861d 100644 --- a/src/NadekoBot/Modules/Xp/XpService.cs +++ b/src/NadekoBot/Modules/Xp/XpService.cs @@ -1200,36 +1200,39 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand { var avatarUrl = stats.User.RealAvatarUrl(); - var result = await _c.GetImageDataAsync(avatarUrl); - if (!result.TryPickT0(out var data, out _)) + if (avatarUrl is not null) { - using (var http = _httpFactory.CreateClient()) + var result = await _c.GetImageDataAsync(avatarUrl); + if (!result.TryPickT0(out var data, out _)) { - var avatarData = await http.GetByteArrayAsync(avatarUrl); - using (var tempDraw = Image.Load(avatarData)) + using (var http = _httpFactory.CreateClient()) { - tempDraw.Mutate(x => x - .Resize(template.User.Icon.Size.X, template.User.Icon.Size.Y) - .ApplyRoundedCorners(Math.Max(template.User.Icon.Size.X, - template.User.Icon.Size.Y) - / 2.0f)); - await using (var stream = await tempDraw.ToStreamAsync()) + var avatarData = await http.GetByteArrayAsync(avatarUrl); + using (var tempDraw = Image.Load(avatarData)) { - data = stream.ToArray(); + tempDraw.Mutate(x => x + .Resize(template.User.Icon.Size.X, template.User.Icon.Size.Y) + .ApplyRoundedCorners(Math.Max(template.User.Icon.Size.X, + template.User.Icon.Size.Y) + / 2.0f)); + await using (var stream = await tempDraw.ToStreamAsync()) + { + data = stream.ToArray(); + } } } + + await _c.SetImageDataAsync(avatarUrl, data); } - await _c.SetImageDataAsync(avatarUrl, data); + using var toDraw = Image.Load(data); + if (toDraw.Size() != new Size(template.User.Icon.Size.X, template.User.Icon.Size.Y)) + toDraw.Mutate(x => x.Resize(template.User.Icon.Size.X, template.User.Icon.Size.Y)); + + img.Mutate(x => x.DrawImage(toDraw, + new Point(template.User.Icon.Pos.X, template.User.Icon.Pos.Y), + 1)); } - - using var toDraw = Image.Load(data); - if (toDraw.Size() != new Size(template.User.Icon.Size.X, template.User.Icon.Size.Y)) - toDraw.Mutate(x => x.Resize(template.User.Icon.Size.X, template.User.Icon.Size.Y)); - - img.Mutate(x => x.DrawImage(toDraw, - new Point(template.User.Icon.Pos.X, template.User.Icon.Pos.Y), - 1)); } catch (Exception ex) { diff --git a/src/NadekoBot/Services/Impl/RedisBotStringsProvider.cs b/src/NadekoBot/Services/Impl/RedisBotStringsProvider.cs index ef1aaead6..d86f1421c 100644 --- a/src/NadekoBot/Services/Impl/RedisBotStringsProvider.cs +++ b/src/NadekoBot/Services/Impl/RedisBotStringsProvider.cs @@ -5,8 +5,6 @@ using System.Web; namespace NadekoBot.Services; -// todo fix - /// /// Uses to load strings into redis hash (only on Shard 0) /// and retrieves them from redis via diff --git a/src/NadekoBot/_common/Attributes/CmdAttribute.cs b/src/NadekoBot/_common/Attributes/CmdAttribute.cs index 79e4209ce..6bb0002b8 100644 --- a/src/NadekoBot/_common/Attributes/CmdAttribute.cs +++ b/src/NadekoBot/_common/Attributes/CmdAttribute.cs @@ -16,6 +16,3 @@ public sealed class CmdAttribute : CommandAttribute Summary = memberName.ToLowerInvariant(); } } - -[AttributeUsage(AttributeTargets.Method)] -public sealed class PromptableAttribute: Attribute; \ No newline at end of file diff --git a/src/NadekoBot/_common/TypeReaders/ModuleTypeReader.cs b/src/NadekoBot/_common/TypeReaders/ModuleTypeReader.cs index 33908dda0..77d58e401 100644 --- a/src/NadekoBot/_common/TypeReaders/ModuleTypeReader.cs +++ b/src/NadekoBot/_common/TypeReaders/ModuleTypeReader.cs @@ -44,8 +44,6 @@ public sealed class ModuleOrExprTypeReader : NadekoTypeReader } } -// todo chagne commands.en-us to have the new type name - public sealed class ModuleOrExpr { public string Name { get; set; } diff --git a/src/NadekoBot/_common/_Extensions/UserExtensions.cs b/src/NadekoBot/_common/_Extensions/UserExtensions.cs index 6718615c3..6bfd4061e 100644 --- a/src/NadekoBot/_common/_Extensions/UserExtensions.cs +++ b/src/NadekoBot/_common/_Extensions/UserExtensions.cs @@ -9,10 +9,8 @@ public static class UserExtensions => usr.AvatarId is null ? new(usr.GetDefaultAvatarUrl()) : new Uri(usr.GetAvatarUrl(ImageFormat.Auto, size)); // This method is only used for the xp card - public static Uri RealAvatarUrl(this DiscordUser usr) - => usr.AvatarId is null - ? new(CDN.GetDefaultUserAvatarUrl(ushort.Parse(usr.Discriminator))) - : new Uri(usr.AvatarId.StartsWith("a_", StringComparison.InvariantCulture) - ? $"{DiscordConfig.CDNUrl}avatars/{usr.UserId}/{usr.AvatarId}.gif" - : $"{DiscordConfig.CDNUrl}avatars/{usr.UserId}/{usr.AvatarId}.png"); + public static Uri? RealAvatarUrl(this DiscordUser usr) + => Uri.TryCreate(CDN.GetDefaultUserAvatarUrl(usr.UserId), UriKind.Absolute, out var uri) + ? uri + : null; } \ No newline at end of file