diff --git a/src/CommandHandlers/ChatMessageHandler.cs b/src/CommandHandlers/ChatMessageHandler.cs index 066f574..fb754fc 100644 --- a/src/CommandHandlers/ChatMessageHandler.cs +++ b/src/CommandHandlers/ChatMessageHandler.cs @@ -40,16 +40,8 @@ class ChatMessageHandler : CommandHandler { } // send an http request to api to check for 'IndefiniteOpenChatBan' or 'TemporaryOpenChatBan' - HttpClient httpClient = new(); - var content = new FormUrlEncodedContent( - new Dictionary { - { "token", client.PlayerData.UNToken } - } - ); - - httpClient.Timeout = new TimeSpan(0, 0, 3); - var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/CheckForVikingBan", content).Result; - UserBanType? banType = response.Content.ReadFromJsonAsync().Result; + ApiWebService apiWebService = new(); + var banType = apiWebService.CheckForUserBan(client); if (banType != null && (banType == UserBanType.IndefiniteOpenChatBan || banType == UserBanType.TemporaryOpenChatBan)) { client.Send(Utils.ArrNetworkPacket(new string[] { "SMF", "-1", "CB", "1", "Sorry, You've Been Banned From Using Type Chat", "1" }, "SMF")); return; } diff --git a/src/Core/ApiWebService.cs b/src/Core/ApiWebService.cs new file mode 100644 index 0000000..5612a38 --- /dev/null +++ b/src/Core/ApiWebService.cs @@ -0,0 +1,38 @@ +using System; +using System.Net.Http.Json; + +namespace sodoffmmo.Core; + +public class ApiWebService +{ + public UserBanType? CheckForUserBan(Client client) + { + HttpClient httpClient = new(); + var content = new FormUrlEncodedContent( + new Dictionary { + { "token", client.PlayerData.UNToken } + } + ); + httpClient.Timeout = new TimeSpan(0, 0, 3); + + var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/CheckForVikingBan", content).Result; + if (response.StatusCode == System.Net.HttpStatusCode.OK && response.Content != null) return response.Content.ReadFromJsonAsync().Result; + else return null; + } + + public HttpResponseMessage BanUser(Client client, string userId, string banType, string days) + { + HttpClient httpClient = new(); + var content = new FormUrlEncodedContent( + new Dictionary { + { "token", client.PlayerData.UNToken }, + { "userId", userId }, + { "banType", banType }, + { "days", days } + } + ); + httpClient.Timeout = new TimeSpan(0, 0, 3); + var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/AddBanToVikingByGuid", content).Result; + return response; + } +} diff --git a/src/Management/Commands/BanCommand.cs b/src/Management/Commands/BanCommand.cs index 0b26233..a772172 100644 --- a/src/Management/Commands/BanCommand.cs +++ b/src/Management/Commands/BanCommand.cs @@ -13,19 +13,9 @@ class BanCommand : IManagementCommand if(arguments.Length < 2) { client.Send(Utils.BuildServerSideMessage($"Expected 3 Args, Got {arguments.Length + 1}", "Server")); return; } // send an http request to the api set in appsettings - HttpClient httpClient = new(); - var content = new FormUrlEncodedContent( - new Dictionary { - { "token", client.PlayerData.UNToken }, - { "userId", arguments[0] }, - { "banType", arguments[1] }, - { "days", arguments[2] } - } - ); - - httpClient.Timeout = new TimeSpan(0, 0, 3); - var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/AddBanToVikingByGuid", content).Result; - string? responseString = response.Content.ReadAsStringAsync().Result; + ApiWebService apiWebService = new(); + var response = apiWebService.BanUser(client, arguments[0], arguments[1], arguments[2]); + var responseString = response.Content.ReadAsStringAsync().Result; if (response.StatusCode != System.Net.HttpStatusCode.OK && responseString != null) { client.Send(Utils.BuildServerSideMessage(responseString, "Server")); return; } else if (responseString != null) { client.Send(Utils.BuildServerSideMessage("User Banned Successfully", "Server")); return; }