Skip to content

Commit 6226cb8

Browse files
committed
Fix leave join message detection, don't remove team prefixes from them
1 parent 2ea540e commit 6226cb8

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

src/pterodactyl/smp_commands.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,27 @@ async fn handle_log_message(
147147
ptero_server_id: &str,
148148
message: &str,
149149
) -> Result<(), crate::Error> {
150-
if let Some((username, action)) = message.split_once(' ') {
151-
if action == "joined the game" || action == "left the game" {
152-
let sanitized_username = sanitize_username(username);
153-
let message = format!("{} {}", sanitized_username, action);
154-
broadcast_message(
155-
&data.discord_handle,
156-
&data.pterodactyl,
157-
webhook_cache,
158-
ptero_server_id,
159-
Some(&sanitized_username),
160-
true,
161-
&message,
162-
)
163-
.await?;
164-
}
150+
#[allow(clippy::manual_map)]
151+
let leave_join_user_action = if let Some(username) = message.strip_suffix(" joined the game") {
152+
Some((username, "joined the game"))
153+
} else if let Some(username) = message.strip_suffix(" left the game") {
154+
Some((username, "left the game"))
155+
} else {
156+
None
157+
};
158+
if let Some((username, action)) = leave_join_user_action {
159+
let sanitized_username = sanitize_username(username, true);
160+
let message = format!("{} {}", sanitize_username(username, false), action);
161+
broadcast_message(
162+
&data.discord_handle,
163+
&data.pterodactyl,
164+
webhook_cache,
165+
ptero_server_id,
166+
Some(&sanitized_username),
167+
true,
168+
&message,
169+
)
170+
.await?;
165171
}
166172

167173
Ok(())
@@ -200,7 +206,7 @@ async fn handle_server_log<H: PteroWebSocketHandle>(
200206
webhook_cache,
201207
ptero_server_id,
202208
ptero_server,
203-
&sanitize_username(sender),
209+
&sanitize_username(sender, true),
204210
message,
205211
)
206212
.await?;
@@ -332,7 +338,7 @@ fn avatar_url(username: &str) -> String {
332338
format!("https://visage.surgeplay.com/face/256/{username}")
333339
}
334340

335-
fn sanitize_username(username: &str) -> Cow<str> {
341+
fn sanitize_username(username: &str, remove_team_prefix: bool) -> Cow<str> {
336342
if !username.contains('§') && (!username.contains('[') || !username.contains(']')) {
337343
return username.into();
338344
}
@@ -354,17 +360,19 @@ fn sanitize_username(username: &str) -> Cow<str> {
354360
}
355361

356362
// remove team name prefixes
357-
while result.starts_with('[') {
358-
if let Some(close_bracket_index) = result.find(']') {
359-
result.drain(..=close_bracket_index);
360-
} else {
361-
break;
363+
if remove_team_prefix {
364+
while result.starts_with('[') {
365+
if let Some(close_bracket_index) = result.find(']') {
366+
result.drain(..=close_bracket_index);
367+
} else {
368+
break;
369+
}
362370
}
371+
let non_whitespace_index = result
372+
.find(|char: char| !char.is_whitespace())
373+
.unwrap_or(result.len());
374+
result.drain(..non_whitespace_index);
363375
}
364-
let non_whitespace_index = result
365-
.find(|char: char| !char.is_whitespace())
366-
.unwrap_or(result.len());
367-
result.drain(..non_whitespace_index);
368376

369377
if result.is_empty() {
370378
return username.into();

0 commit comments

Comments
 (0)