Skip to content

Commit

Permalink
KickTime in config, and gag
Browse files Browse the repository at this point in the history
  • Loading branch information
daffyyyy committed Dec 3, 2023
1 parent 5ea71f1 commit 0f32daa
Show file tree
Hide file tree
Showing 7 changed files with 387 additions and 11 deletions.
3 changes: 2 additions & 1 deletion BanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void AddBanBySteamid(string playerSteamId, CCSPlayerController? issuer, s

var sql = "INSERT INTO `sa_bans` (`player_steamid`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`) " +
"VALUES (@playerSteamid, @adminSteamid, @adminName, @banReason, @duration, @ends, @created)";

_dbConnection.Execute(sql, new
{
playerSteamid = playerSteamId,
Expand Down Expand Up @@ -90,7 +91,7 @@ public void UnbanPlayer(string playerPattern)

_dbConnection.Open();

string sqlUnban = "UPDATE sa_bans SET status = 'UNBANNED' WHERE player_steamid = @pattern OR player_name = @pattern";
string sqlUnban = "UPDATE sa_bans SET status = 'UNBANNED' WHERE player_steamid = @pattern OR player_name = @pattern AND status = 'ACTIVE'";
_dbConnection.Execute(sqlUnban, new { pattern = playerPattern });

_dbConnection.Close();
Expand Down
184 changes: 179 additions & 5 deletions CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.Logging;
Expand All @@ -12,6 +13,8 @@
namespace CS2_SimpleAdmin;
public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdminConfig>
{
public List<int> gaggedPlayers = new List<int>();

internal string dbConnectionString = string.Empty;
public override string ModuleName => "CS2-SimpleAdmin";
public override string ModuleDescription => "";
Expand Down Expand Up @@ -70,6 +73,24 @@ public void OnConfigParsed(CS2_SimpleAdminConfig config)
MySqlCommand command = new MySqlCommand(sql, connection);
command.ExecuteNonQuery();

sql = @"CREATE TABLE IF NOT EXISTS `sa_mutes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`player_steamid` varchar(64) NOT NULL,
`player_name` varchar(128) NOT NULL,
`admin_steamid` varchar(64) NOT NULL,
`admin_name` varchar(128) NOT NULL,
`reason` varchar(255) NOT NULL,
`duration` int(11) NOT NULL,
`ends` timestamp NOT NULL,
`created` timestamp NOT NULL,
`type` enum('GAG','MUTE','') NOT NULL DEFAULT 'GAG',
`status` enum('ACTIVE','UNMUTED','EXPIRED','') NOT NULL DEFAULT 'ACTIVE',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci";

command = new MySqlCommand(sql, connection);
command.ExecuteNonQuery();

connection.Close();
}

Expand Down Expand Up @@ -105,25 +126,176 @@ public void OnKickCommand(CCSPlayerController? caller, CommandInfo command)
return;

player!.Pawn.Value!.Freeze();
string reason = "Brak powodu";
string reason = "Unknown";

if (command.ArgCount >= 2)
reason = command.GetArg(2);

if (command.ArgCount >= 2)
{
player!.PrintToCenter($"{Config.Messages.PlayerKickMessage}".Replace("{REASON}", reason).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName));
AddTimer(10.0f, () => Helper.KickPlayer(player!.UserId, reason));
AddTimer(Config.KickTime, () => Helper.KickPlayer(player!.UserId, reason));

}
else
{
AddTimer(10.0f, () => Helper.KickPlayer(player!.UserId));
AddTimer(Config.KickTime, () => Helper.KickPlayer(player!.UserId));
}

Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminKickMessage}".Replace("{REASON}", reason).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
}

[ConsoleCommand("css_gag")]
[RequiresPermissions("@css/chat")]
[CommandHelper(minArgs: 1, usage: "<#userid or name> [time in minutes/0 perm] [reason]", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
public void OnGagCommand(CCSPlayerController? caller, CommandInfo command)
{
if (!GetTarget(command, out var player))
return;
if (command.ArgCount < 2)
return;

int time = 0;
string reason = "Unknown";

MuteManager _muteManager = new(dbConnectionString);

int.TryParse(command.GetArg(2), out time);

if (command.ArgCount >= 3)
reason = command.GetArg(3);

_muteManager.MutePlayer(player, caller, reason, time, 0);

if (!gaggedPlayers.Contains((int)player!.Index))
gaggedPlayers.Add((int)player.Index);

if (time == 0)
{
player!.PrintToCenter($"{Config.Messages.PlayerGagMessagePerm}".Replace("{REASON}", reason).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName));
Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminGagMessagePerm}".Replace("{REASON}", reason).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
}
else
{
player!.PrintToCenter($"{Config.Messages.PlayerGagMessageTime}".Replace("{REASON}", reason).Replace("{TIME}", time.ToString()).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName));
Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminGagMessageTime}".Replace("{REASON}", reason).Replace("{TIME}", time.ToString()).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
}
}

