Skip to content

Commit

Permalink
1.6.2a
Browse files Browse the repository at this point in the history
```diff
+ Added duration and reason menu when admin try to ban/mute without specific time
+ Added `NotifyPenaltiesToAdminOnConnect` config setting to disable notifications globally
+ Added immunity check when using game vote to kick player

- Removed `Discord.Net.Webhook` package (too heavy)
```
  • Loading branch information
daffyyyy committed Oct 5, 2024
1 parent bd817d6 commit 6847da2
Show file tree
Hide file tree
Showing 20 changed files with 533 additions and 329 deletions.
17 changes: 9 additions & 8 deletions CS2-SimpleAdmin/CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Capabilities;
using CounterStrikeSharp.API.Core.Commands;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Commands.Targeting;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CS2_SimpleAdmin.Managers;
using CS2_SimpleAdminApi;
using Discord.Webhook;
using Microsoft.Extensions.Logging;
using MySqlConnector;

namespace CS2_SimpleAdmin;

[MinimumApiVersion(260)]
[MinimumApiVersion(271)]
public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdminConfig>
{
internal static CS2_SimpleAdmin Instance { get; private set; } = new();

public override string ModuleName => "CS2-SimpleAdmin" + (Helper.IsDebugBuild ? " (DEBUG)" : " (RELEASE)");
public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)";
public override string ModuleAuthor => "daffyy & Dliix66";
public override string ModuleVersion => "1.6.1b";

public CS2_SimpleAdminConfig Config { get; set; } = new();

public override string ModuleVersion => "1.6.2a";

public override void Load(bool hotReload)
{
Instance = this;
Expand Down Expand Up @@ -98,6 +94,11 @@ public void OnConfigParsed(CS2_SimpleAdminConfig config)
}

Task.Run(() => Database.DatabaseMigration());

PermissionManager = new PermissionManager(Database);
BanManager = new BanManager(Database);
MuteManager = new MuteManager(Database);
WarnManager = new WarnManager(Database);

Config = config;
Helper.UpdateConfig(config);
Expand All @@ -110,7 +111,7 @@ public void OnConfigParsed(CS2_SimpleAdminConfig config)
_localizer = Localizer;

if (!string.IsNullOrEmpty(Config.Discord.DiscordLogWebhook))
DiscordWebhookClientLog = new DiscordWebhookClient(Config.Discord.DiscordLogWebhook);
DiscordWebhookClientLog = new DiscordManager(Config.Discord.DiscordLogWebhook);

PluginInfo.ShowAd(ModuleVersion);
if (Config.EnableUpdateCheck)
Expand Down
1 change: 0 additions & 1 deletion CS2-SimpleAdmin/CS2-SimpleAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.266" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Discord.Net.Webhook" Version="3.16.0" />
<PackageReference Include="MySqlConnector" Version="2.3.7" />
<PackageReference Include="Newtonsoft.Json" Version="*" />
</ItemGroup>
Expand Down
33 changes: 15 additions & 18 deletions CS2-SimpleAdmin/Commands/basebans.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.ValveConstants.Protobuf;
using CS2_SimpleAdmin.Managers;
using CS2_SimpleAdmin.Menus;
using CS2_SimpleAdminApi;

namespace CS2_SimpleAdmin;
Expand All @@ -30,21 +30,22 @@ public void OnBanCommand(CCSPlayerController? caller, CommandInfo command)
{
return;
}

Database.Database database = new(DbConnectionString);
BanManager banManager = new(database, Config);

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


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

playersToTarget.ForEach(player =>
{
if (caller!.CanTarget(player))
if (!caller.CanTarget(player)) return;

if (!int.TryParse(command.GetArg(2), out var time) && caller != null && caller.IsValid)
{
Ban(caller, player, time, reason, callerName, banManager, command);
DurationMenu.OpenMenu(caller, $"{_localizer?["sa_ban"] ?? "Ban"}: {player.PlayerName}", player,
ManagePlayersMenu.BanMenu);
return;
}

Ban(caller, player, time, reason, callerName, BanManager, command);
});
}

