@@ -85,70 +85,121 @@ void RegisterDefaultCommands()
8585 } ) ;
8686 SetCommandDescription ( "kick" , "Kicks a player from the server" ) ;
8787
88- RegisterCommand ( "ban" , ( player , args ) =>
89- {
90- if ( ! isPlayerAdmin ( player . SteamId ) ) return ;
91- // hacky fix,
92- // Extract player name from the command message
93- string playerIdent = string . Join ( " " , args ) ;
94- // try to find a user with the username first
95- var playerToBan = AllPlayers . ToList ( ) . Find ( p => p . Username . Equals ( playerIdent , StringComparison . OrdinalIgnoreCase ) ) ;
96- // if there is no player with the username try to find someone with that fisher ID
97- if ( playerToBan == null )
98- playerToBan = AllPlayers . ToList ( ) . Find ( p => p . FisherID . Equals ( playerIdent , StringComparison . OrdinalIgnoreCase ) ) ;
99-
100- // this could be programmed better, but it works
101- if ( playerToBan == null )
88+ RegisterCommand (
89+ "ban" ,
90+ ( player , args ) =>
10291 {
103- var previousPlayer = PreviousPlayers . ToList ( ) . Find ( p => p . FisherID . Equals ( playerIdent , StringComparison . OrdinalIgnoreCase ) ) ;
104- if ( previousPlayer != null )
105- {
106- messagePlayer ( $ "There is a previous player with that name, if you meant to ban them add a # before the ID: #{ playerIdent } ", player . SteamId ) ;
92+ if ( ! isPlayerAdmin ( player . SteamId ) )
10793 return ;
94+
95+ WFPlayer ? playerToBan = null ;
96+ string playerIdent ;
97+
98+ string rawArgs = string . Join ( " " , args ) ;
99+ string banReason = string . Empty ;
100+
101+ var numQuotesInArgs = rawArgs . Count ( c => c == '"' ) ;
102+ var hasBanReason = numQuotesInArgs >= 2 ;
103+ // While we'd hope admins use delimiters properly, it's actually totally fine for this case if they
104+ // e.g. use quotes inside quotes to quote the target's offending message within the banReason
105+ if ( hasBanReason )
106+ {
107+ var firstQuoteIndex = rawArgs . IndexOf ( '"' ) ;
108+ var lastQuoteIndex = rawArgs . LastIndexOf ( '"' ) ;
109+ banReason = rawArgs
110+ . Substring ( firstQuoteIndex + 1 , lastQuoteIndex - firstQuoteIndex - 1 )
111+ . Trim ( ) ;
112+ rawArgs = rawArgs . Remove (
113+ firstQuoteIndex ,
114+ lastQuoteIndex - firstQuoteIndex + 1
115+ ) ;
108116 }
109-
110- previousPlayer = PreviousPlayers . ToList ( ) . Find ( p => $ "#{ p . FisherID } ". Equals ( playerIdent , StringComparison . OrdinalIgnoreCase ) ) ;
111- if ( previousPlayer != null )
117+ playerIdent = rawArgs . Trim ( ) ;
118+
119+ var targetIsSteamID = System . Text . RegularExpressions . Regex . IsMatch (
120+ playerIdent ,
121+ @"^7656119\d{10}$"
122+ ) ;
123+
124+ // find player by username
125+ var playerMatchingUsername = AllPlayers
126+ . ToList ( )
127+ . Find ( p =>
128+ p . Username . Equals ( playerIdent , StringComparison . OrdinalIgnoreCase )
129+ ) ;
130+ // find player by fisher ID shortcode
131+ var targetIsFID = AllPlayers
132+ . ToList ( )
133+ . Find ( p =>
134+ p . FisherID . Equals ( playerIdent , StringComparison . OrdinalIgnoreCase )
135+ ) ;
136+
137+ if ( targetIsSteamID )
112138 {
113- playerToBan = new WFPlayer ( previousPlayer . SteamId , previousPlayer . Username , new SteamNetworkingIdentity ( ) )
139+ CSteamID steamId = new CSteamID ( Convert . ToUInt64 ( playerIdent ) ) ;
140+ var username = Steamworks . SteamFriends . GetFriendPersonaName ( steamId ) ;
141+ playerToBan = new WFPlayer ( steamId , username , new SteamNetworkingIdentity ( ) )
114142 {
115- FisherID = previousPlayer . FisherID ,
116- Username = previousPlayer . Username ,
143+ Username = username == string . Empty ? playerIdent : username
117144 } ;
118145 }
119- }
120-
121- // use regex to check if its a steam ID
122- if ( playerToBan == null && System . Text . RegularExpressions . Regex . IsMatch ( playerIdent , @"^7656119\d{10}$" ) )
123- {
124- // if it is a steam ID, try to find the player by steam ID
125- CSteamID steamId = new CSteamID ( Convert . ToUInt64 ( playerIdent ) ) ;
126- if ( isPlayerBanned ( steamId ) )
127- banPlayer ( steamId ) ;
146+ else if ( playerMatchingUsername != null )
147+ {
148+ playerToBan = playerMatchingUsername ;
149+ }
150+ else if ( targetIsFID != null )
151+ {
152+ playerToBan = targetIsFID ;
153+ }
128154 else
129- banPlayer ( steamId , true ) ;
130-
131- messagePlayer ( $ "Banned player with Steam ID { playerIdent } ", player . SteamId ) ;
132- return ;
133- }
134-
135- if ( playerToBan == null )
136- {
137- messagePlayer ( "Player not found!" , player . SteamId ) ;
138- }
139- else
140- {
155+ {
156+ // (Defer these searches to last resort, as they could potentially be costlier)
157+ var previousPlayer = PreviousPlayers
158+ . ToList ( )
159+ . Find ( p =>
160+ $ "#{ p . FisherID } ". Equals (
161+ playerIdent ,
162+ StringComparison . OrdinalIgnoreCase
163+ )
164+ ) ;
165+ if ( previousPlayer != null )
166+ {
167+ messagePlayer (
168+ $ "There is a previous player with that FisherID, if you meant to ban them add a # before the ID: #{ playerIdent } ",
169+ player . SteamId
170+ ) ;
171+ return ;
172+ }
173+ previousPlayer = PreviousPlayers
174+ . ToList ( )
175+ . Find ( p =>
176+ p . FisherID . Equals ( playerIdent , StringComparison . OrdinalIgnoreCase )
177+ ) ;
178+ if ( previousPlayer != null )
179+ {
180+ playerToBan = new WFPlayer (
181+ previousPlayer . SteamId ,
182+ previousPlayer . Username ,
183+ new SteamNetworkingIdentity ( )
184+ ) ;
185+ }
186+ }
141187
142- if ( isPlayerBanned ( playerToBan . SteamId ) )
143- banPlayer ( playerToBan . SteamId ) ;
188+ if ( playerToBan == null )
189+ {
190+ messagePlayer ( "Player not found!" , player . SteamId ) ;
191+ }
144192 else
145- banPlayer ( playerToBan . SteamId , true ) ; // save to file if they are not already in there!
146-
147- messagePlayer ( $ "Banned { playerToBan . Username } ", player . SteamId ) ;
148- messageGlobal ( $ "{ playerToBan . Username } has been banned from the server.") ;
193+ {
194+ banPlayer (
195+ playerToBan . SteamId ,
196+ ! isPlayerBanned ( playerToBan . SteamId ) ,
197+ banReason
198+ ) ;
199+ }
149200 }
150- } ) ;
151- SetCommandDescription ( "ban" , "Bans a player from the server " ) ;
201+ ) ;
202+ SetCommandDescription ( "ban" , "Usage: !ban (username|steamID|FisherID) \" Reason for ban \" " ) ;
152203
153204 RegisterCommand ( "prev" , ( player , args ) =>
154205 {
0 commit comments