Codacy refactoring

This commit is contained in:
Toastie (DCS Team) 2024-10-29 22:34:41 +13:00
parent ea1516e5db
commit 4bac506bfc
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
4 changed files with 29 additions and 39 deletions

View file

@ -30,10 +30,14 @@ public class CreateSelectionBoxPanelCommand : ApplicationCommandModule
{ {
List<Database.Category> verifiedCategories = await Utilities.GetVerifiedChannels(); List<Database.Category> verifiedCategories = await Utilities.GetVerifiedChannels();
if (verifiedCategories.Count == 0) return new List<DiscordSelectComponent>(); if (verifiedCategories.Count == 0)
{
return new List<DiscordSelectComponent>();
}
verifiedCategories = verifiedCategories.OrderBy(x => x.name).ToList(); verifiedCategories = verifiedCategories.OrderBy(x => x.name).ToList();
List<DiscordSelectComponent> selectionComponents = new List<DiscordSelectComponent>(); List<DiscordSelectComponent> selectionComponents = new List<DiscordSelectComponent>();
int selectionOptions = 0; int selectionOptions = 0;
for (int selectionBoxes = 0; selectionBoxes < 5 && selectionOptions < verifiedCategories.Count; selectionBoxes++) 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) 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()); await interaction.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral());

View file

@ -63,6 +63,7 @@ public class NewCommand : ApplicationCommandModule
public static async Task CreateButtons(InteractionContext command, List<Database.Category> verifiedCategories) public static async Task CreateButtons(InteractionContext command, List<Database.Category> verifiedCategories)
{ {
DiscordInteractionResponseBuilder builder = new DiscordInteractionResponseBuilder().WithContent(" "); DiscordInteractionResponseBuilder builder = new DiscordInteractionResponseBuilder().WithContent(" ");
int nrOfButtons = 0; int nrOfButtons = 0;
for (int nrOfButtonRows = 0; nrOfButtonRows < 5 && nrOfButtons < verifiedCategories.Count; nrOfButtonRows++) 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(); verifiedCategories = verifiedCategories.OrderBy(x => x.name).ToList();
List<DiscordSelectComponent> selectionComponents = new List<DiscordSelectComponent>(); List<DiscordSelectComponent> selectionComponents = new List<DiscordSelectComponent>();
int selectionOptions = 0; int selectionOptions = 0;
for (int selectionBoxes = 0; selectionBoxes < 5 && selectionOptions < verifiedCategories.Count; selectionBoxes++) 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 ", ""); stringID = interaction.Data.CustomId.Replace("supportchild_newcommandbutton ", "");
break; break;
case ComponentType.StringSelect: 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]; stringID = interaction.Data.Values[0];
break; break;
@ -116,7 +121,10 @@ public class NewCommand : ApplicationCommandModule
return; 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()); await interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate, new DiscordInteractionResponseBuilder().AsEphemeral());

View file

@ -460,24 +460,10 @@ public static class Database
public static List<StaffMember> GetActiveStaff(params ulong[] ignoredUserIDs) public static List<StaffMember> 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(); using MySqlConnection c = GetConnection();
c.Open(); 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(); selection.Prepare();
MySqlDataReader results = selection.ExecuteReader(); MySqlDataReader results = selection.ExecuteReader();
@ -499,26 +485,10 @@ public static class Database
public static List<StaffMember> GetAllStaff(params ulong[] ignoredUserIDs) public static List<StaffMember> 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(); using MySqlConnection c = GetConnection();
c.Open(); 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(); selection.Prepare();
MySqlDataReader results = selection.ExecuteReader(); MySqlDataReader results = selection.ExecuteReader();

View file

@ -50,6 +50,8 @@ internal static class EventHandler
case BadRequestException ex: case BadRequestException ex:
Logger.Error("JSON Message: " + ex.JsonMessage); Logger.Error("JSON Message: " + ex.JsonMessage);
break; break;
default:
break;
} }
return Task.CompletedTask; return Task.CompletedTask;
} }