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

use azalea::prelude::*;

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let account = Account::offline("bot");
    // or let account = Account::microsoft("email").await.unwrap();

    ClientBuilder::new()
        .set_handler(handle)
        .start(account, "localhost")
        .await
        .unwrap();
}

#[derive(Default, Clone, Component)]
pub struct State {}

async fn handle(bot: Client, event: Event, _state: State) -> anyhow::Result<()> {
    if let Event::Chat(m) = event
        && let (Some(sender), content) = m.split_sender_and_content()
    {
        if sender == bot.username() {
            // ignore our own messages
            return Ok(());
        }
        bot.chat(&content);
    }

    Ok(())
}