fCraft.PlayerInfo.Mute C# (CSharp) Method

Mute() public method

Mutes this player (prevents from writing chat messages).
public Mute ( [ player, System.TimeSpan duration, bool announce, bool raiseEvents ) : void
player [ Player who is doing the muting.
duration System.TimeSpan Duration of the mute. If a player is already muted for same or greater length of time, /// PlayerOpException is thrown with NoActionNeeded code. If a player is already muted for a shorter length of time, /// the mute duration is extended.
announce bool Whether to announce muting publicly on the sever.
raiseEvents bool Whether to raise PlayerInfo.MuteChanging and MuteChanged events.
return void
        public void Mute( [NotNull] Player player, TimeSpan duration, bool announce, bool raiseEvents )
        {
            if ( player == null )
                throw new ArgumentNullException( "player" );
            if ( duration <= TimeSpan.Zero ) {
                throw new ArgumentException( "Mute duration may not be zero or negative.", "duration" );
            }

            // Check if player is trying to mute self
            if ( player.Info == this ) {
                PlayerOpException.ThrowCannotTargetSelf( player, this, "mute" );
            }

            lock ( actionLock ) {
                // Check if player can mute in general
                if ( !player.Can( Permission.Mute ) ) {
                    PlayerOpException.ThrowPermissionMissing( player, this, "mute", Permission.Mute );
                }

                // Check if player has sufficient rank permissions
                if ( !player.Can( Permission.Mute, Rank ) ) {
                    PlayerOpException.ThrowPermissionLimit( player, this, "mute", Permission.Mute );
                }

                // Check if target is already muted for longer
                DateTime newMutedUntil = DateTime.UtcNow.Add( duration );
                if ( newMutedUntil > MutedUntil ) {
                    // raise PlayerInfo.MuteChanging event
                    if ( raiseEvents ) {
                        if ( RaiseMuteChangingEvent( this, player, duration, false, announce ) ) {
                            PlayerOpException.ThrowCancelled( player, this );
                        }
                    }

                    // actually mute
                    MutedUntil = newMutedUntil;
                    MutedBy = player.Name;
                    LastModified = DateTime.UtcNow;

                    // raise PlayerInfo.MuteChanged event
                    if ( raiseEvents ) {
                        RaiseMuteChangedEvent( this, player, duration, false, announce );
                    }

                    // log and announce mute publicly
                    Logger.Log( LogType.UserActivity,
                                "Player {0} was muted by {1} for {2}",
                                Name, player.Name, duration );
                    if ( announce ) {
                        Player target = PlayerObject;
                        if ( target != null ) {
                            target.Message( "You were muted by {0}&S for {1}",
                                            player.ClassyName, duration.ToMiniString() );
                        }
                        Server.Message( target,
                                        "&SPlayer {0}&S was muted by {1}&S for {2}",
                                        ClassyName, player.ClassyName, duration.ToMiniString() );
                    }
                } else {
                    // no action needed - already muted for same or longer duration
                    string msg = String.Format( "Player {0} was already muted by {1} ({2} left)",
                                                ClassyName, MutedBy,
                                                TimeMutedLeft.ToMiniString() );
                    string colorMsg = String.Format( "&SPlayer {0}&S was already muted by {1}&S ({2} left)",
                                                     ClassyName, MutedByClassy,
                                                     TimeMutedLeft.ToMiniString() );
                    throw new PlayerOpException( player, this, PlayerOpExceptionCode.NoActionNeeded, msg, colorMsg );
                }
            }
        }

Usage Example

Example #1
0
 bool DetectChatSpam()
 {
     if (this == Console)
     {
         return(false);
     }
     if (spamChatLog.Count >= SpamChatCount)
     {
         DateTime oldestTime = spamChatLog.Dequeue();
         if (DateTime.UtcNow.Subtract(oldestTime).TotalSeconds < SpamChatTimer)
         {
             muteWarnings++;
             if (muteWarnings > ConfigKey.AntispamMaxWarnings.GetInt())
             {
                 Session.KickNow("You were kicked for repeated spamming.", LeaveReason.MessageSpamKick);
                 Server.SendToAll("&W{0} was kicked for repeated spamming.", GetClassyName());
             }
             else
             {
                 Info.Mute("(antispam)", AutoMuteDuration);
                 Message("You have been muted for {0} seconds. Slow down.", AutoMuteDuration.TotalSeconds);
             }
             return(true);
         }
     }
     spamChatLog.Enqueue(DateTime.UtcNow);
     return(false);
 }