C#:
/***********************************************************************************************************************/
/*** DO NOT edit this file! Edit the files under `oxide/config` and/or `oxide/lang`, created once plugin has loaded. ***/
/***********************************************************************************************************************/
/*** Developed and maintained by netch.dev ***/
/*** Copyright, All rights reserved. ***/
/*** Permission is granted to use this on your server only. You do not have permission to re-sell or steal code. ***/
/*** Website: https://www.modpulse.com ***/
/*** Support Discord: https://discord.com/invite/yz5D3PBXa8 ***/
/***********************************************************************************************************************/
using Newtonsoft.Json;
using Oxide.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Oxide.Plugins {
[Info("CommandLogger", "Netch", "1.0.0")]
[Description("Logs commands so you can find out what your players are trying to use ")]
public class CommandLogger : RustPlugin {
#region Vars
private const string ADMIN_PERM = "commandlogger.admin"; // Users with this permission can use the console commands
#endregion
#region Hooks
private void Init() {
if (!permission.PermissionExists(ADMIN_PERM)) permission.RegisterPermission(ADMIN_PERM, this);
data = Interface.Oxide.DataFileSystem.ReadObject<Data>(Name);
}
private void OnNewSave(string filename) {
if (config.WipeDataFile) {
data.CommandsUsed.Clear();
SaveData();
}
}
private void OnServerSave() => SaveData();
private void Unload() => SaveData();
private object OnPlayerCommand(BasePlayer player, string command, string[] args) {
if (config.CommandsToIgnore.Contains(command)) return null;
command = command.ToLower();
if (config.LogAllCommandsToConsole) {
Puts($"{player.displayName} ({player.UserIDString}) ran command: {command} {string.Join(" ", args)}");
}
if (config.LogAllCommandsToData) {
if (data.CommandsUsed.ContainsKey(command)) {
data.CommandsUsed[command]++;
} else {
data.CommandsUsed.Add(command, 1);
}
}
return null;
}
#endregion
#region Commands
[ConsoleCommand("cl")]
private void CommandLoggerWipeConsoleCmd(ConsoleSystem.Arg args) {
BasePlayer player = args.Player();
if (player != null && !permission.UserHasPermission(player.UserIDString, ADMIN_PERM)) {
ConsolePrint(player, "Perm");
return;
}
string action = args.GetString(0, "help");
switch (action.ToLower()) {
case "wipe":
data.CommandsUsed.Clear();
SaveData();
ConsolePrint(player, "Wiped");
break;
case "check":
case "list":
if (data.CommandsUsed.Count == 0) {
ConsolePrint(player, "None");
return;
}
ConsolePrint(player, "Commands");
if (player != null) {
foreach (KeyValuePair<string, int> item in data.CommandsUsed.OrderByDescending(x => x.Value)) {
player.ConsoleMessage(Lang("Cmd", player.UserIDString, item.Key, item.Value));
};
} else {
foreach (KeyValuePair<string, int> item in data.CommandsUsed.OrderByDescending(x => x.Value)) {
Puts(Lang("Cmd", null, item.Key, item.Value));
};
}
ConsolePrint(player, "==================");
break;
case "ignore":
string commandToIgnore = args.GetString(1, "");
if (string.IsNullOrEmpty(commandToIgnore)) {
ConsolePrint(player, "CommandUsage");
return;
}
commandToIgnore = commandToIgnore.Replace("/", "");
if (config.CommandsToIgnore.Contains(commandToIgnore)) {
ConsolePrint(player, "AlreadyIgnored");
return;
}
data.CommandsUsed.Remove(commandToIgnore);
config.CommandsToIgnore.Add(commandToIgnore);
SaveConfig();
ConsolePrint(player, "Ignored");
break;
default:
ConsolePrint(player, "CommandUsage");
break;
}
}
#endregion
#region Config
static Configuration config;
public class Configuration {
[JsonProperty("Log all player chat commands to the console (true/false)")] public bool LogAllCommandsToConsole;
[JsonProperty("Log all player chat commands to oxide/data/CommandLogger.json (true/false)")] public bool LogAllCommandsToData;
[JsonProperty("Wipe the data file on a new map (true/false)")] public bool WipeDataFile;
[JsonProperty("Chat commands to ignore")] public List<string> CommandsToIgnore;
public static Configuration DefaultConfig() {
return new Configuration {
LogAllCommandsToConsole = false,
LogAllCommandsToData = true,
WipeDataFile = false,
CommandsToIgnore = new List<string> { "help", "info", "commands" },
};
}
}
protected override void LoadConfig() {
base.LoadConfig();
try {
config = Config.ReadObject<Configuration>();
if (config == null)
LoadDefaultConfig();
SaveConfig();
} catch (Exception e) {
Debug.LogException(e);
PrintWarning("Creating new config file.");
LoadDefaultConfig();
}
}
protected override void LoadDefaultConfig() => config = Configuration.DefaultConfig();
protected override void SaveConfig() => Config.WriteObject(config);
#endregion
#region Data
Data data;
class Data {
public readonly Dictionary<string, int> CommandsUsed = new Dictionary<string, int>(); // Command, Amount of times the command was used
}
private void SaveData() => Interface.Oxide.DataFileSystem.WriteObject(Name, data);
#endregion
#region Localization
private void ConsolePrint(BasePlayer player, string langID) {
if (player != null) {
player.ConsoleMessage(Lang(langID, player.UserIDString));
} else {
Puts(Lang(langID));
}
}
private string Lang(string key, string playerID = null, params object[] args) {
return string.Format(lang.GetMessage(key, this, playerID), args);
}
protected override void LoadDefaultMessages() {
lang.RegisterMessages(new Dictionary<string, string> {
["P"] = "<color=#FFD700>[Command Logger]</color>",
["Perm"] = "You don't have permission to use this command.",
["Cmd"] = "/{0} used x{1} times.",
["Wiped"] = "Wiped the command logger data file.",
["None"] = "No commands have been saved.",
["Commands"] = "Commands Used:",
["CommandUsage"] = "Command Usage:\ncl wipe - Wipes all saved command data.\ncl list - Lists all stored commands.\ncl ignore <command> - Ignore the command from being tracked.",
["Ignored"] = "Added that command to the config ignore list.",
}, this);
}
#endregion
}
}