From 021f8154c8b6767223a9203a8b97de3468dae605 Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 13 Jun 2024 20:19:49 -0400 Subject: [PATCH] Fix ID bug --- Cargo.lock | 1 + Cargo.toml | 1 + src/bot.rs | 62 +++++++++++++++++++++++++++-------------------------- src/main.rs | 5 +++-- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 90a7b4f..4e1bc82 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -732,6 +732,7 @@ dependencies = [ "toml", "veloren-client", "veloren-common", + "veloren-common-net", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4eee39f..0210899 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" tokio = "1.38.0" veloren-common = { git = "https://gitlab.com/veloren/veloren", branch = "master", features = ["no-assets"] } veloren-client = { git = "https://gitlab.com/veloren/veloren", branch = "master" } +veloren-common-net = { git = "https://gitlab.com/veloren/veloren", branch = "master" } toml = "0.8.14" serde = { version = "1.0.203", features = ["derive"] } diff --git a/src/bot.rs b/src/bot.rs index 2540fd5..5c143f6 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -6,24 +6,26 @@ use veloren_common::{ clock::Clock, comp::{invite::InviteKind, ChatType, ControllerInputs}, uid::Uid, + uuid::Uuid, ViewDistances, }; +use veloren_common_net::msg::PlayerInfo; use crate::Config; pub struct Bot { client: Client, clock: Clock, - admin_list: Vec, - ban_list: Vec, + admin_list: Vec, + ban_list: Vec, } impl Bot { pub fn new( username: &str, password: &str, - admin_list: Vec, - ban_list: Vec, + admin_list: Vec, + ban_list: Vec, ) -> Result { let client = connect_to_veloren(username, password)?; let clock = Clock::new(Duration::from_secs_f64(0.1)); @@ -87,17 +89,11 @@ impl Bot { if let Event::Chat(message) = event { match message.chat_type { ChatType::Tell(sender, _) | ChatType::Group(sender, _) => { - let sender_uuid = self - .client - .player_list() - .get(&sender) - .unwrap() - .uuid - .to_string(); + let sender_info = self.client.player_list().get(&sender).unwrap().clone(); self.handle_message( message.into_content().as_plain().unwrap_or(""), - &sender_uuid, + &sender_info, )?; } _ => {} @@ -107,38 +103,42 @@ impl Bot { Ok(()) } - fn handle_message(&mut self, content: &str, sender: &String) -> Result<(), String> { - let mut names = content.split_whitespace(); + fn handle_message(&mut self, content: &str, sender: &PlayerInfo) -> Result<(), String> { + let mut words = content.split_whitespace(); - if let Some(command) = names.next() { + if let Some(command) = words.next() { match command { "admin" => { - if self.admin_list.contains(sender) || self.admin_list.is_empty() { - self.adminify_players(names)?; + if self.admin_list.contains(&sender.uuid) || self.admin_list.is_empty() { + self.adminify_players(words)?; } } "ban" => { - if self.admin_list.contains(sender) { - self.kick_players(names.clone()); - self.ban_players(names)?; + if self.admin_list.contains(&sender.uuid) { + self.kick_players(words.clone()); + self.ban_players(words)?; } } "cheese" => self .client .send_command("group".to_string(), vec!["I love cheese!".to_string()]), "inv" => { - if !self.ban_list.contains(sender) { - self.invite_players(names); + if !self.ban_list.contains(&sender.uuid) { + if content == "inv" { + self.invite_players([sender.player_alias.as_str()].into_iter()); + } else { + self.invite_players(words); + } } } "kick" => { - if self.admin_list.contains(sender) { - self.kick_players(names); + if self.admin_list.contains(&sender.uuid) { + self.kick_players(words); } } "unban" => { - if self.admin_list.contains(sender) { - self.unban_players(names)?; + if self.admin_list.contains(&sender.uuid) { + self.unban_players(words)?; } } _ => {} @@ -197,9 +197,11 @@ impl Bot { fn invite_players<'a, T: Iterator>(&mut self, names: T) { for name in names { - if let Some(player_id) = self.find_uid(&name) { + if let Some(player_id) = self.find_uid(name) { self.client .send_invite(player_id.clone(), InviteKind::Group); + } else { + eprintln!("{name} could not be invited") } } } @@ -219,7 +221,7 @@ impl Bot { fn unban_players<'a, T: Iterator>(&mut self, names: T) -> Result<(), String> { let uuids = names .filter_map(|name| self.find_uuid(name)) - .collect::>(); + .collect::>(); for uuid in uuids { if let Some(index) = self @@ -255,10 +257,10 @@ impl Bot { }) } - fn find_uuid(&self, name: &str) -> Option { + fn find_uuid(&self, name: &str) -> Option { self.client.player_list().iter().find_map(|(_, info)| { if info.player_alias == name { - Some(info.uuid.to_string()) + Some(info.uuid) } else { None } diff --git a/src/main.rs b/src/main.rs index 7bb7127..f4002b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,13 +7,14 @@ use std::{ use bot::Bot; use serde::{Deserialize, Serialize}; +use veloren_common::uuid::Uuid; #[derive(Serialize, Deserialize)] struct Config { pub username: String, pub password: String, - pub admin_list: Vec, - pub ban_list: Vec, + pub admin_list: Vec, + pub ban_list: Vec, } impl Config {