From 4bac506bfcb640c96b42ff02965d885b93221736 Mon Sep 17 00:00:00 2001 From: Toastie Date: Tue, 29 Oct 2024 22:34:41 +1300 Subject: [PATCH] Codacy refactoring --- Commands/CreateSelectionBoxPanelCommand.cs | 16 +++++++-- Commands/NewCommand.cs | 12 +++++-- Database.cs | 38 +++------------------- EventHandler.cs | 2 ++ 4 files changed, 29 insertions(+), 39 deletions(-) diff --git a/Commands/CreateSelectionBoxPanelCommand.cs b/Commands/CreateSelectionBoxPanelCommand.cs index dfa9827..573073b 100644 --- a/Commands/CreateSelectionBoxPanelCommand.cs +++ b/Commands/CreateSelectionBoxPanelCommand.cs @@ -30,10 +30,14 @@ public class CreateSelectionBoxPanelCommand : ApplicationCommandModule { List verifiedCategories = await Utilities.GetVerifiedChannels(); - if (verifiedCategories.Count == 0) return new List(); + if (verifiedCategories.Count == 0) + { + return new List(); + } verifiedCategories = verifiedCategories.OrderBy(x => x.name).ToList(); List selectionComponents = new List(); + int selectionOptions = 0; for (int selectionBoxes = 0; selectionBoxes < 5 && selectionOptions < verifiedCategories.Count; selectionBoxes++) { @@ -51,9 +55,15 @@ public class CreateSelectionBoxPanelCommand : ApplicationCommandModule public static async Task OnSelectionMenuUsed(DiscordInteraction interaction) { - if (interaction.Data.Values == null || interaction.Data.Values.Length <= 0) return; + if (interaction.Data.Values == null || interaction.Data.Values.Length <= 0) + { + return; + } - if (!ulong.TryParse(interaction.Data.Values[0], out ulong categoryID) || categoryID == 0) return; + if (!ulong.TryParse(interaction.Data.Values[0], out ulong categoryID) || categoryID == 0) + { + return; + } await interaction.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral()); diff --git a/Commands/NewCommand.cs b/Commands/NewCommand.cs index 0ae9d53..c914f3a 100644 --- a/Commands/NewCommand.cs +++ b/Commands/NewCommand.cs @@ -63,6 +63,7 @@ public class NewCommand : ApplicationCommandModule public static async Task CreateButtons(InteractionContext command, List verifiedCategories) { DiscordInteractionResponseBuilder builder = new DiscordInteractionResponseBuilder().WithContent(" "); + int nrOfButtons = 0; for (int nrOfButtonRows = 0; nrOfButtonRows < 5 && nrOfButtons < verifiedCategories.Count; nrOfButtonRows++) { @@ -82,6 +83,7 @@ public class NewCommand : ApplicationCommandModule { verifiedCategories = verifiedCategories.OrderBy(x => x.name).ToList(); List selectionComponents = new List(); + int selectionOptions = 0; for (int selectionBoxes = 0; selectionBoxes < 5 && selectionOptions < verifiedCategories.Count; selectionBoxes++) { @@ -106,7 +108,10 @@ public class NewCommand : ApplicationCommandModule stringID = interaction.Data.CustomId.Replace("supportchild_newcommandbutton ", ""); break; case ComponentType.StringSelect: - if (interaction.Data.Values == null || interaction.Data.Values.Length <= 0) return; + if (interaction.Data.Values == null || interaction.Data.Values.Length <= 0) + { + return; + } stringID = interaction.Data.Values[0]; break; @@ -116,7 +121,10 @@ public class NewCommand : ApplicationCommandModule return; } - if (!ulong.TryParse(stringID, out ulong categoryID) || categoryID == 0) return; + if (!ulong.TryParse(stringID, out ulong categoryID) || categoryID == 0) + { + return; + } await interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate, new DiscordInteractionResponseBuilder().AsEphemeral()); diff --git a/Database.cs b/Database.cs index f263972..d4d53dc 100644 --- a/Database.cs +++ b/Database.cs @@ -460,24 +460,10 @@ public static class Database public static List GetActiveStaff(params ulong[] ignoredUserIDs) { - bool first = true; - string filterString = ""; - foreach (ulong userID in ignoredUserIDs) - { - if (first) - { - first = false; - filterString += "AND user_id != " + userID; - } - else - { - filterString += "&& user_id != " + userID; - } - } - using MySqlConnection c = GetConnection(); c.Open(); - using MySqlCommand selection = new MySqlCommand(@"SELECT * FROM staff WHERE active = true " + filterString, c); + using MySqlCommand selection = new MySqlCommand(@"SELECT * FROM staff WHERE active = true AND user_id NOT IN (@user_ids)", c); + selection.Parameters.AddWithValue("@user_ids", string.Join(",", ignoredUserIDs)); selection.Prepare(); MySqlDataReader results = selection.ExecuteReader(); @@ -499,26 +485,10 @@ public static class Database public static List GetAllStaff(params ulong[] ignoredUserIDs) { - bool first = true; - string filterString = ""; - foreach (ulong userID in ignoredUserIDs) - { - if (first) - { - first = false; - filterString += "WHERE user_id != " + userID; - } - else - { - filterString += "&& user_id != " + userID; - } - - } - - using MySqlConnection c = GetConnection(); c.Open(); - using MySqlCommand selection = new MySqlCommand(@"SELECT * FROM staff " + filterString, c); + using MySqlCommand selection = new MySqlCommand(@"SELECT * FROM staff WHERE user_id NOT IN (@user_ids)", c); + selection.Parameters.AddWithValue("@user_ids", string.Join(",", ignoredUserIDs)); selection.Prepare(); MySqlDataReader results = selection.ExecuteReader(); diff --git a/EventHandler.cs b/EventHandler.cs index d2d0406..ea1d59f 100644 --- a/EventHandler.cs +++ b/EventHandler.cs @@ -50,6 +50,8 @@ internal static class EventHandler case BadRequestException ex: Logger.Error("JSON Message: " + ex.JsonMessage); break; + default: + break; } return Task.CompletedTask; }