From bc286562e1c506245d855348ef2f76bd6521f98d Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 11 Jun 2024 20:11:12 -0400 Subject: [PATCH] Add invite and kick functions --- src/main.rs | 86 ++++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1b1a05e..50ff52f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,56 +53,28 @@ fn main() { for event in events { match event { - Event::Chat(message) => { - if let ChatType::Tell(from, _) = message.chat_type { + Event::Chat(message) => match message.chat_type { + ChatType::Tell(from, _) | ChatType::Group(from, _) => { if let Some(content) = message.content().as_plain() { - println!("{from}: {content}"); + let mut words = content.split_whitespace(); - if content == "inv" { - client.send_invite(from, InviteKind::Group); - } else if content.starts_with("inv") { - let player_names = - content.trim_start_matches("inv").split_whitespace(); + if let Some(command) = words.next() { + match command { + "inv" => invite_players(&mut client, words), + "kick" => { + let sender_info = client.player_list().get(&from).unwrap(); - for name in player_names { - if let Some(player_id) = - client.player_list().iter().find_map(|(id, info)| { - if info.player_alias == name { - Some(id) - } else { - None - } - }) - { - client.send_invite(player_id.clone(), InviteKind::Group); - } - } - } - - if content.starts_with("kick") { - let player_names = - content.trim_start_matches("kick").split_whitespace(); - let sender_info = client.player_list().get(&from).unwrap(); - - if sender_info.uuid.to_string() == CRABO_UUID { - for name in player_names { - if let Some(player_id) = - client.player_list().iter().find_map(|(id, info)| { - if info.player_alias == name { - Some(id) - } else { - None - } - }) - { - client.kick_from_group(player_id.clone()); + if sender_info.uuid.to_string() == CRABO_UUID { + kick_players(&mut client, words) } } + _ => continue, } } } } - } + _ => {} + }, _ => {} } } @@ -134,3 +106,35 @@ fn connect_to_veloren(password: String) -> Client { )) .expect("Failed to create client instance.") } + +fn invite_players<'a, T: Iterator>(client: &mut Client, names: T) { + for name in names { + let find_id = client.player_list().iter().find_map(|(id, info)| { + if info.player_alias == name { + Some(id) + } else { + None + } + }); + + if let Some(player_id) = find_id { + client.send_invite(player_id.clone(), InviteKind::Group); + } + } +} + +fn kick_players<'a, T: Iterator>(client: &mut Client, names: T) { + for name in names { + let find_id = client.player_list().iter().find_map(|(id, info)| { + if info.player_alias == name { + Some(id) + } else { + None + } + }); + + if let Some(player_id) = find_id { + client.kick_from_group(player_id.clone()); + } + } +}