From 4103a82fb2258f292e326bd531256e29f877316c Mon Sep 17 00:00:00 2001 From: Resxt <55228336+Resxt@users.noreply.github.com> Date: Tue, 21 Mar 2023 02:50:45 +0100 Subject: [PATCH] chat_commands 1.0.0 This is a port of my MW3 chat_commands script (version 1.3.2) https://github.com/Resxt/Plutonium-IW5-Scripts/commit/ba1322b933e5afcbf9b7dd1348d26485ceae3afa The array declarations such as array = ["value"] were changed to use the array function The tell function (player chat) was changed IPrintLnBold. More explanation in the README Support for clantags was added. Clantags were changing the player's name which would then make the script fail to find the player when typing his name. This has been tested and fixed. More explanation in the README Errors and util functions related to camo were removed. The way camo works seems to be really different than MW3 so work is required to implement these again --- chat_commands/README.md | 70 +++++ chat_commands/chat_commands.gsc | 538 ++++++++++++++++++++++++++++++++ 2 files changed, 608 insertions(+) create mode 100644 chat_commands/README.md create mode 100644 chat_commands/chat_commands.gsc diff --git a/chat_commands/README.md b/chat_commands/README.md new file mode 100644 index 0000000..c39c068 --- /dev/null +++ b/chat_commands/README.md @@ -0,0 +1,70 @@ +# Chat commands + +Let players execute commands by typing in the chat. +This can be used to display text to the player, for example the server rules or execute GSC code, just like console commands. +This works in private games, on dedicated servers that use [IW4MAdmin](https://github.com/RaidMax/IW4M-Admin) and those that don't. +If you do monitor your server with [IW4MAdmin](https://github.com/RaidMax/IW4M-Admin) then make sure to read the [notes section](#notes). + +The `chat_command` scripts you find here work for both MP and ZM and can be placed in the `scripts` folder. +MP only scripts are in the [mp](mp) folder and ZM only scripts are in the [zm](zm) folder. + +## chat_commands.gsc + +The core script that holds the configuration, runs all the chat logic and holds utils function that are shared between all the `chat_command` scripts. +**[IMPORTANT]** Installing it is **mandatory** to make the commands work as this is the core of this whole system and all the command scripts depend on it. +Also note that this script doesn't provide any command on its own. You must install at least one command script to be able to use commands. Otherwise it will always say that you don't have any command. + +### Main features + +- Easy per server (port) commands configuration. You can either pass an array of one server port, or multiple, or the `level.chat_commands["ports"]` array to easily add a command to one/multiple/all servers +- Chat text print and functions support +- Optional permissions level system to restrict commands to players with a certain permission level (disabled by default) +- All exceptions are handled with error messages (no commands on the server, not enough arguments, command doesn't exist, command doesn't have any help message, player doesn't exist etc.) +- A `commands` command that lists all available commands on the server you're on dynamically (only lists commands you have access to if the permission system is enabled) +- A `help` command that explains how to use a given command. For example `help map` (only works on commands you have access to if the permission system is enabled) +- All commands that require a target work with `me`. Also it doesn't matter how you type the player's name as long as you type the full name. +- Configurable command prefix. Set to `!` by default +- A plugin system to easily allow adding/removing commands. Each command has its own GSC file to easily add/remove/review/configure your commands. This also makes contributing by creating a PR to add a command a lot easier + +### Dvars + +Here are the dvars you can configure: + +| Name | Description | Default value | Accepted values | +|---|---|---|---| +| cc_debug | Toggle whether the script is in debug mode or not. This is used to print players GUID in the console when they connect | 0 | 0 or 1 | +| cc_prefix | The symbol to type before the command name in the chat. Only one character is supported. The `/` symbol won't work normally as it's reserved by the game. If you use the `/` symbol as prefix you will need to type double slash in the game | ! | Any working symbol | +| cc_permission_enabled | Toggle whether the permission system is enabled or not. If it's disabled any player can run any available | 0 | 0 or 1 | +| cc_permission_mode | Changes whether the permission dvars values are names or guids | name | name or guid | +| cc_permission_default | The default permission level players who aren't found in the permission dvars will be granted | 1 | Any plain number from 0 to `cc_permission_max` | +| cc_permission_max | The maximum/most elevated permission level | 4 | Any plain number above 0 | +| cc_permission_0 | A list of names or guids of players who will be granted the permission level 0 when connecting (no access to any command) | "" | Names or guids (depending on `cc_permission_mode`). Each value is separated with a colon (:) | +| cc_permission_1 | A list of names or guids of players who will be granted the permission level 1 when connecting | "" | Names or guids (depending on `cc_permission_mode`). Each value is separated with a colon (:) | +| cc_permission_2 | A list of names or guids of players who will be granted the permission level 2 when connecting | "" | Names or guids (depending on `cc_permission_mode`). Each value is separated with a colon (:) | +| cc_permission_3 | A list of names or guids of players who will be granted the permission level 3 when connecting | "" | Names or guids (depending on `cc_permission_mode`). Each value is separated with a colon (:) | +| cc_permission_4 | A list of names or guids of players who will be granted the permission level 4 when connecting | "" | Names or guids (depending on `cc_permission_mode`). Each value is separated with a colon (:) | + +### Configuration + +Below is an example CFG showing how each dvars can be configured. +The values you see are the default values that will be used if you don't set a dvar. + +```c +set cc_debug 0 +set cc_permission_enabled 0 +set cc_permission_mode "name" +set cc_permission_default 1 +set cc_permission_max 4 +set cc_permission_0 "" +set cc_permission_1 "" +set cc_permission_2 "" +set cc_permission_3 "" +set cc_permission_4 "" +``` + +### Notes + +- To pass an argument with a space you need to put `'` around it. For example if a player name is `The Moonlight` then you would write `!teleport 'The Moonlight' Resxt` +- If you use [IW4MAdmin](https://github.com/RaidMax/IW4M-Admin) make sure you have a different commands prefix to avoid conflicts. For example `!` for IW4MAdmin commands and `.` for this script. The commands prefix can be modified by changing the value of the `cc_prefix` dvar. As for [IW4MAdmin](https://github.com/RaidMax/IW4M-Admin), at the time of writing, if you want to change it you'll need to change the value of [CommandPrefix](https://github.com/RaidMax/IW4M-Admin/wiki/Configuration#advanced-configuration) +- If you prefer to display information (error messages, status change etc.) in the player's chat rather than on his screen you can do that on a dedicated server. For this you need to install [t6-gsc-utils.dll](https://github.com/fedddddd/t6-gsc-utils#installation) (dedicated server only) and change `self IPrintLnBold(message);` to `self tell(message);` in the `TellPlayer` function +- Support for clantags was added. You can use the player names in-game or in the dvars without having to care about their clantag. The [setClantag function](https://github.com/fedddddd/t6-gsc-utils#chat) replaces the player name so additional work was required to make the script ignore the clantag diff --git a/chat_commands/chat_commands.gsc b/chat_commands/chat_commands.gsc new file mode 100644 index 0000000..f094bee --- /dev/null +++ b/chat_commands/chat_commands.gsc @@ -0,0 +1,538 @@ +/* +========================================================================== +| Game: Plutonium T6 | +| Description : Display text and run GSC code | +| by typing commands in the chat | +| Author: Resxt | +========================================================================== +| https://github.com/Resxt/Plutonium-T6-Scripts/tree/main/chat_commands | +========================================================================== +*/ + + + +/* Init section */ + +Main() +{ + InitChatCommands(); +} + +InitChatCommands() +{ + InitChatCommandsDvars(); + + level.chat_commands = []; // don't touch + level.chat_commands["ports"] = array("4976", "4977"); // an array of the ports of all your servers you want to have the script running on. This is useful to easily pass this array as first arg of CreateCommand to have the command on all your servers + level.chat_commands["no_commands_message"] = array("^1No commands found", "You either ^1didn't add any chat_command file ^7to add a new command ^1or ^7there are ^1no command configured on this port", "chat_commands.gsc is ^1just the base system. ^7It doesn't provide any command on its own", "Also ^1make sure the ports are configured properly ^7in the CreateCommand function of your command file(s)"); // the lines to print in the chat when the server doesn't have any command added + level.chat_commands["no_commands_wait"] = 6; // time to wait between each line in when printing that specific message in the chat + + level thread OnPlayerConnect(); + level thread ChatListener(); +} + +InitChatCommandsDvars() +{ + SetDvarIfNotInitialized("cc_debug", 0); + SetDvarIfNotInitialized("cc_prefix", "!"); + + SetDvarIfNotInitialized("cc_permission_enabled", 0); + SetDvarIfNotInitialized("cc_permission_mode", "name"); + SetDvarIfNotInitialized("cc_permission_default", 1); + SetDvarIfNotInitialized("cc_permission_max", 4); + + for (i = 0; i <= GetDvarInt("cc_permission_max"); i++) + { + SetDvarIfNotInitialized("cc_permission_" + i, ""); + } +} + + + +/* Commands section */ + +/* + the ports of the servers this command will be created for + the name of the command, this is what players will type in the chat + the type of the command: is for arrays of text to display text in the player's chat and is to execute a function + when is "text" this is an array of lines to print in the chat. When is "function" this is a function pointer (a reference to a function) + (optional, if no value is provided then anyone who's permission level is default or above can run the command) the minimum permission level required to run this command. For example if this is set to 3 then any user with permission level 3 or 4 will be able to run this command + (optional) an array of the lines to print when typing the help command in the chat followed by a command name. You can also pass an array of one preset string to have it auto generated, for example: ["default_help_one_player"] +*/ +CreateCommand(serverPorts, commandName, commandType, commandValue, commandMinimumPermission, commandHelp) +{ + foreach (serverPort in serverPorts) + { + level.commands[serverPort][commandName]["type"] = commandType; + + if (IsDefined(commandHelp)) + { + commandHelpMessage = commandHelp; + commandHelpString = commandHelp[0]; + + if (commandHelpString == "default_help_one_player") + { + commandHelpMessage = array("Example: " + GetDvar("cc_prefix") + commandName + " me", "Example: " + GetDvar("cc_prefix") + commandName + " Resxt"); + } + else if (commandHelpString == "default_help_two_players") + { + commandHelpMessage = array("Example: " + GetDvar("cc_prefix") + commandName + " me Resxt", "Example: " + GetDvar("cc_prefix") + commandName + " Resxt me", "Example: " + GetDvar("cc_prefix") + commandName + " Resxt Eldor"); + } + + level.commands[serverPort][commandName]["help"] = commandHelpMessage; + } + + if (commandType == "text") + { + level.commands[serverPort][commandName]["text"] = commandValue; + } + else if (commandType == "function") + { + level.commands[serverPort][commandName]["function"] = commandValue; + } + + if (IsDefined(commandMinimumPermission)) + { + level.commands[serverPort][commandName]["permission"] = commandMinimumPermission; + } + else + { + level.commands[serverPort][commandName]["permission"] = GetDvarInt("cc_permission_default"); + } + } +} + + + +/* Chat section */ + +ChatListener() +{ + while (true) + { + level waittill("say", message, player); + + if (message[0] != GetDvar("cc_prefix")) // For some reason checking for the buggy character doesn't work so we start at the second character if the first isn't the command prefix + { + message = GetSubStr(message, 1); // Remove the random/buggy character at index 0, get the real message + } + + if (message[0] != GetDvar("cc_prefix")) // If the message doesn't start with the command prefix + { + continue; // stop + } + + if (PermissionIsEnabled() && player GetPlayerPermissionLevel() == 0) + { + player thread TellPlayer(InsufficientPermissionError(0), 1); + continue; // stop + } + + commandArray = StrTok(message, " "); // Separate the command by space character. Example: ["!map", "mp_dome"] + command = commandArray[0]; // The command as text. Example: !map + args = []; // The arguments passed to the command. Example: ["mp_dome"] + arg = ""; + + for (i = 1; i < commandArray.size; i++) + { + checkedArg = commandArray[i]; + + if (checkedArg[0] != "'" && arg == "") + { + args = AddElementToArray(args, checkedArg); + } + else if (checkedArg[0] == "'") + { + arg = StrTok(checkedArg, "'")[0] + " "; + } + else if (checkedArg[checkedArg.size - 1] == "'") + { + args = AddElementToArray(args, (arg + StrTok(checkedArg, "'")[0])); + arg = ""; + } + else + { + arg += (checkedArg + " "); + } + } + + if (IsDefined(level.commands[GetDvar("net_port")])) + { + if (command == GetDvar("cc_prefix") + "commands") // commands command + { + if (GetDvarInt("cc_permission_enabled")) + { + playerCommands = []; + + foreach (commandName in GetArrayKeys(level.commands[GetDvar("net_port")])) + { + if (PlayerHasSufficientPermissions(player, level.commands[GetDvar("net_port")][commandName]["permission"])) + { + playerCommands = AddElementToArray(playerCommands, commandName); + } + } + + player thread TellPlayer(playerCommands, 2, true); + } + else + { + player thread TellPlayer(GetArrayKeys(level.commands[GetDvar("net_port")]), 2, true); + } + } + else + { + // help command + if (command == GetDvar("cc_prefix") + "help" && !IsDefined(level.commands[GetDvar("net_port")]["help"]) || command == GetDvar("cc_prefix") + "help" && IsDefined(level.commands[GetDvar("net_port")]["help"]) && args.size >= 1) + { + if (args.size < 1) + { + player thread TellPlayer(NotEnoughArgsError(1), 1.5); + } + else + { + commandValue = level.commands[GetDvar("net_port")][args[0]]; + + if (IsDefined(commandValue)) + { + if (!PermissionIsEnabled() || PlayerHasSufficientPermissions(player, commandValue["permission"])) + { + commandHelp = commandValue["help"]; + + if (IsDefined(commandHelp)) + { + player thread TellPlayer(commandHelp, 1.5); + } + else + { + player thread TellPlayer(CommandHelpDoesNotExistError(args[0]), 1); + } + } + else + { + player thread TellPlayer(InsufficientPermissionError(player GetPlayerPermissionLevel(), args[0], commandValue["permission"]), 1.5); + } + } + else + { + if (args[0] == "commands") + { + player thread TellPlayer(CommandHelpDoesNotExistError(args[0]), 1); + } + else + { + player thread TellPlayer(CommandDoesNotExistError(args[0]), 1); + } + } + } + } + // any other command + else + { + commandName = GetSubStr(command, 1); + commandValue = level.commands[GetDvar("net_port")][commandName]; + + if (IsDefined(commandValue)) + { + if (!PermissionIsEnabled() || PlayerHasSufficientPermissions(player, commandValue["permission"])) + { + if (commandValue["type"] == "text") + { + player thread TellPlayer(commandValue["text"], 2); + } + else if (commandValue["type"] == "function") + { + error = player [[commandValue["function"]]](args); + + if (IsDefined(error)) + { + player thread TellPlayer(error, 1.5); + } + } + } + else + { + player thread TellPlayer(InsufficientPermissionError(player GetPlayerPermissionLevel(), commandName, commandValue["permission"]), 1.5); + } + } + else + { + player thread TellPlayer(CommandDoesNotExistError(commandName), 1); + } + } + } + } + else + { + player thread TellPlayer(level.chat_commands["no_commands_message"], level.chat_commands["no_commands_wait"], false); + } + } +} + +TellPlayer(messages, waitTime, isCommand) +{ + for (i = 0; i < messages.size; i++) + { + message = messages[i]; + + if (IsDefined(isCommand) && isCommand) + { + message = GetDvar("cc_prefix") + message; + } + + self IPrintLnBold(message); + + if (i < (messages.size - 1)) // Don't unnecessarily wait after the last message has been displayed + { + wait waitTime; + } + } +} + + + +/* Player section */ + +OnPlayerConnect() +{ + for(;;) + { + level waittill("connected", player); + + if (player IsBot()) + { + continue; // stop + } + + if (!IsDefined(player.pers["chat_commands"])) + { + player.pers["chat_commands"] = []; + + if (DebugIsOn()) + { + Print("GUID of " + player.name + ": " + player.guid); + } + } + + player SetPlayerPermissionLevel(player GetPlayerPermissionLevelFromDvar()); + } +} + + + +/* Error functions section */ + +CommandDoesNotExistError(commandName) +{ + return array("The command " + commandName + " doesn't exist", "Type " + GetDvar("cc_prefix") + "commands to get a list of commands"); +} + +CommandHelpDoesNotExistError(commandName) +{ + return array("The command " + commandName + " doesn't have any help message"); +} + +InsufficientPermissionError(playerPermissionLevel, commandName, requiredPermissionLevel) +{ + if (playerPermissionLevel == 0) + { + return array("You don't have the permissions to run any command"); + } + + return array("Access to the ^5" + commandName + " ^7command refused", "Your permission level is ^5" + playerPermissionLevel + " ^7and the minimum permission level for this command is ^5" + requiredPermissionLevel); +} + +InvalidPermissionLevelError(requestedPermissionLevel) +{ + return array("^5" + requestedPermissionLevel + " ^7is not a valid permission level", "Permission levels range from ^50 ^7to ^5" + GetDvarInt("cc_permission_max")); +} + +NotEnoughArgsError(minimumArgs) +{ + return array("Not enough arguments supplied", "At least " + minimumArgs + " argument expected"); +} + +PlayerDoesNotExistError(playerName) +{ + return array("Player " + playerName + " was not found"); +} + +DvarDoesNotExistError(dvarName) +{ + return array("The dvar " + dvarName + " doesn't exist"); +} + + + +/* Utils section */ + +FindPlayerByName(name) +{ + if (name == "me") + { + return self; + } + + foreach (player in level.players) + { + playerName = player.name; + + if (IsSubStr(playerName, "]")) // support for clantags + { + playerName = StrTok(playerName, "]")[1]; // ignore the clantag + } + + if (ToLower(playerName) == ToLower(name)) + { + return player; + } + } +} + +ToggleStatus(commandName, commandDisplayName, player) +{ + SetStatus(commandName, player, !GetStatus(commandName, player)); + + statusMessage = "^2ON"; + + if (!GetStatus(commandName, player)) + { + statusMessage = "^1OFF"; + } + + if (self.name == player.name) + { + self TellPlayer(array("You changed your " + commandDisplayName + " status to " + statusMessage), 1); + } + else + { + self TellPlayer(array(player.name + " " + commandDisplayName + " status changed to " + statusMessage), 1); + player TellPlayer(array(self.name + " changed your " + commandDisplayName + " status to " + statusMessage), 1); + } +} + +GetStatus(commandName, player) +{ + if (!IsDefined(player.pers["chat_commands"])) // avoid undefined errors in the console + { + player.pers["chat_commands"] = []; + } + + if (!IsDefined(player.pers["chat_commands"]["status"])) // avoid undefined errors in the console + { + player.pers["chat_commands"]["status"] = []; + } + + if (!IsDefined(player.pers["chat_commands"]["status"][commandName])) // status is set to OFF/false by default + { + SetStatus(commandName, player, false); + } + + return player.pers["chat_commands"]["status"][commandName]; +} + +SetStatus(commandName, player, status) +{ + player.pers["chat_commands"]["status"][commandName] = status; +} + +GetPlayerPermissionLevelFromDvar() +{ + for (dvarIndex = GetDvarInt("cc_permission_max"); dvarIndex > 0; dvarIndex--) + { + dvarName = "cc_permission_" + dvarIndex; + + foreach (value in StrTok(GetDvar(dvarName), ":")) + { + if (GetDvar("cc_permission_mode") == "name") + { + playerName = self.name; + + if (IsSubStr(playerName, "]")) // support for clantags + { + playerName = StrTok(playerName, "]")[1]; // ignore the clantag + } + + if (ToLower(value) == ToLower(playerName)) + { + return dvarIndex; + } + } + else + { + if (value == self.guid) + { + return dvarIndex; + } + } + } + } + + return GetDvarInt("cc_permission_default"); +} + +GetPlayerPermissionLevel() +{ + return self.pers["chat_commands"]["permission_level"]; +} + +SetPlayerPermissionLevel(newPermissionLevel) +{ + self.pers["chat_commands"]["permission_level"] = newPermissionLevel; +} + +PlayerHasSufficientPermissions(player, targetedPermissionLevel) +{ + playerPermissionLevel = player GetPlayerPermissionLevel(); + + if (playerPermissionLevel == 0) + { + return false; + } + + if (!IsDefined(targetedPermissionLevel)) + { + return true; + } + + return playerPermissionLevel >= targetedPermissionLevel; +} + +DvarIsInitialized(dvarName) +{ + result = GetDvar(dvarName); + return result != ""; +} + +SetDvarIfNotInitialized(dvarName, dvarValue) +{ + if (!DvarIsInitialized(dvarName)) + { + SetDvar(dvarName, dvarValue); + } +} + +IsBot() +{ + return IsDefined(self.pers["isBot"]) && self.pers["isBot"]; +} + +TargetIsMyself(targetName) +{ + return targetName == "me" || ToLower(targetName) == ToLower(self.name); +} + +AddElementToArray(array, element) +{ + array[array.size] = element; + return array; +} + +DebugIsOn() +{ + return GetDvarInt("cc_debug"); +} + +PermissionIsEnabled() +{ + return GetDvarInt("cc_permission_enabled"); +} \ No newline at end of file