aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2024-12-23 01:12:27 +0000
committermat <git@matdoes.dev>2024-12-23 01:12:27 +0000
commit3b1fe490b01258b370d58e70c71945f522010639 (patch)
tree512ae2513109f53efc1ab7c7800bfc7ef3bf4e6e /azalea/examples
parent478fe722f58137634525dc4ae1a8a9bdc9cc32fa (diff)
downloadazalea-drasl-3b1fe490b01258b370d58e70c71945f522010639.tar.xz
make testbot take cli args
Diffstat (limited to 'azalea/examples')
-rw-r--r--azalea/examples/testbot/main.rs79
1 files changed, 59 insertions, 20 deletions
diff --git a/azalea/examples/testbot/main.rs b/azalea/examples/testbot/main.rs
index 9c8d1085..81cd2bb8 100644
--- a/azalea/examples/testbot/main.rs
+++ b/azalea/examples/testbot/main.rs
@@ -2,7 +2,8 @@
//!
//! Usage:
//! - Modify the consts below if necessary.
-//! - Run `cargo r --example testbot`
+//! - Run `cargo r --example testbot -- --owner <owner> --name <username/email>
+//! --address <address>`.
//! - Commands are prefixed with `!` in chat. You can send them either in public
//! chat or as a /msg.
//! - Some commands to try are `!goto`, `!killaura true`, `!down`. Check the
@@ -15,6 +16,7 @@ mod commands;
pub mod killaura;
use std::time::Duration;
+use std::{env, process};
use std::{sync::Arc, thread};
use azalea::brigadier::command_dispatcher::CommandDispatcher;
@@ -26,10 +28,6 @@ use azalea::ClientInformation;
use commands::{register_commands, CommandSource};
use parking_lot::Mutex;
-const USERNAME: &str = "azalea";
-const ADDRESS: &str = "localhost";
-/// The bot will only listen to commands sent by the player with this username.
-const OWNER_USERNAME: &str = "py5";
/// Whether the bot should run /particle a ton of times to show where it's
/// pathfinding to. You should only have this on if the bot has operator
/// permissions, otherwise it'll just spam the server console unnecessarily.
@@ -37,27 +35,28 @@ const PATHFINDER_DEBUG_PARTICLES: bool = false;
#[tokio::main]
async fn main() {
+ let args = parse_args();
+
thread::spawn(deadlock_detection_thread);
- let account = Account::offline(USERNAME);
+ let account = if args.name.contains('@') {
+ Account::microsoft(&args.name).await.unwrap()
+ } else {
+ Account::offline(&args.name)
+ };
let mut commands = CommandDispatcher::new();
register_commands(&mut commands);
- let commands = Arc::new(commands);
+
+ let join_address = args.address.clone();
let builder = SwarmBuilder::new();
builder
.set_handler(handle)
.set_swarm_handler(swarm_handle)
- .add_account_with_state(
- account,
- State {
- commands: commands.clone(),
- ..Default::default()
- },
- )
+ .add_account_with_state(account, State::new(args, commands))
.join_delay(Duration::from_millis(100))
- .start(ADDRESS)
+ .start(join_address)
.await
.unwrap();
}
@@ -92,17 +91,19 @@ pub enum BotTask {
None,
}
-#[derive(Component, Clone)]
+#[derive(Component, Clone, Default)]
pub struct State {
+ pub args: Args,
pub commands: Arc<CommandDispatcher<Mutex<CommandSource>>>,
pub killaura: bool,
pub task: Arc<Mutex<BotTask>>,
}
-impl Default for State {
- fn default() -> Self {
+impl State {
+ fn new(args: Args, commands: CommandDispatcher<Mutex<CommandSource>>) -> Self {
Self {
- commands: Arc::new(CommandDispatcher::new()),
+ args,
+ commands: Arc::new(commands),
killaura: true,
task: Arc::new(Mutex::new(BotTask::None)),
}
@@ -131,7 +132,7 @@ async fn handle(bot: Client, event: azalea::Event, state: State) -> anyhow::Resu
let (Some(username), content) = chat.split_sender_and_content() else {
return Ok(());
};
- if username != OWNER_USERNAME {
+ if username != state.args.owner {
return Ok(());
}
@@ -201,3 +202,41 @@ async fn swarm_handle(
Ok(())
}
+
+#[derive(Debug, Clone, Default)]
+pub struct Args {
+ pub owner: String,
+ pub name: String,
+ pub address: String,
+}
+
+fn parse_args() -> Args {
+ let mut owner_username = None;
+ let mut bot_username = None;
+ let mut address = None;
+
+ let mut args = env::args().skip(1);
+ while let Some(arg) = args.next() {
+ match arg.as_str() {
+ "--owner" | "-O" => {
+ owner_username = args.next();
+ }
+ "--name" | "-N" => {
+ bot_username = args.next();
+ }
+ "--address" | "-A" => {
+ address = args.next();
+ }
+ _ => {
+ eprintln!("Unknown argument: {}", arg);
+ process::exit(1);
+ }
+ }
+ }
+
+ Args {
+ owner: owner_username.unwrap_or_else(|| "admin".to_string()),
+ name: bot_username.unwrap_or_else(|| "azalea".to_string()),
+ address: address.unwrap_or_else(|| "localhost".to_string()),
+ }
+}