Expand All @@ -70,7 +71,7 @@ internal void Ban(CCSPlayerController? caller, CCSPlayerController player, int t
// Asynchronously handle banning logic
Task.Run(async () =>
{
await (banManager ??= new BanManager(Database, Config)).BanPlayer(playerInfo, adminInfo, reason, time);
await BanManager.BanPlayer(playerInfo, adminInfo, reason, time);
});

// Update banned players list
Expand Down Expand Up @@ -168,8 +169,7 @@ public void OnAddBanCommand(CCSPlayerController? caller, CommandInfo command)
// Asynchronous ban operation if player is not online or not found
Task.Run(async () =>
{
var banManager = new BanManager(Database, Config);
await banManager.AddBanBySteamid(steamid, adminInfo, reason, time);
await BanManager.AddBanBySteamid(steamid, adminInfo, reason, time);
});

command.ReplyToCommand($"Player with steamid {steamid} is not online. Ban has been added offline.");
Expand Down Expand Up @@ -224,8 +224,7 @@ public void OnBanIpCommand(CCSPlayerController? caller, CommandInfo command)
// Asynchronous ban operation if player is not online or not found
Task.Run(async () =>
{
var banManager = new BanManager(Database, Config);
await banManager.AddBanByIp(ipAddress, adminInfo, reason, time);
await BanManager.AddBanByIp(ipAddress, adminInfo, reason, time);
});

command.ReplyToCommand($"Player with ip {ipAddress} is not online. Ban has been added offline.");
Expand Down Expand Up @@ -269,8 +268,7 @@ public void OnUnbanCommand(CCSPlayerController? caller, CommandInfo command)
var pattern = command.GetArg(1);
var reason = command.GetArg(2);

BanManager banManager = new(Database, Config);
Task.Run(async () => await banManager.UnbanPlayer(pattern, callerSteamId, reason));
Task.Run(async () => await BanManager.UnbanPlayer(pattern, callerSteamId, reason));

Helper.LogCommand(caller, command);

Expand Down Expand Up @@ -406,8 +404,7 @@ public void OnUnwarnCommand(CCSPlayerController? caller, CommandInfo command)

var pattern = command.GetArg(1);

WarnManager warnManager = new(Database);
Task.Run(async () => await warnManager.UnwarnPlayer(pattern));
Task.Run(async () => await WarnManager.UnwarnPlayer(pattern));

Helper.LogCommand(caller, command);
command.ReplyToCommand($"Unwarned player with pattern {pattern}.");
Expand Down
21 changes: 6 additions & 15 deletions CS2-SimpleAdmin/Commands/basecommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ public void OnPenaltiesCommand(CCSPlayerController? caller, CommandInfo command)

Task.Run(async () =>
{
// Initialize managers
MuteManager muteManager = new(Database);
WarnManager warnManager = new(Database);

try
{
var warns = await warnManager.GetPlayerWarns(PlayersInfo[userId], false);
var warns = await WarnManager.GetPlayerWarns(PlayersInfo[userId], false);

// Check if the player is muted
var activeMutes = await muteManager.IsPlayerMuted(PlayersInfo[userId].SteamId.SteamId64.ToString());
var activeMutes = await MuteManager.IsPlayerMuted(PlayersInfo[userId].SteamId.SteamId64.ToString());

Dictionary<PenaltyType, List<string>> mutesList = new()
{
Expand Down Expand Up @@ -221,8 +217,7 @@ public void OnDelAdminCommand(CCSPlayerController? caller, CommandInfo command)
public void RemoveAdmin(CCSPlayerController? caller, string steamid, bool globalDelete = false, CommandInfo? command = null)
{
if (Database == null) return;
PermissionManager adminManager = new(Database);
_ = adminManager.DeleteAdminBySteamId(steamid, globalDelete);
_ = PermissionManager.DeleteAdminBySteamId(steamid, globalDelete);

AddTimer(2, () =>
{
Expand Down Expand Up @@ -313,8 +308,7 @@ public void OnDelGroupCommand(CCSPlayerController? caller, CommandInfo command)
private void RemoveGroup(CCSPlayerController? caller, string name, CommandInfo? command = null)
{
if (Database == null) return;
PermissionManager adminManager = new(Database);
_ = adminManager.DeleteGroup(name);
_ = PermissionManager.DeleteGroup(name);

AddTimer(2, () =>
{
Expand Down Expand Up @@ -551,9 +545,6 @@ public void OnWarnsCommand(CCSPlayerController? caller, CommandInfo command)
if (playersToTarget.Count > 1)
return;

Database.Database database = new(DbConnectionString);
WarnManager warnManager = new(database);

playersToTarget.ForEach(player =>
{
if (!player.UserId.HasValue) return;
Expand All @@ -565,7 +556,7 @@ public void OnWarnsCommand(CCSPlayerController? caller, CommandInfo command)

Task.Run(async () =>
{
var warnsList = await warnManager.GetPlayerWarns(PlayersInfo[userId], false);
var warnsList = await WarnManager.GetPlayerWarns(PlayersInfo[userId], false);
var sortedWarns = warnsList
.OrderBy(warn => (string)warn.status == "ACTIVE" ? 0 : 1)
.ThenByDescending(warn => (int)warn.id)
Expand All @@ -576,7 +567,7 @@ public void OnWarnsCommand(CCSPlayerController? caller, CommandInfo command)
warnsMenu?.AddMenuOption($"[{((string)w.status == "ACTIVE" ? $"{ChatColors.LightRed}X" : $"{ChatColors.Lime}✔️")}{ChatColors.Default}] {(string)w.reason}",
(controller, option) =>
{
_ = warnManager.UnwarnPlayer(PlayersInfo[userId], (int)w.id);
_ = WarnManager.UnwarnPlayer(PlayersInfo[userId], (int)w.id);
player.PrintToChat(_localizer["sa_admin_warns_unwarn", player.PlayerName, (string)w.reason]);
});
});
Expand Down
59 changes: 30 additions & 29 deletions CS2-SimpleAdmin/Commands/basecomms.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 CS2_SimpleAdmin.Managers;
using CS2_SimpleAdmin.Menus;
using CS2_SimpleAdminApi;

namespace CS2_SimpleAdmin;
Expand All @@ -27,20 +28,21 @@ public void OnGagCommand(CCSPlayerController? caller, CommandInfo command)
{
return;
}

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


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

MuteManager muteManager = new(Database);

playersToTarget.ForEach(player =>
{
if (caller!.CanTarget(player))
if (!caller!.CanTarget(player)) return;
if (!int.TryParse(command.GetArg(2), out var time) && caller != null && caller.IsValid)
{
Gag(caller, player, time, reason, callerName, muteManager, command);
DurationMenu.OpenMenu(caller, $"{_localizer?["sa_gag"] ?? "Gag"}: {player.PlayerName}", player,
ManagePlayersMenu.GagMenu);
return;
}

Gag(caller, player, time, reason, callerName, MuteManager, command);
});
}

Expand Down Expand Up @@ -124,7 +126,6 @@ public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command)
? command.GetArg(3)
: (_localizer?["sa_unknown"] ?? "Unknown");

MuteManager muteManager = new(Database);
int.TryParse(command.GetArg(2), out var time);

// Get player and admin info
Expand All @@ -140,14 +141,14 @@ public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command)
if (!caller.CanTarget(player)) return;

// Perform the gag for an online player
Gag(caller, player, time, reason, callerName, muteManager, silent: true);
Gag(caller, player, time, reason, callerName, MuteManager, silent: true);
}
else
{
// Asynchronous gag operation for offline players
Task.Run(async () =>
{
await muteManager.AddMuteBySteamid(steamid, adminInfo, reason, time);
await MuteManager.AddMuteBySteamid(steamid, adminInfo, reason, time);
});

command.ReplyToCommand($"Player with steamid {steamid} is not online. Gag has been added offline.");
Expand Down Expand Up @@ -244,19 +245,20 @@ public void OnMuteCommand(CCSPlayerController? caller, CommandInfo command)
return;
}

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

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

MuteManager muteManager = new(Database);

playersToTarget.ForEach(player =>
{
if (caller!.CanTarget(player))
if (!caller!.CanTarget(player)) return;
if (!int.TryParse(command.GetArg(2), out var time) && caller != null && caller.IsValid)
{
Mute(caller, player, time, reason, callerName, muteManager, command);
DurationMenu.OpenMenu(caller, $"{_localizer?["sa_mute"] ?? "Mute"}: {player.PlayerName}", player,
ManagePlayersMenu.MuteMenu);
return;
}

Mute(caller, player, time, reason, callerName, MuteManager, command);
});
}

Expand Down Expand Up @@ -343,7 +345,6 @@ public void OnAddMuteCommand(CCSPlayerController? caller, CommandInfo command)
? command.GetArg(3)
: (_localizer?["sa_unknown"] ?? "Unknown");

MuteManager muteManager = new(Database);
int.TryParse(command.GetArg(2), out var time);

// Get player and admin info
Expand All @@ -359,14 +360,14 @@ public void OnAddMuteCommand(CCSPlayerController? caller, CommandInfo command)
if (!caller.CanTarget(player)) return;

// Perform the mute for an online player
Mute(caller, player, time, reason, callerName, muteManager, silent: true);
Mute(caller, player, time, reason, callerName, MuteManager, silent: true);
}
else
{
// Asynchronous mute operation for offline players
Task.Run(async () =>
{
await muteManager.AddMuteBySteamid(steamid, adminInfo, reason, time, 1);
await MuteManager.AddMuteBySteamid(steamid, adminInfo, reason, time, 1);
});

command.ReplyToCommand($"Player with steamid {steamid} is not online. Mute has been added offline.");
Expand Down Expand Up @@ -464,20 +465,21 @@ public void OnSilenceCommand(CCSPlayerController? caller, CommandInfo command)
{
return;
}

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


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

MuteManager muteManager = new(Database);

playersToTarget.ForEach(player =>
{
if (caller!.CanTarget(player))
if (!caller!.CanTarget(player)) return;
if (!int.TryParse(command.GetArg(2), out var time) && caller != null && caller.IsValid)
{
Silence(caller, player, time, reason, callerName, muteManager, command);
DurationMenu.OpenMenu(caller, $"{_localizer?["sa_silence"] ?? "Silence"}: {player.PlayerName}", player,
ManagePlayersMenu.SilenceMenu);
return;
}

Silence(caller, player, time, reason, callerName, MuteManager, command);
});
}

Expand Down Expand Up @@ -562,7 +564,6 @@ public void OnAddSilenceCommand(CCSPlayerController? caller, CommandInfo command
: (_localizer?["sa_unknown"] ?? "Unknown");

int.TryParse(command.GetArg(2), out var time);
MuteManager muteManager = new(Database);

// Get player and admin info
var adminInfo = caller != null && caller.UserId.HasValue ? PlayersInfo[caller.UserId.Value] : null;
Expand All @@ -577,14 +578,14 @@ public void OnAddSilenceCommand(CCSPlayerController? caller, CommandInfo command
if (!caller.CanTarget(player)) return;

// Perform the silence for an online player
Silence(caller, player, time, reason, callerName, muteManager, silent: true);
Silence(caller, player, time, reason, callerName, MuteManager, silent: true);
}
else
{
// Asynchronous silence operation for offline players
Task.Run(async () =>
{
await muteManager.AddMuteBySteamid(steamid, adminInfo, reason, time, 2);
await MuteManager.AddMuteBySteamid(steamid, adminInfo, reason, time, 2);
});

command.ReplyToCommand($"Player with steamid {steamid} is not online. Silence has been added offline.");
Expand Down
Loading

0 comments on commit 6847da2

Please sign in to comment.