Skip to content

Commit

Permalink
1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
oqyh authored Dec 27, 2023
1 parent 9391580 commit 26d1844
Showing 1 changed file with 72 additions and 29 deletions.
101 changes: 72 additions & 29 deletions Chat_Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ namespace Chat_Logger;

public class ChatLoggerConfig : BasePluginConfig
{
[JsonPropertyName("CLModeLogs")] public bool CLModeLogs { get; set; } = false;
[JsonPropertyName("LogChatFormat")] public string LogChatFormat { get; set; } = "[{TIME}] {PLAYERNAME} {MESSAGE} (SteamID: {STEAMID})";
[JsonPropertyName("SendLogToText")] public bool SendLogToText { get; set; } = false;
[JsonPropertyName("LogChatFormat")] public string LogChatFormat { get; set; } = "[{TIME}] {TEAM} [{PLAYERNAME}] {MESSAGE} (SteamID: {STEAMID})";
[JsonPropertyName("LogFileFormat")] public string LogFileFormat { get; set; } = ".txt";
[JsonPropertyName("LogFileDateFormat")] public string LogFileDateFormat { get; set; } = "MM-dd-yyyy";
[JsonPropertyName("LogInsideFileTimeFormat")] public string LogInsideFileTimeFormat { get; set; } = "HH:mm:ss";

[JsonPropertyName("SendLogToWebHook")] public bool SendLogToWebHook { get; set; } = false;
[JsonPropertyName("LogDiscordChatFormat")] public string LogDiscordChatFormat { get; set; } = "[{DATE} - {TIME}] {TEAM} {PLAYERNAME} {MESSAGE} (IpAddress: {IP})";
[JsonPropertyName("WebHookURL")] public string WebHookURL { get; set; } = "https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
[JsonPropertyName("LogDiscordChatFormat")] public string LogDiscordChatFormat { get; set; } = "[{DATE} - {TIME}] {TEAM} {MESSAGE} (IpAddress: {IP})";
}