[ConsoleCommand("css_addgag")]
[RequiresPermissions("@css/chat")]
[CommandHelper(minArgs: 1, usage: "<steamid> [time in minutes/0 perm] [reason]", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command)
{
if (command.ArgCount < 2)
return;
if (string.IsNullOrEmpty(command.GetArg(1))) return;

string steamid = command.GetArg(1);

if (!Helper.IsValidSteamID64(steamid))
{
command.ReplyToCommand($"Invalid SteamID64.");
return;
}

int time = 0;
string reason = "Unknown";

MuteManager _muteManager = new(dbConnectionString);

int.TryParse(command.GetArg(2), out time);

if (command.ArgCount >= 3)
reason = command.GetArg(3);

_muteManager.AddMuteBySteamid(steamid, caller, reason, time, 0);

List<CCSPlayerController> matches = Helper.GetPlayerFromSteamid64(steamid);
if (matches.Count == 1)
{
CCSPlayerController? player = matches.FirstOrDefault();
if (player != null)
{
if (time == 0)
{
player!.PrintToCenter($"{Config.Messages.PlayerGagMessagePerm}".Replace("{REASON}", reason).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName));
Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminGagMessagePerm}".Replace("{REASON}", reason).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
}
else
{
player!.PrintToCenter($"{Config.Messages.PlayerGagMessageTime}".Replace("{REASON}", reason).Replace("{TIME}", time.ToString()).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName));
Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminGagMessageTime}".Replace("{REASON}", reason).Replace("{TIME}", time.ToString()).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
}

if (!gaggedPlayers.Contains((int)player.Index))
gaggedPlayers.Add((int)player.Index);
}
}
command.ReplyToCommand($"Gagged player with steamid {steamid}.");
}

[ConsoleCommand("css_unmute")]
[RequiresPermissions("@css/chat")]
[CommandHelper(minArgs: 1, usage: "<steamid or name> <type [gag/mute]>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command)
{
if (command.GetArg(1).Length <= 1)
{
command.ReplyToCommand($"Too short pattern to search.");
return;
}

string pattern = command.GetArg(1);

MuteManager _muteManager = new(dbConnectionString);

if (Helper.IsValidSteamID64(pattern))
{
List<CCSPlayerController> matches = Helper.GetPlayerFromSteamid64(pattern);
if (matches.Count == 1)
{
CCSPlayerController? player = matches.FirstOrDefault();
if (player != null)
{
gaggedPlayers.Remove((int)player.Index);
}
}
}
else
{
List<CCSPlayerController> matches = Helper.GetPlayerFromName(pattern);
if (matches.Count == 1)
{
CCSPlayerController? player = matches.FirstOrDefault();
if (player != null)
{
gaggedPlayers.Remove((int)player.Index);
}
}
}

if (command.ArgCount >= 3)
{
string? action = command.GetArg(2)?.ToLower();

if (action == "gag")
{
_muteManager.UnmutePlayer(pattern, 0); // Unmute by type 0 (gag)
}
else if (action == "mute")
{
_muteManager.UnmutePlayer(pattern, 1); // Unmute by type 1 (mute)
}
}
else
{
_muteManager.UnmutePlayer(pattern, 2); // Default unmute (all types)
}

command.ReplyToCommand($"Unmuted player with pattern {pattern}.");
}

[ConsoleCommand("css_ban")]
[RequiresPermissions("@css/ban")]
[CommandHelper(minArgs: 1, usage: "<#userid or name> [time in minutes/0 perm] [reason]", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
Expand Down Expand Up @@ -159,7 +331,7 @@ public void OnBanCommand(CCSPlayerController? caller, CommandInfo command)
Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminBanMessageTime}".Replace("{REASON}", reason).Replace("{TIME}", time.ToString()).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
}

AddTimer(10.0f, () => Helper.KickPlayer(player!.UserId));
AddTimer(Config.KickTime, () => Helper.KickPlayer(player!.UserId));
}

[ConsoleCommand("css_addban")]
Expand Down Expand Up @@ -197,6 +369,8 @@ public void OnAddBanCommand(CCSPlayerController? caller, CommandInfo command)
CCSPlayerController? player = matches.FirstOrDefault();
if (player != null)
{
player!.Pawn.Value!.Freeze();

if (time == 0)
{
player!.PrintToCenter($"{Config.Messages.PlayerBanMessagePerm}".Replace("{REASON}", reason).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName));
Expand All @@ -208,7 +382,7 @@ public void OnAddBanCommand(CCSPlayerController? caller, CommandInfo command)
Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminBanMessageTime}".Replace("{REASON}", reason).Replace("{TIME}", time.ToString()).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
}

