From 2eade86cf7a12a6ec64496aedbfc3d3a3bd44e1a Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 23 Oct 2022 16:51:49 -0500 Subject: make `handle` cleaner Arc -> Event, Arc> -> State Items in State now need to have interior mutability (i.e. Arc>), but it's a worthwhile tradeoff since it allows the user to customize it for each field --- azalea/examples/craft_dig_straight_down.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'azalea/examples/craft_dig_straight_down.rs') 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>, } #[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, state: Arc>) { +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, state: Arc>) { } _ => {} } + + Ok(()) } -- cgit v1.2.3