aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/craft_dig_straight_down.rs
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-10-23 16:51:49 -0500
committermat <github@matdoes.dev>2022-10-23 16:51:49 -0500
commit2eade86cf7a12a6ec64496aedbfc3d3a3bd44e1a (patch)
treec010d3f02beaec29741b723a1bc6e7eaad59e193 /azalea/examples/craft_dig_straight_down.rs
parenta9ff79a10553026b0fa32f0e31f1e0442467ca78 (diff)
downloadazalea-drasl-2eade86cf7a12a6ec64496aedbfc3d3a3bd44e1a.tar.xz
make `handle` cleaner
Arc<Event> -> Event, Arc<Mutex<State>> -> State Items in State now need to have interior mutability (i.e. Arc<Mutex<T>>), but it's a worthwhile tradeoff since it allows the user to customize it for each field
Diffstat (limited to 'azalea/examples/craft_dig_straight_down.rs')
-rw-r--r--azalea/examples/craft_dig_straight_down.rs16
1 files changed, 9 insertions, 7 deletions
diff --git a/azalea/examples/craft_dig_straight_down.rs b/azalea/examples/craft_dig_straight_down.rs
index 48e1fd22..9e675f28 100644
--- a/azalea/examples/craft_dig_straight_down.rs
+++ b/azalea/examples/craft_dig_straight_down.rs
@@ -3,9 +3,9 @@ use azalea::{Bot, Client, Event};
use parking_lot::Mutex;
use std::sync::Arc;
-#[derive(Default)]
+#[derive(Default, Clone)]
struct State {
- pub started: bool,
+ pub started: Arc<Mutex<bool>>,
}
#[tokio::main]
@@ -16,7 +16,7 @@ async fn main() {
azalea::start(azalea::Options {
account,
address: "localhost",
- state: Arc::new(Mutex::new(State::default())),
+ state: State::default(),
plugins: vec![],
handle,
})
@@ -24,13 +24,13 @@ async fn main() {
.unwrap();
}
-async fn handle(bot: Client, event: Arc<Event>, state: Arc<Mutex<State>>) {
+async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
match event {
- Event::Message(m) => {
+ Event::Chat(m) => {
if m.username == bot.player.username {
- return;
+ return Ok(());
};
- if m.message = "go" {
+ if m.content == "go" {
// make sure we only start once
let ctx_lock = ctx.lock().unwrap();
if ctx_lock.started {
@@ -74,4 +74,6 @@ async fn handle(bot: Client, event: Arc<Event>, state: Arc<Mutex<State>>) {
}
_ => {}
}
+
+ Ok(())
}