From ee6c60ab53e7d848d86b257b69a4ec4f2fd53f56 Mon Sep 17 00:00:00 2001 From: Sakura Akeno Isayeki Date: Thu, 22 Apr 2021 01:04:11 +0200 Subject: [PATCH 1/4] Fix NullReferenceException on out-of-server Member check --- Modules/UserLookupModule.cs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Modules/UserLookupModule.cs b/Modules/UserLookupModule.cs index fa0dcb8..cc9ec7f 100644 --- a/Modules/UserLookupModule.cs +++ b/Modules/UserLookupModule.cs @@ -32,41 +32,41 @@ public UserLookupModule(TrustlistUserApiService trustlist, AuthApiService auth, public async Task LookupAsync(CommandContext context, DiscordUser user) => await LookupAsync(context, user, user.Id); [Command("insert"), Aliases("add"), RequireGuild, RequireUserPermissions(Permissions.BanMembers)] - public async Task InsertUserAsync(CommandContext context, DiscordUser user, [Range(0, 3)] byte level, [RemainingText] string reason) + public async Task InsertUserAsync(CommandContext context, DiscordUser user, byte level, [RemainingText] string reason) { - await InsertUserAsync(context, user, user.Id, level, reason); + await InsertUserAsync(context, user, level, reason, false); } [Command("ban"), RequireGuild, RequireUserPermissions(Permissions.BanMembers), RequireBotPermissions(Permissions.BanMembers)] public async Task BanUserAsync(CommandContext context, DiscordUser user, [Range(0, 3)] byte level, [RemainingText] string reason) { - await InsertUserAsync(context, user, user.Id, level, reason, true); + await InsertUserAsync(context, user, level, reason, true); } - private async Task InsertUserAsync(CommandContext context, DiscordUser user, ulong userId, byte level, string reason, bool banUser = false) + private async Task InsertUserAsync(CommandContext context, DiscordUser user, byte level, string reason, bool banUser = false) { if (user is not null) { if (user?.Id == context.User.Id) { - await context.RespondAsync($"{context.User.Mention} You cannot insert yourself in the Trustlist."); + await context.RespondAsync("You cannot insert yourself in the Trustlist."); return; } else if (user.IsBot) { - await context.RespondAsync($"{context.User.Mention} You cannot insert a Bot in the Trustlist."); + await context.RespondAsync("You cannot insert a Bot in the Trustlist."); return; } - else if ((user as DiscordMember).Roles.Any(r => r.Permissions == (r.Permissions & Permissions.ManageGuild))) + else if ((user as DiscordMember)?.Roles.Any(r => r.Permissions == (r.Permissions & Permissions.ManageGuild)) ?? false) { - await context.RespondAsync($"You cannot insert a server operator in the Trustlist. Demote them first."); + await context.RespondAsync("You cannot insert a server operator in the Trustlist. Demote them first."); return; } } if (reason.Length < 5) { - await context.RespondAsync($"{context.User.Mention} Reason is too short"); + await context.RespondAsync("Reason is too short"); } else { @@ -78,23 +78,23 @@ private async Task InsertUserAsync(CommandContext context, DiscordUser user, ulo { await trustlist.InsertOrEscalateUserAsync(new() { - Id = userId, + Id = user.Id, EscalationLevel = level, EscalationNote = reason }, await auth.GetOrUpdateAuthTokenAsync(context.Guild.Id)); - await context.RespondAsync($"User '{user?.Mention ?? userId.ToString()}' successfully inserted into Trustlist."); - await LookupAsync(context, user, userId); + await context.RespondAsync($"User '{user?.Mention ?? user.Id.ToString()}' successfully inserted into Trustlist."); + await LookupAsync(context, user, user.Id); if (banUser || (config.AutoBanBlacklisted && level >= 3)) { - await context.Guild.BanMemberAsync(userId, 0, $"[SocialGuard] {reason}"); + await context.Guild.BanMemberAsync(user.Id, 0, $"[SocialGuard] {reason}"); await context.Guild.GetChannel(config.BanLogChannel).SendMessageAsync($"Banned user '{user}'."); } } else { - await context.RespondAsync("No API Credentials set. Use ``sg config accesskey `` to set an Access Key."); + await context.RespondAsync($"No API Credentials set. Use ``{context.Prefix}sg config accesskey `` to set an Access Key."); } } catch (ApplicationException e) From 46ba3f289231c1be5dcd153ef90228b5599ab7c6 Mon Sep 17 00:00:00 2001 From: Sakura Akeno Isayeki Date: Thu, 22 Apr 2021 02:17:29 +0200 Subject: [PATCH 2/4] Fix Discord Name display + Embed inconsistencies on lookup & guild join --- GuildTrafficHandler.cs | 2 +- Modules/UserLookupModule.cs | 19 ++++++++++++------- Utilities.cs | 20 ++++++++++++-------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/GuildTrafficHandler.cs b/GuildTrafficHandler.cs index bb9ea00..1e3a4f0 100644 --- a/GuildTrafficHandler.cs +++ b/GuildTrafficHandler.cs @@ -29,7 +29,7 @@ public async Task OnMemberJoinedAsync(DiscordClient _, GuildMemberAddEventArgs e { TrustlistUser entry = await apiService.LookupUserAsync(e.Member.Id); DiscordChannel joinLog = e.Guild.GetChannel(config.JoinLogChannel); - DiscordEmbed entryEmbed = Utilities.BuildUserRecordEmbed(entry, e.Member, e.Member.Id); + DiscordEmbed entryEmbed = Utilities.BuildUserRecordEmbed(entry, e.Member); await joinLog.SendMessageAsync($"User **{e.Member}** ({e.Member.Mention}) has joined the server.", entryEmbed); diff --git a/Modules/UserLookupModule.cs b/Modules/UserLookupModule.cs index cc9ec7f..42c362f 100644 --- a/Modules/UserLookupModule.cs +++ b/Modules/UserLookupModule.cs @@ -29,7 +29,7 @@ public UserLookupModule(TrustlistUserApiService trustlist, AuthApiService auth, } [Command("lookup"), Aliases("get")] - public async Task LookupAsync(CommandContext context, DiscordUser user) => await LookupAsync(context, user, user.Id); + public async Task LookupAsync(CommandContext context, DiscordUser user) => await RespondLookupAsync(context, user); [Command("insert"), Aliases("add"), RequireGuild, RequireUserPermissions(Permissions.BanMembers)] public async Task InsertUserAsync(CommandContext context, DiscordUser user, byte level, [RemainingText] string reason) @@ -83,13 +83,13 @@ await trustlist.InsertOrEscalateUserAsync(new() EscalationNote = reason }, await auth.GetOrUpdateAuthTokenAsync(context.Guild.Id)); - await context.RespondAsync($"User '{user?.Mention ?? user.Id.ToString()}' successfully inserted into Trustlist."); - await LookupAsync(context, user, user.Id); + string userMention = (user as DiscordMember)?.Mention ?? user.Id.ToString(); + await context.RespondAsync($"User '{userMention}' successfully inserted into Trustlist.", await LookupAsync(user)); if (banUser || (config.AutoBanBlacklisted && level >= 3)) { await context.Guild.BanMemberAsync(user.Id, 0, $"[SocialGuard] {reason}"); - await context.Guild.GetChannel(config.BanLogChannel).SendMessageAsync($"Banned user '{user}'."); + await context.Guild.GetChannel(config.BanLogChannel).SendMessageAsync($"Banned user '{userMention}'."); } } else @@ -107,15 +107,20 @@ await trustlist.InsertOrEscalateUserAsync(new() } } - public async Task LookupAsync(CommandContext context, DiscordUser user, ulong userId, bool silenceOnClear = false) + public async Task RespondLookupAsync(CommandContext context, DiscordUser user, bool silenceOnClear = false) { - TrustlistUser entry = await trustlist.LookupUserAsync(userId); + TrustlistUser entry = await trustlist.LookupUserAsync(user.Id); if (!silenceOnClear || entry.EscalationLevel is not 0) { - await context.RespondAsync(embed: Utilities.BuildUserRecordEmbed(entry, user, userId)); + await context.RespondAsync(Utilities.BuildUserRecordEmbed(entry, user)); } } + + public async Task LookupAsync(DiscordUser user) + { + return Utilities.BuildUserRecordEmbed(await trustlist.LookupUserAsync(user.Id), user); + } } } } diff --git a/Utilities.cs b/Utilities.cs index bf41119..654d0d2 100644 --- a/Utilities.cs +++ b/Utilities.cs @@ -7,6 +7,7 @@ using System.Security.Cryptography; using System; using DSharpPlus.Entities; +using DSharpPlus; namespace SocialGuard.YC { @@ -29,7 +30,7 @@ public static IApiConfig PopulateApiConfig(this IApiConfig config) return config; } - public static DiscordEmbed BuildUserRecordEmbed(TrustlistUser trustlistUser, DiscordUser discordUser, ulong userId) + public static DiscordEmbed BuildUserRecordEmbed(TrustlistUser trustlistUser, DiscordUser discordUser) { (DiscordColor color, string name, string desc) = trustlistUser?.EscalationLevel switch { @@ -40,12 +41,8 @@ public static DiscordEmbed BuildUserRecordEmbed(TrustlistUser trustlistUser, Dis }; DiscordEmbedBuilder builder = new(); - builder.WithTitle($"Trustlist User : {discordUser?.Username ?? userId.ToString()}"); - - if (discordUser is not null) - { - builder.AddField("ID", $"``{discordUser?.Id}``", true); - } + builder.WithTitle($"Trustlist User : {discordUser?.Username}"); + builder.AddField("ID", $"`{discordUser?.Id}`", true); builder.Color = color; builder.Description = desc; @@ -54,7 +51,7 @@ public static DiscordEmbed BuildUserRecordEmbed(TrustlistUser trustlistUser, Dis if (trustlistUser is not null) { builder.AddField("Escalation Level", $"{trustlistUser.EscalationLevel} - {name}", true) - .AddField("Emitter", $"{trustlistUser.Emitter.DisplayName} (``{trustlistUser.Emitter.Login}``)") + .AddField("Emitter", $"{trustlistUser.Emitter.DisplayName} (`{trustlistUser.Emitter.Login}`)") .AddField("First Entered", trustlistUser.EntryAt.ToString(), true) .AddField("Last Escalation", trustlistUser.LastEscalated.ToString(), true) .AddField("Reason", trustlistUser.EscalationNote); @@ -85,5 +82,12 @@ internal static string GenerateLocalMasterKey() string localMasterKeyBase64 = Convert.ToBase64String(bytes); return localMasterKeyBase64; } + + public static DiscordEmbedBuilder WithAuthor(this DiscordEmbedBuilder embed, DiscordUser user) + { + return embed.WithAuthor(user.GetFullUsername(), null, user.GetAvatarUrl(ImageFormat.Auto, 128)); + } + + public static string GetFullUsername(this DiscordUser user) => $"{user.Username}#{user.Discriminator}"; } } From d901aab477ff56fa8fd13b3d716e8134e436fa2b Mon Sep 17 00:00:00 2001 From: Sakura Akeno Isayeki Date: Thu, 22 Apr 2021 02:23:27 +0200 Subject: [PATCH 3/4] Fix missing confimations for Register & Login --- Modules/GuildConfigModule.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/GuildConfigModule.cs b/Modules/GuildConfigModule.cs index 970668b..5c0080d 100644 --- a/Modules/GuildConfigModule.cs +++ b/Modules/GuildConfigModule.cs @@ -72,7 +72,7 @@ public async Task ConfigureAccessKeyAsync(CommandContext context, string usernam GuildConfig config = await repository.FindOrCreateConfigAsync(context.Guild.Id); config.ApiLogin = new(username, encryption.Encrypt(password)); await repository.ReplaceOneAsync(config); - await context.RespondAsync($"API credentials has been set."); + await context.Channel.SendMessageAsync($"API credentials has been set."); } [Command("set-autoban"), Aliases("autoban"), RequireUserPermissions(Permissions.ManageGuild), RequireBotPermissions(Permissions.BanMembers)] @@ -105,7 +105,7 @@ public async Task RegisterAsync(CommandContext context, string username, [EmailA AuthRegisterCredentials credentials = new(username, email, password); AuthResponse result = await auth.RegisterNewUserAsync(credentials); - await context.RespondAsync($"{context.User.Mention} {result.Status} : {result.Message}\n"); + await context.Channel.SendMessageAsync($"{context.User.Mention} {result.Status} : {result.Message}\n"); } } } From fd575f6e658639b49a84955c7fa58bff84482663 Mon Sep 17 00:00:00 2001 From: Sakura Akeno Isayeki Date: Thu, 22 Apr 2021 02:24:26 +0200 Subject: [PATCH 4/4] Bump Version > 2.1.2 --- SocialGuard.YC.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SocialGuard.YC.csproj b/SocialGuard.YC.csproj index 1cdbae3..f9951cf 100644 --- a/SocialGuard.YC.csproj +++ b/SocialGuard.YC.csproj @@ -2,7 +2,7 @@ net5.0 - 2.1.1-dev + 2.1.2 Sakura Akeno Isayeki Nodsoft Systems NSYS SocialGuard (YC)