aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/echo.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-10-07 19:57:42 -0500
committerGitHub <noreply@github.com>2022-10-07 19:57:42 -0500
commitba4cfaafaec97a3c5b9405fe542035ebe9039edd (patch)
tree5fc7340a49f96d84f86ed6adf400ad461c47d1b6 /azalea/examples/echo.rs
parente0bcab53b8a3721a008e47062c6b5972fa64b8ad (diff)
downloadazalea-drasl-ba4cfaafaec97a3c5b9405fe542035ebe9039edd.tar.xz
Bot API (#27)
Basically make the `azalea` crate have stuff
Diffstat (limited to 'azalea/examples/echo.rs')
-rw-r--r--azalea/examples/echo.rs60
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(())
}