AddTimer(10.0f, () => Helper.KickPlayer(player.UserId));
AddTimer(Config.KickTime, () => Helper.KickPlayer(player.UserId));
}
}
command.ReplyToCommand($"Banned player with steamid {steamid}.");
Expand Down
11 changes: 10 additions & 1 deletion Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ public class Messages
public string PlayerBanMessagePerm { get; set; } = "You have been banned permanently for {REASON} by {ADMIN}!";
[JsonPropertyName("PlayerKickMessage")]
public string PlayerKickMessage { get; set; } = "You have been kicked for {REASON} by {ADMIN}!";
[JsonPropertyName("PlayerGagMessageTime")]
public string PlayerGagMessageTime { get; set; } = "You have been gagged for {REASON} for {TIME} minutes by {ADMIN}!";
[JsonPropertyName("PlayerGagMessagePerm")]
public string PlayerGagMessagePerm { get; set; } = "You have been gagged permanently for {REASON} by {ADMIN}!";
[JsonPropertyName("AdminBanMessageTime")]
public string AdminBanMessageTime { get; set; } = "Admin {ADMIN} banned {PLAYER} for {REASON} for {TIME} minutes!";
[JsonPropertyName("AdminBanMessagePerm")]
public string AdminBanMessagePerm { get; set; } = "Admin {ADMIN} banned {PLAYER} permanently for {REASON}";

[JsonPropertyName("AdminKickMessage")]
public string AdminKickMessage { get; set; } = "Admin {ADMIN} kicked {PLAYER} for {REASON}!";
[JsonPropertyName("AdminGagMessageTime")]
public string AdminGagMessageTime { get; set; } = "Admin {ADMIN} gagged {PLAYER} for {REASON} for {TIME} minutes!";
[JsonPropertyName("AdminGagMessagePerm")]
public string AdminGagMessagePerm { get; set; } = "Admin {ADMIN} gagged {PLAYER} permanently for {REASON}";
[JsonPropertyName("AdminSlayMessage")]
public string AdminSlayMessage { get; set; } = "Admin {ADMIN} slayed {PLAYER}!";
[JsonPropertyName("AdminSlapMessage")]
Expand Down Expand Up @@ -63,6 +70,8 @@ public class CS2_SimpleAdminConfig : BasePluginConfig

[JsonPropertyName("Prefix")]
public string Prefix { get; set; } = "{GREEN}[SimpleAdmin]";
[JsonPropertyName("KickTime")]
public int KickTime { get; set; } = 10;

[JsonPropertyName("Messages")]
public Messages Messages { get; set; } = new Messages();
Expand Down
54 changes: 52 additions & 2 deletions Events.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities;
using static CounterStrikeSharp.API.Core.Listeners;

namespace CS2_SimpleAdmin
{
public partial class CS2_SimpleAdmin
{
private void registerEvents()
{
RegisterListener<Listeners.OnClientAuthorized>(OnClientAuthorized);
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<OnClientAuthorized>(OnClientAuthorized);
RegisterListener<OnClientDisconnect>(OnClientDisconnect);
RegisterListener<OnMapStart>(OnMapStart);
AddCommandListener("say", OnCommandSay);
AddCommandListener("say_team", OnCommandSay);
}

private HookResult OnCommandSay(CCSPlayerController? player, CommandInfo info)
{
if (player == null || !player.IsValid || info.GetArg(1).Length == 0) return HookResult.Continue;

if (gaggedPlayers.Contains((int)player.Index))
{
return HookResult.Handled;
}

return HookResult.Continue;
}

private void OnClientAuthorized(int playerSlot, SteamID steamID)
Expand All @@ -28,20 +45,53 @@ private void OnClientAuthorized(int playerSlot, SteamID steamID)
}

BanManager _banManager = new(dbConnectionString);
MuteManager _muteManager = new(dbConnectionString);
bool isBanned = _banManager.IsPlayerBanned(player.AuthorizedSteamID.SteamId64.ToString());
List<dynamic> activeMutes = _muteManager.IsPlayerMuted(player.AuthorizedSteamID.SteamId64.ToString());

if (activeMutes.Count > 0)
{
// Player is muted, handle mute
foreach (var mute in activeMutes)
{
string muteType = mute.type;

if (muteType == "GAG")
{
if (!gaggedPlayers.Contains((int)player.Index))
gaggedPlayers.Add((int)player.Index);
}
else
{
continue;
}
}
}

// Player is banned, kick him
if (isBanned)
{
Helper.KickPlayer(player.UserId, "Banned");
}
}

private void OnClientDisconnect(int playerSlot)
{
CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot);

if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;

gaggedPlayers.Remove((int)player.Index);
}

private void OnMapStart(string mapName)
{
AddTimer(120.0f, () =>
{
BanManager _banManager = new(dbConnectionString);
_banManager.ExpireOldBans();
MuteManager _muteManager = new(dbConnectionString);
_muteManager.ExpireOldMutes();
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
}
}
Expand Down
5 changes: 4 additions & 1 deletion Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public static List<CCSPlayerController> GetPlayerFromName(string name)

public static List<CCSPlayerController> GetPlayerFromSteamid64(string steamid)
{
return Utilities.GetPlayers().FindAll(x => x.AuthorizedSteamID!.SteamId64.ToString().Equals(steamid));
return Utilities.GetPlayers().FindAll(x =>
x.AuthorizedSteamID != null &&
x.AuthorizedSteamID.SteamId64.ToString().Equals(steamid, StringComparison.OrdinalIgnoreCase)
);
}

public static bool IsValidSteamID64(string input)
Expand Down
Loading

0 comments on commit 0f32daa

Please sign in to comment.