public class ChatLogger : BasePlugin, IPluginConfig<ChatLoggerConfig>
{
public override string ModuleName => "Chat Logger";
public override string ModuleVersion => "1.0.0";
public override string ModuleVersion => "1.0.1";
public override string ModuleAuthor => "Gold KingZ";
public override string ModuleDescription => "Log Any Chat Discord Or Log Text";
public ChatLoggerConfig Config { get; set; } = new ChatLoggerConfig();
private bool? TeamChat = null;
private Dictionary<int, bool> BteamChat = new Dictionary<int, bool>();

public void OnConfigParsed(ChatLoggerConfig config)
{
Expand All @@ -35,15 +35,21 @@ public void OnConfigParsed(ChatLoggerConfig config)
public override void Load(bool hotReload)
{
AddCommandListener("say", OnPlayerSayPublic);
AddCommandListener("say_team", OnPlayerSayTeam);
AddCommandListener("say_team", OnPlayerSayTeam);
}

private HookResult OnPlayerSayPublic(CCSPlayerController? player, CommandInfo info)
{
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || player.AuthorizedSteamID == null|| player.IpAddress == null)return HookResult.Continue;

TeamChat = false;
string chatteam = TeamChat.Value ? "[TEAM]" : "[ALL]";
string? chatteam = null;

if (player.UserId.HasValue)
{
BteamChat[player.UserId.Value] = false;
bool isTeamChat = BteamChat[player.UserId.Value];
chatteam = isTeamChat ? "[TEAM]" : "[ALL]";
}

var message = info.GetArg(1);

Expand All @@ -57,37 +63,41 @@ private HookResult OnPlayerSayPublic(CCSPlayerController? player, CommandInfo in
string Tpath = Path.Combine(ModuleDirectory,"../../plugins/Chat_Logger/logs/") + $"{fileName}";

var vplayername = player.PlayerName;
var vsteamId2 = player.AuthorizedSteamID.SteamId2;
var vsteamId64 = player.AuthorizedSteamID.SteamId64;
var steamId2 = player.AuthorizedSteamID.SteamId2;
var steamId3 = player.AuthorizedSteamID.SteamId3;
var steamId32 = player.AuthorizedSteamID.SteamId32;
var steamId64 = player.AuthorizedSteamID.SteamId64;
var GetIpAddress = player.IpAddress;
var ipAddress = GetIpAddress.Split(':')[0];

if(Config.CLModeLogs && !Directory.Exists(Fpath))
if(Config.SendLogToText && !Directory.Exists(Fpath))
{
Directory.CreateDirectory(Fpath);
}

if(Config.CLModeLogs && !File.Exists(Tpath))
if(Config.SendLogToText && !File.Exists(Tpath))
{
File.Create(Tpath);
}

var replacerlog = ReplaceMessages(Config.LogChatFormat, Time, Date, trimmedMessage, vplayername, vsteamId2, vsteamId64.ToString(), ipAddress, chatteam);
if (Config.CLModeLogs && File.Exists(Tpath))
var replacerlog = ReplaceMessages(Config.LogChatFormat, Time, Date, trimmedMessage, vplayername, steamId2, steamId3, steamId32.ToString(), steamId64.ToString(), ipAddress.ToString(), chatteam ?? "[----]");
if (Config.SendLogToText && File.Exists(Tpath))
{
try
{
File.AppendAllLines(Tpath, new[]{replacerlog});
}catch
{
Console.WriteLine("|||||||||||||||||||||||||||||| E R R O R ||||||||||||||||||||||||||||||");
Console.WriteLine("[Error Cant Write] Please Give Chat_Logger.dll Permissions To Write");
Console.WriteLine("|||||||||||||||||||||||||||||| E R R O R ||||||||||||||||||||||||||||||");
}
}

var replacerlogDiscord = ReplaceMessages(Config.LogDiscordChatFormat, Time, Date, trimmedMessage, vplayername, vsteamId2, vsteamId64.ToString(), ipAddress, chatteam);
var replacerlogDiscord = ReplaceMessages(Config.LogDiscordChatFormat, Time, Date, trimmedMessage, vplayername, steamId2, steamId3, steamId32.ToString(), steamId64.ToString(), ipAddress.ToString(), chatteam ?? "[----]");
if(Config.SendLogToWebHook)
{
Task.Run(() => SendToDiscordWebhook(Config.WebHookURL, replacerlogDiscord));
Task.Run(() => SendToDiscordWebhook(Config.WebHookURL, replacerlog, steamId64.ToString(), vplayername));
}

return HookResult.Continue;
Expand All @@ -97,8 +107,14 @@ private HookResult OnPlayerSayTeam(CCSPlayerController? player, CommandInfo info
{
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || player.AuthorizedSteamID == null|| player.IpAddress == null)return HookResult.Continue;

TeamChat = true;
string chatteam = TeamChat.Value ? "[TEAM]" : "[ALL]";
string? chatteam = null;

if (player.UserId.HasValue)
{
BteamChat[player.UserId.Value] = true;
bool isTeamChat = BteamChat[player.UserId.Value];
chatteam = isTeamChat ? "[TEAM]" : "[ALL]";
}

var message = info.GetArg(1);

Expand All @@ -112,61 +128,83 @@ private HookResult OnPlayerSayTeam(CCSPlayerController? player, CommandInfo info
string Tpath = Path.Combine(ModuleDirectory,"../../plugins/Chat_Logger/logs/") + $"{fileName}";

var vplayername = player.PlayerName;
var vsteamId2 = player.AuthorizedSteamID.SteamId2;
var vsteamId64 = player.AuthorizedSteamID.SteamId64;
var steamId2 = player.AuthorizedSteamID.SteamId2;
var steamId3 = player.AuthorizedSteamID.SteamId3;
var steamId32 = player.AuthorizedSteamID.SteamId32;
var steamId64 = player.AuthorizedSteamID.SteamId64;
var GetIpAddress = player.IpAddress;
var ipAddress = GetIpAddress.Split(':')[0];

if(Config.CLModeLogs && !Directory.Exists(Fpath))
if(Config.SendLogToText && !Directory.Exists(Fpath))
{
Directory.CreateDirectory(Fpath);
}

if(Config.CLModeLogs && !File.Exists(Tpath))
if(Config.SendLogToText && !File.Exists(Tpath))
{
File.Create(Tpath);
}

var replacerlog = ReplaceMessages(Config.LogChatFormat, Time, Date, trimmedMessage, vplayername, vsteamId2, vsteamId64.ToString(), ipAddress, chatteam);
if (Config.CLModeLogs && File.Exists(Tpath))
var replacerlog = ReplaceMessages(Config.LogChatFormat, Time, Date, trimmedMessage, vplayername, steamId2, steamId3, steamId32.ToString(), steamId64.ToString(), ipAddress.ToString(), chatteam ?? "[----]");
if (Config.SendLogToText && File.Exists(Tpath))
{
try
{
File.AppendAllLines(Tpath, new[]{replacerlog});
}catch
{
Console.WriteLine("|||||||||||||||||||||||||||||| E R R O R ||||||||||||||||||||||||||||||");
Console.WriteLine("[Error Cant Write] Please Give Chat_Logger.dll Permissions To Write");
Console.WriteLine("|||||||||||||||||||||||||||||| E R R O R ||||||||||||||||||||||||||||||");
}
}

var replacerlogDiscord = ReplaceMessages(Config.LogDiscordChatFormat, Time, Date, trimmedMessage, vplayername, vsteamId2, vsteamId64.ToString(), ipAddress, chatteam);
var replacerlogDiscord = ReplaceMessages(Config.LogDiscordChatFormat, Time, Date, trimmedMessage, vplayername, steamId2, steamId3, steamId32.ToString(), steamId64.ToString(), ipAddress.ToString(), chatteam ?? "[----]");
if(Config.SendLogToWebHook)
{
Task.Run(() => SendToDiscordWebhook(Config.WebHookURL, replacerlogDiscord));
Task.Run(() => SendToDiscordWebhook(Config.WebHookURL, replacerlog, steamId64.ToString(), vplayername));
}

return HookResult.Continue;
}

private string ReplaceMessages(string Message, string time, string date, string message, string PlayerName, string SteamId, string SteamId64, string IPaddress, string chatteam)
private string ReplaceMessages(string Message, string time, string date, string message, string PlayerName, string SteamId, string SteamId3, string SteamId32, string SteamId64, string IPaddress, string chatteam)
{
var replacedMessage = Message
.Replace("{TIME}", time)
.Replace("{DATE}", date)
.Replace("{MESSAGE}", message)
.Replace("{PLAYERNAME}", PlayerName.ToString())
.Replace("{STEAMID}", SteamId.ToString())
.Replace("{STEAMID3}", SteamId3.ToString())
.Replace("{STEAMID32}", SteamId32.ToString())
.Replace("{STEAMID64}", SteamId64.ToString())
.Replace("{IP}", IPaddress.ToString())
.Replace("{TEAM}", chatteam);
return replacedMessage;
}

static async Task SendToDiscordWebhook(string webhookUrl, string message)
static async Task SendToDiscordWebhook(string webhookUrl, string message, string steamUserId, string STEAMNAME)
{
string profileLink = GetSteamProfileLink(steamUserId);

using (HttpClient client = new HttpClient())
{
var payload = new { content = message };
var embed = new
{
description = message,
author = new
{
name = STEAMNAME,
url = profileLink
}
};

var payload = new
{
embeds = new[] { embed }
};

var jsonPayload = Newtonsoft.Json.JsonConvert.SerializeObject(payload);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");

Expand All @@ -182,5 +220,10 @@ static async Task SendToDiscordWebhook(string webhookUrl, string message)
}
}
}

static string GetSteamProfileLink(string userId)
{
return $"https://steamcommunity.com/profiles/{userId}";
}

}

0 comments on commit 26d1844

Please sign in to comment.