diff --git a/src/Management/Commands/BanCommand.cs b/src/Management/Commands/BanCommand.cs new file mode 100644 index 0000000..0b26233 --- /dev/null +++ b/src/Management/Commands/BanCommand.cs @@ -0,0 +1,34 @@ +using System; +using System.Net.Http.Json; +using sodoffmmo.Attributes; +using sodoffmmo.Core; + +namespace sodoffmmo.Management.Commands; + +[ManagementCommand("ban", Role.Moderator)] +class BanCommand : IManagementCommand +{ + public void Handle(Client client, string[] arguments) + { + 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; + + 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; } + else { client.Send(Utils.BuildServerSideMessage("Empty Response", "Server")); } + } +}