aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/echo.rs
blob: 75cd235f0a27ae056f4b79bc63d5f6b04028f1ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::sync::Arc;

use azalea::{Account, Client, Event};
use parking_lot::Mutex;

#[tokio::main]
async fn main() {
    let account = Account::offline("bot");
    // or let account = azalea::Account::microsoft("access token").await;

    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::Kick(m) => {
            println!(m);
            bot.reconnect().await.unwrap();
        }
        Event::HungerUpdate(h) => {
            if !h.using_held_item() && h.hunger <= 17 {
                bot.hold(azalea::ItemGroup::Food).await?;
                bot.use_held_item().await?;
            }
        }
        _ => {}
    }

    Ok(())
}