aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/examples/echo.rs
blob: f37cd904230ad49a1caf63569e400c726825863a (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
47
48
49
//! A simple bot that repeats chat messages sent by other players.

use azalea_client::{Account, Client, Event};

#[tokio::main]
async fn main() {
    env_logger::init();
    // deadlock detection, you can safely delete this block if you're not trying to
    // debug deadlocks in azalea
    {
        use parking_lot::deadlock;
        use std::thread;
        use std::time::Duration;
        thread::spawn(move || loop {
            thread::sleep(Duration::from_secs(10));
            let deadlocks = deadlock::check_deadlock();
            if deadlocks.is_empty() {
                continue;
            }
            println!("{} deadlocks detected", deadlocks.len());
            for (i, threads) in deadlocks.iter().enumerate() {
                println!("Deadlock #{i}");
                for t in threads {
                    println!("Thread Id {:#?}", t.thread_id());
                    println!("{:#?}", t.backtrace());
                }
            }
        });
    }

    let account = Account::offline("bot");
    // or let account = Account::microsoft("email").await;

    let (client, mut rx) = Client::join(&account, "localhost").await.unwrap();

    while let Some(event) = rx.recv().await {
        match &event {
            Event::Chat(m) => {
                if let (Some(sender), content) = m.split_sender_and_content() {
                    if sender == client.profile.name {
                        continue; // ignore our own messages
                    }
                    client.chat(&content);
                };
            }
            _ => {}
        }
    }
}