Fix cheese command; Add ban command

This commit is contained in:
Jeff 2024-06-12 22:36:41 -04:00
parent 0f07c72fd2
commit 91d71deb1d
2 changed files with 60 additions and 11 deletions

View File

@ -4,7 +4,7 @@ use tokio::runtime::Runtime;
use veloren_client::{addr::ConnectionArgs, Client, Event}; use veloren_client::{addr::ConnectionArgs, Client, Event};
use veloren_common::{ use veloren_common::{
clock::Clock, clock::Clock,
comp::{invite::InviteKind, ChatType, ControllerInputs}, comp::{invite::InviteKind, ChatMode, ChatType, ControllerInputs},
uid::Uid, uid::Uid,
ViewDistances, ViewDistances,
}; };
@ -15,10 +15,16 @@ pub struct Bot {
client: Client, client: Client,
clock: Clock, clock: Clock,
admin_list: Vec<String>, admin_list: Vec<String>,
ban_list: Vec<String>,
} }
impl Bot { impl Bot {
pub fn new(username: &str, password: &str, admin_list: Vec<String>) -> Result<Self, String> { pub fn new(
username: &str,
password: &str,
admin_list: Vec<String>,
ban_list: Vec<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(1.0)); let clock = Clock::new(Duration::from_secs_f64(1.0));
@ -26,6 +32,7 @@ impl Bot {
client, client,
clock, clock,
admin_list, admin_list,
ban_list,
}) })
} }
@ -101,23 +108,34 @@ impl Bot {
} }
fn handle_message(&mut self, content: &str, sender: &String) -> Result<(), String> { fn handle_message(&mut self, content: &str, sender: &String) -> Result<(), String> {
let mut words = content.split_whitespace(); let mut names = content.split_whitespace();
if let Some(command) = words.next() { if let Some(command) = names.next() {
match command { match command {
"admin" => { "admin" => {
if self.admin_list.contains(sender) { if self.admin_list.contains(sender) {
self.adminify_players(words)?; self.adminify_players(names)?;
}
}
"ban" => {
if self.admin_list.contains(sender) {
self.kick_players(names.clone());
self.ban_players(names)?;
}
}
"inv" => {
if !self.ban_list.contains(sender) {
self.invite_players(names)
} }
} }
"inv" => self.invite_players(words),
"kick" => { "kick" => {
if self.admin_list.contains(sender) { if self.admin_list.contains(sender) {
self.kick_players(words) self.kick_players(names)
} }
} }
"cheese" => { "cheese" => {
self.client.send_chat("/g I love cheese!".to_string()); self.client.chat_mode = ChatMode::Group;
self.client.send_chat("I love cheese!".to_string());
} }
_ => {} _ => {}
} }
@ -132,15 +150,40 @@ impl Bot {
) -> Result<(), String> { ) -> Result<(), String> {
for name in names { for name in names {
if let Some(player_id) = self.find_uuid(&name) { if let Some(player_id) = self.find_uuid(&name) {
if !self.admin_list.contains(&player_id) {
self.admin_list.push(player_id); self.admin_list.push(player_id);
} }
} }
}
let old_config = Config::read()?; let old_config = Config::read()?;
let new_config = Config { let new_config = Config {
username: old_config.username, username: old_config.username,
password: old_config.password, password: old_config.password,
admin_list: self.admin_list.clone(), admin_list: self.admin_list.clone(),
ban_list: old_config.ban_list,
};
new_config.write()?;
Ok(())
}
fn ban_players<'a, T: Iterator<Item = &'a str>>(&mut self, names: T) -> Result<(), String> {
for name in names {
if let Some(player_id) = self.find_uuid(&name) {
if !self.ban_list.contains(&player_id) {
self.ban_list.push(player_id);
}
}
}
let old_config = Config::read()?;
let new_config = Config {
username: old_config.username,
password: old_config.password,
admin_list: old_config.admin_list,
ban_list: self.ban_list.clone(),
}; };
new_config.write()?; new_config.write()?;

View File

@ -13,6 +13,7 @@ struct Config {
pub username: String, pub username: String,
pub password: String, pub password: String,
pub admin_list: Vec<String>, pub admin_list: Vec<String>,
pub ban_list: Vec<String>,
} }
impl Config { impl Config {
@ -33,7 +34,12 @@ impl Config {
fn main() { fn main() {
let config = Config::read().unwrap(); let config = Config::read().unwrap();
let mut bot = Bot::new(&config.username, &config.password, config.admin_list) let mut bot = Bot::new(
&config.username,
&config.password,
config.admin_list,
config.ban_list,
)
.expect("Failed to create bot"); .expect("Failed to create bot");
bot.select_character().expect("Failed to select character"); bot.select_character().expect("Failed to select character");