diff options
Diffstat (limited to 'azalea/examples/echo.rs')
| -rw-r--r-- | azalea/examples/echo.rs | 60 |
1 files changed, 34 insertions, 26 deletions
diff --git a/azalea/examples/echo.rs b/azalea/examples/echo.rs index c9e46a09..75cd235f 100644 --- a/azalea/examples/echo.rs +++ b/azalea/examples/echo.rs @@ -1,38 +1,46 @@ -use azalea::{Account, Event}; +use std::sync::Arc; -let account = Account::offline("bot"); -// or let account = azalea::Account::microsoft("access token").await; +use azalea::{Account, Client, Event}; +use parking_lot::Mutex; -let bot = account.join("localhost".try_into().unwrap()).await.unwrap(); +#[tokio::main] +async fn main() { + let account = Account::offline("bot"); + // or let account = azalea::Account::microsoft("access token").await; -loop { - match bot.next().await { - Event::Message(m) { - if m.username == bot.username { return }; + azalea::start(azalea::Options { + account, + address: "localhost", + state: Arc::new(Mutex::new(State::default())), + plugins: vec![], + handle, + }) + .await + .unwrap(); +} + +pub struct State {} + +async fn handle(bot: Client, event: Arc<Event>, state: Arc<Mutex<State>>) -> anyhow::Result<()> { + match event { + Event::Chat(m) => { + if m.username == bot.username { + return Ok(()); // ignore our own messages + }; bot.chat(m.message).await; - }, - Event::Kicked(m) { + } + Event::Kick(m) => { println!(m); bot.reconnect().await.unwrap(); - }, - Event::Hunger(h) { + } + Event::HungerUpdate(h) => { if !h.using_held_item() && h.hunger <= 17 { - match bot.hold(azalea::ItemGroup::Food).await { - Ok(_) => {}, - Err(e) => { - println!("{}", e); - break; - } - } - match bot.use_held_item().await { - Ok(_) => {}, - Err(e) => { - println!("{}", e); - break; - } - } + bot.hold(azalea::ItemGroup::Food).await?; + bot.use_held_item().await?; } } _ => {} } + + Ok(()) } |
