Merge pull request 'Moderation Commands' (#1) from moderation-commands into main

Reviewed-on: https://gitea.milenia.local.alanmoon.net/Moonbase/sodoff-mmo/pulls/1
This commit is contained in:
Alan Moon 2025-03-07 18:15:42 -08:00
commit a7aae8763e
5 changed files with 102 additions and 3 deletions

View File

@ -1,4 +1,6 @@
using sodoffmmo.Attributes;
using System.Net;
using System.Net.Http.Json;
using sodoffmmo.Attributes;
using sodoffmmo.Core;
using sodoffmmo.Data;
using sodoffmmo.Management;
@ -37,6 +39,13 @@ class ChatMessageHandler : CommandHandler {
return;
}
// send an http request to api to check for 'IndefiniteOpenChatBan' or 'TemporaryOpenChatBan'
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; }
client.Room.Send(Utils.BuildChatMessage(client.PlayerData.Uid, message, client.PlayerData.DiplayName), client);
NetworkObject cmd = new();

54
src/Core/ApiWebService.cs Normal file
View File

@ -0,0 +1,54 @@
using System;
using System.Net.Http.Json;
using sodoffmmo.CommandHandlers;
namespace sodoffmmo.Core;
public class ApiWebService
{
public UserBanType? CheckForUserBan(Client client)
{
HttpClient httpClient = new();
var content = new FormUrlEncodedContent(
new Dictionary<string, string> {
{ "token", client.PlayerData.UNToken }
}
);
httpClient.Timeout = new TimeSpan(0, 0, 3);
try
{
var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/CheckForVikingBan", content).Result;
Log("Moderation/CheckForVikingBan");
if (response.StatusCode == System.Net.HttpStatusCode.OK && response.Content != null) return response.Content.ReadFromJsonAsync<UserBanType>().Result;
else return null;
} catch (Exception e) { LogError(e.Message); return null; }
}
public string? BanUser(Client client, string userId, string banType, string days)
{
HttpClient httpClient = new();
var content = new FormUrlEncodedContent(
new Dictionary<string, string> {
{ "token", client.PlayerData.UNToken },
{ "userId", userId },
{ "banType", banType },
{ "days", days }
}
);
httpClient.Timeout = new TimeSpan(0, 0, 3);
try
{
var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/AddBanToVikingByGuid", content).Result;
Log("Moderation/AddBanToVikingByGuid");
if (response.StatusCode == System.Net.HttpStatusCode.OK && response.Content != null) return response.Content.ReadAsStringAsync().Result;
else return null;
} catch (Exception e) { LogError(e.Message); return null; }
}
private void Log(string endpoint) => Console.WriteLine($"Sent API Request To {Configuration.ServerConfiguration.ApiUrl}/{endpoint}");
private void LogError(string message) => Console.WriteLine($"An Error Has Occured When Sending An API Request - {message}");
}

10
src/Core/UserBanType.cs Normal file
View File

@ -0,0 +1,10 @@
namespace sodoffmmo.Core;
public enum UserBanType
{
NotBanned = 0,
IndefiniteOpenChatBan = 1,
TemporaryOpenChatBan = 2,
IndefiniteAccountBan = 3,
TemporaryAccountBan = 4
}

View File

@ -0,0 +1,26 @@
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; }
if (arguments[0] == "help") client.Send(Utils.BuildServerSideMessage($"::ban - This bans a user who is in-room. First argument is the id of the user in-room. Room user lists start at 0.", "Server"));
var clientToBan = client.Room!.Clients.ToArray()[int.Parse(arguments[0])].PlayerData.Uid;
if (clientToBan == null) { client.Send(Utils.BuildServerSideMessage($"User Could Not Be Found", "Server")); return; }
// send an http request to the api set in appsettings
ApiWebService apiWebService = new();
var response = apiWebService.BanUser(client, clientToBan, arguments[1], arguments[2]);
if (response != null) { client.Send(Utils.BuildServerSideMessage("User Banned Successfully", "Server")); return; }
else { client.Send(Utils.BuildServerSideMessage("Empty Response", "Server")); }
}
}

View File

@ -61,9 +61,9 @@
"// Authentication Optional": "authentication is required only for moderation activities",
"// Authentication RequiredForChat": "authentication is required only for moderation activities and using chat (if chat is enabled)",
"// Authentication Required": "authentication is required to connect to mmo",
"Authentication": "Disabled",
"Authentication": "Required",
"// ApiUrl": "SoDOff API server URL for authentication calls",
"// ApiUrl": "SoDOff API server URL for authentication calls and other calls",
"ApiUrl": "http://localhost:5000",
"// BypassToken": "Token allowed to connect without authentication",