1
0
Fork 0
mirror of https://github.com/azalea-rs/azalea-viaversion.git synced 2025-08-02 23:44:39 +00:00
azalea-viaversion/examples/echo.rs
mat b847cc2bf1
Support Socks5 proxies (#14)
* add support for socks5 proxies

* update Cargo.lock and fix doctest

* make cli example docs use a code block for example usage
2025-05-07 16:50:14 -05:00

42 lines
1.5 KiB
Rust

//! An [`azalea`] bot that repeats chat messages sent by other players.
//!
//! # Note
//! The `never_type` feature is completely optional, see how the `swarm` example
//! does not use it.
#![feature(never_type)]
use azalea::{NoState, StartError, prelude::*};
use azalea_viaversion::ViaVersionPlugin;
#[tokio::main]
async fn main() -> Result<!, StartError> {
tracing_subscriber::fmt::init();
// Initialize a 1.21.4 ViaProxy instance
let plugin = ViaVersionPlugin::start("1.21.4").await;
let builder = ClientBuilder::new().add_plugins(plugin);
// Start the client and connect to a localhost server
let acc = Account::offline("Azalea");
builder.set_handler(handle).start(acc, "localhost").await
}
/// A simple event handler that repeats chat messages sent by other players.
async fn handle(bot: Client, event: Event, _: NoState) -> anyhow::Result<()> {
match event {
Event::Chat(message) => {
// Split the message into the sender and message content
let (sender, message) = message.split_sender_and_content();
// If the sender is not the bot, repeat the message
if sender.is_none_or(|sender| sender != bot.username()) {
bot.chat(&message);
}
}
// Log disconnect reasons
Event::Disconnect(Some(reason)) => eprintln!("Disconnected: {}", reason.to_ansi()),
Event::Disconnect(None) => eprintln!("Disconnected: No reason provided"),
_ => {}
}
Ok(())
}