Fix ID bug

This commit is contained in:
Jeff 2024-06-13 20:19:49 -04:00
parent 2520a3a8db
commit 021f8154c8
4 changed files with 37 additions and 32 deletions

1
Cargo.lock generated
View File

@ -732,6 +732,7 @@ dependencies = [
"toml", "toml",
"veloren-client", "veloren-client",
"veloren-common", "veloren-common",
"veloren-common-net",
] ]
[[package]] [[package]]

View File

@ -7,6 +7,7 @@ edition = "2021"
tokio = "1.38.0" tokio = "1.38.0"
veloren-common = { git = "https://gitlab.com/veloren/veloren", branch = "master", features = ["no-assets"] } veloren-common = { git = "https://gitlab.com/veloren/veloren", branch = "master", features = ["no-assets"] }
veloren-client = { git = "https://gitlab.com/veloren/veloren", branch = "master" } 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" toml = "0.8.14"
serde = { version = "1.0.203", features = ["derive"] } serde = { version = "1.0.203", features = ["derive"] }

View File

@ -6,24 +6,26 @@ use veloren_common::{
clock::Clock, clock::Clock,
comp::{invite::InviteKind, ChatType, ControllerInputs}, comp::{invite::InviteKind, ChatType, ControllerInputs},
uid::Uid, uid::Uid,
uuid::Uuid,
ViewDistances, ViewDistances,
}; };
use veloren_common_net::msg::PlayerInfo;
use crate::Config; use crate::Config;
pub struct Bot { pub struct Bot {
client: Client, client: Client,
clock: Clock, clock: Clock,
admin_list: Vec<String>, admin_list: Vec<Uuid>,
ban_list: Vec<String>, ban_list: Vec<Uuid>,
} }
impl Bot { impl Bot {
pub fn new( pub fn new(
username: &str, username: &str,
password: &str, password: &str,
admin_list: Vec<String>, admin_list: Vec<Uuid>,
ban_list: Vec<String>, ban_list: Vec<Uuid>,
) -> Result<Self, String> { ) -> Result<Self, String> {
let client = connect_to_veloren(username, password)?; let client = connect_to_veloren(username, password)?;
let clock = Clock::new(Duration::from_secs_f64(0.1)); let clock = Clock::new(Duration::from_secs_f64(0.1));
@ -87,17 +89,11 @@ impl Bot {
if let Event::Chat(message) = event { if let Event::Chat(message) = event {
match message.chat_type { match message.chat_type {
ChatType::Tell(sender, _) | ChatType::Group(sender, _) => { ChatType::Tell(sender, _) | ChatType::Group(sender, _) => {
let sender_uuid = self let sender_info = self.client.player_list().get(&sender).unwrap().clone();
.client
.player_list()
.get(&sender)
.unwrap()
.uuid
.to_string();
self.handle_message( self.handle_message(
message.into_content().as_plain().unwrap_or(""), message.into_content().as_plain().unwrap_or(""),
&sender_uuid, &sender_info,
)?; )?;
} }
_ => {} _ => {}
@ -107,38 +103,42 @@ impl Bot {
Ok(()) Ok(())
} }
fn handle_message(&mut self, content: &str, sender: &String) -> Result<(), String> { fn handle_message(&mut self, content: &str, sender: &PlayerInfo) -> Result<(), String> {
let mut names = content.split_whitespace(); let mut words = content.split_whitespace();
if let Some(command) = names.next() { if let Some(command) = words.next() {
match command { match command {
"admin" => { "admin" => {
if self.admin_list.contains(sender) || self.admin_list.is_empty() { if self.admin_list.contains(&sender.uuid) || self.admin_list.is_empty() {
self.adminify_players(names)?; self.adminify_players(words)?;
} }
} }
"ban" => { "ban" => {
if self.admin_list.contains(sender) { if self.admin_list.contains(&sender.uuid) {
self.kick_players(names.clone()); self.kick_players(words.clone());
self.ban_players(names)?; self.ban_players(words)?;
} }
} }
"cheese" => self "cheese" => self
.client .client
.send_command("group".to_string(), vec!["I love cheese!".to_string()]), .send_command("group".to_string(), vec!["I love cheese!".to_string()]),
"inv" => { "inv" => {
if !self.ban_list.contains(sender) { if !self.ban_list.contains(&sender.uuid) {
self.invite_players(names); if content == "inv" {
self.invite_players([sender.player_alias.as_str()].into_iter());
} else {
self.invite_players(words);
}
} }
} }
"kick" => { "kick" => {
if self.admin_list.contains(sender) { if self.admin_list.contains(&sender.uuid) {
self.kick_players(names); self.kick_players(words);
} }
} }
"unban" => { "unban" => {
if self.admin_list.contains(sender) { if self.admin_list.contains(&sender.uuid) {
self.unban_players(names)?; self.unban_players(words)?;
} }
} }
_ => {} _ => {}
@ -197,9 +197,11 @@ impl Bot {
fn invite_players<'a, T: Iterator<Item = &'a str>>(&mut self, names: T) { fn invite_players<'a, T: Iterator<Item = &'a str>>(&mut self, names: T) {
for name in names { for name in names {
if let Some(player_id) = self.find_uid(&name) { if let Some(player_id) = self.find_uid(name) {
self.client self.client
.send_invite(player_id.clone(), InviteKind::Group); .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<Item = &'a str>>(&mut self, names: T) -> Result<(), String> { fn unban_players<'a, T: Iterator<Item = &'a str>>(&mut self, names: T) -> Result<(), String> {
let uuids = names let uuids = names
.filter_map(|name| self.find_uuid(name)) .filter_map(|name| self.find_uuid(name))
.collect::<Vec<String>>(); .collect::<Vec<Uuid>>();
for uuid in uuids { for uuid in uuids {
if let Some(index) = self if let Some(index) = self
@ -255,10 +257,10 @@ impl Bot {
}) })
} }
fn find_uuid(&self, name: &str) -> Option<String> { fn find_uuid(&self, name: &str) -> Option<Uuid> {
self.client.player_list().iter().find_map(|(_, info)| { self.client.player_list().iter().find_map(|(_, info)| {
if info.player_alias == name { if info.player_alias == name {
Some(info.uuid.to_string()) Some(info.uuid)
} else { } else {
None None
} }

View File

@ -7,13 +7,14 @@ use std::{
use bot::Bot; use bot::Bot;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use veloren_common::uuid::Uuid;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct Config { struct Config {
pub username: String, pub username: String,
pub password: String, pub password: String,
pub admin_list: Vec<String>, pub admin_list: Vec<Uuid>,
pub ban_list: Vec<String>, pub ban_list: Vec<Uuid>,
} }
impl Config { impl Config {