// Chat: a tag before the name of admins and VIPs in the public chat, a word filter and a mute, after Legacy's EasyChat // and BlockedSystem. A player gets the tag of the first group of the list they are in (admins: the "admin" tag). // Admins: /mute [minutes] (60 by default), /unmute . A muted player's lines do not go out, they are // told for how long. // Install: copy this file into the server's data folder, plugins/Chat.cs. Settings: plugins/config/Chat.json (Tags: // group, text, colour, icon and its tooltip, in order: Icon is a Material icon name, "star", or an https image from Imgur, Discord or // GitHub; admins without one get the game's shield. BannedWords: replaced by stars, whatever their case). // Script engine rules: test objects with != null, no static fields that change, callbacks with one parameter at most. using RustLegacy; public class ChatTag { public string Group { get; set; } = ""; public string Text { get; set; } = ""; public string Color { get; set; } = "#ffffff"; public string Icon { get; set; } = ""; /// The icon's tooltip; empty: the text. public string Tooltip { get; set; } = ""; } public class ChatConfig { public List Tags { get; set; } = new List { new ChatTag { Group = "admin", Text = "[Admin]", Color = "#ff5a3c" }, new ChatTag { Group = "vip", Text = "[VIP]", Color = "#ffd200", Icon = "star" }, }; public List BannedWords { get; set; } = new List(); } public class ChatData { /// SteamID → end of the mute, in unix seconds. public Dictionary Muted { get; set; } = new Dictionary(); } public class Chat : RustPlugin { ChatConfig _config; ChatData _data; public override void OnLoaded() { _config = LoadConfig(); _data = LoadData(); AddCommand( "mute", OnMute, "Mute a player in the public chat", true, " [minutes]" ); AddCommand( "unmute", OnUnmute, "Let a muted player talk again", true, "" ); foreach ( var p in Players ) Tag( p ); Every( 30f, () => { foreach ( var p in Players ) Tag( p ); } ); // a player put in a group gets their tag } public override void OnPlayerConnected( RustPlayer player ) => Tag( player ); long Now() => DateTime.UtcNow.Ticks / 10_000_000; void Tag( RustPlayer p ) { foreach ( var tag in _config.Tags ) { if ( (tag.Group == "admin" && IsAdmin( p )) || InGroup( p.SteamId, tag.Group ) ) { SetChatTag( p, tag.Text, tag.Color, tag.Icon, tag.Tooltip ); return; } } SetChatTag( p, "" ); } public override string OnPlayerChat( RustPlayer player, string message ) { long until = 0; if ( _data.Muted.TryGetValue( player.SteamId, out until ) && until > Now() ) { Reply( player, $"You are muted for {(until - Now() + 59) / 60} more min." ); return ""; } return Filter( message ); } string Filter( string message ) { foreach ( var word in _config.BannedWords ) { if ( string.IsNullOrWhiteSpace( word ) ) continue; var at = message.IndexOf( word, StringComparison.OrdinalIgnoreCase ); while ( at >= 0 ) { message = message.Substring( 0, at ) + new string( '*', word.Length ) + message.Substring( at + word.Length ); at = message.IndexOf( word, at + word.Length, StringComparison.OrdinalIgnoreCase ); } } return message; } void OnMute( ChatCommand cmd ) { var target = cmd.Args.Length > 0 ? FindPlayer( cmd.Args[0] ) : null; if ( target == null ) { Reply( cmd.Player, "Usage: /mute [minutes]" ); return; } int minutes = 60; if ( cmd.Args.Length > 1 ) int.TryParse( cmd.Args[1], out minutes ); _data.Muted[target.SteamId] = Now() + Math.Max( 1, minutes ) * 60L; SaveData( _data ); Reply( cmd.Player, $"{target.DisplayName} is muted for {Math.Max( 1, minutes )} min." ); Reply( target, $"An admin muted you for {Math.Max( 1, minutes )} min." ); } void OnUnmute( ChatCommand cmd ) { var target = cmd.Args.Length > 0 ? FindPlayer( cmd.Args[0] ) : null; if ( target == null || !_data.Muted.Remove( target.SteamId ) ) { Reply( cmd.Player, "Usage: /unmute (someone muted)" ); return; } SaveData( _data ); Reply( cmd.Player, $"{target.DisplayName} can talk again." ); Reply( target, "You can talk again." ); } }