Skip to main content

ChatPacket

Enum ChatPacket 

pub enum ChatPacket {
    System(Arc<ClientboundSystemChat>),
    Player(Arc<ClientboundPlayerChat>),
    Disguised(Arc<ClientboundDisguisedChat>),
}
Expand description

A chat packet, either a system message or a chat message.

Variants§

§

System(Arc<ClientboundSystemChat>)

§

Player(Arc<ClientboundPlayerChat>)

§

Disguised(Arc<ClientboundDisguisedChat>)

Implementations§

§

impl ChatPacket

pub fn message(&self) -> FormattedText

Get the message shown in chat for this packet.

See Self::split_sender_and_content for more details about how this works.

Examples found in repository?
azalea/examples/testbot/main.rs (line 156)
132async fn handle(bot: Client, event: azalea::Event, state: State) -> eyre::Result<()> {
133    let swarm = bot.resource::<SwarmState>();
134
135    match event {
136        azalea::Event::Init => {
137            bot.set_client_information(ClientInformation {
138                view_distance: 32,
139                ..Default::default()
140            });
141            if swarm.args.pathfinder_debug_particles {
142                bot.ecs
143                    .write()
144                    .entity_mut(bot.entity)
145                    .insert(PathfinderDebugParticles);
146            }
147        }
148        azalea::Event::Chat(chat) => {
149            let (Some(username), content) = chat.split_sender_and_content() else {
150                return Ok(());
151            };
152            if username != swarm.args.owner_username {
153                return Ok(());
154            }
155
156            println!("{:?}", chat.message());
157
158            let command = if chat.is_whisper() {
159                Some(content)
160            } else {
161                content.strip_prefix('!').map(|s| s.to_owned())
162            };
163            if let Some(command) = command {
164                match swarm.commands.execute(
165                    command,
166                    Mutex::new(CommandSource {
167                        bot: bot.clone(),
168                        chat: chat.clone(),
169                        state: state.clone(),
170                    }),
171                ) {
172                    Ok(_) => {}
173                    Err(err) => {
174                        eprintln!("{err:?}");
175                        let command_source = CommandSource {
176                            bot,
177                            chat: chat.clone(),
178                            state: state.clone(),
179                        };
180                        command_source.reply(format!("{err:?}"));
181                    }
182                }
183            }
184        }
185        azalea::Event::Tick => {
186            killaura::tick(bot.clone(), state.clone())?;
187
188            if bot.ticks_connected().is_multiple_of(5) {
189                if let Some(following) = &*state.following_entity.lock()
190                    && following.is_alive()
191                {
192                    let goal = RadiusGoal::new(following.position(), 3.);
193                    if bot.is_calculating_path() {
194                        // keep waiting
195                    } else if !goal.success(bot.position().into()) || bot.is_executing_path() {
196                        bot.start_goto_with_opts(
197                            goal,
198                            PathfinderOpts::new()
199                                .retry_on_no_path(false)
200                                .max_timeout(Duration::from_secs(1)),
201                        );
202                    } else {
203                        following.look_at();
204                    }
205                }
206            }
207        }
208        azalea::Event::Login => {
209            println!("Got login event")
210        }
211        _ => {}
212    }
213
214    Ok(())
215}
216async fn swarm_handle(_swarm: Swarm, event: SwarmEvent, _state: SwarmState) -> eyre::Result<()> {
217    match &event {
218        SwarmEvent::Disconnect(account, _join_opts) => {
219            println!("bot got kicked! {}", account.username());
220        }
221        SwarmEvent::Chat(chat) => {
222            if chat.message().to_string() == "The particle was not visible for anybody" {
223                return Ok(());
224            }
225            println!("{}", chat.message().to_ansi());
226        }
227        _ => {}
228    }
229
230    Ok(())
231}

pub fn split_sender_and_content(&self) -> (Option<String>, String)

A convenience function to determine the username of the sender and the content of a chat message.

This does not preserve formatting codes.

This function uses a few checks to attempt to split the chat message, and is intended to work on most servers. It won’t work for every server though, so in certain cases you may have to reimplement this yourself.

If it’s not a player-sent chat message or the sender couldn’t be determined, the username part will be None.

Also see Self::sender and Self::content if you only need one of the parts.

Examples found in repository?
azalea/examples/echo.rs (line 21)
19async fn handle(bot: Client, event: Event, _state: State) -> eyre::Result<()> {
20    if let Event::Chat(m) = event
21        && let (Some(sender), content) = m.split_sender_and_content()
22    {
23        if sender == bot.username() {
24            // ignore our own messages
25            return Ok(());
26        }
27        bot.chat(content);
28    }
29
30    Ok(())
31}
More examples
Hide additional examples
azalea/examples/testbot/main.rs (line 149)
132async fn handle(bot: Client, event: azalea::Event, state: State) -> eyre::Result<()> {
133    let swarm = bot.resource::<SwarmState>();
134
135    match event {
136        azalea::Event::Init => {
137            bot.set_client_information(ClientInformation {
138                view_distance: 32,
139                ..Default::default()
140            });
141            if swarm.args.pathfinder_debug_particles {
142                bot.ecs
143                    .write()
144                    .entity_mut(bot.entity)
145                    .insert(PathfinderDebugParticles);
146            }
147        }
148        azalea::Event::Chat(chat) => {
149            let (Some(username), content) = chat.split_sender_and_content() else {
150                return Ok(());
151            };
152            if username != swarm.args.owner_username {
153                return Ok(());
154            }
155
156            println!("{:?}", chat.message());
157
158            let command = if chat.is_whisper() {
159                Some(content)
160            } else {
161                content.strip_prefix('!').map(|s| s.to_owned())
162            };
163            if let Some(command) = command {
164                match swarm.commands.execute(
165                    command,
166                    Mutex::new(CommandSource {
167                        bot: bot.clone(),
168                        chat: chat.clone(),
169                        state: state.clone(),
170                    }),
171                ) {
172                    Ok(_) => {}
173                    Err(err) => {
174                        eprintln!("{err:?}");
175                        let command_source = CommandSource {
176                            bot,
177                            chat: chat.clone(),
178                            state: state.clone(),
179                        };
180                        command_source.reply(format!("{err:?}"));
181                    }
182                }
183            }
184        }
185        azalea::Event::Tick => {
186            killaura::tick(bot.clone(), state.clone())?;
187
188            if bot.ticks_connected().is_multiple_of(5) {
189                if let Some(following) = &*state.following_entity.lock()
190                    && following.is_alive()
191                {
192                    let goal = RadiusGoal::new(following.position(), 3.);
193                    if bot.is_calculating_path() {
194                        // keep waiting
195                    } else if !goal.success(bot.position().into()) || bot.is_executing_path() {
196                        bot.start_goto_with_opts(
197                            goal,
198                            PathfinderOpts::new()
199                                .retry_on_no_path(false)
200                                .max_timeout(Duration::from_secs(1)),
201                        );
202                    } else {
203                        following.look_at();
204                    }
205                }
206            }
207        }
208        azalea::Event::Login => {
209            println!("Got login event")
210        }
211        _ => {}
212    }
213
214    Ok(())
215}

pub fn sender(&self) -> Option<String>

Get the username of the sender of the message.

If it’s not a player-sent chat message or the sender couldn’t be determined, this will be None.

See Self::split_sender_and_content for more details about how this works.

Examples found in repository?
azalea/examples/steal.rs (line 29)
27async fn handle(bot: Client, event: Event, state: State) -> eyre::Result<()> {
28    if let Event::Chat(m) = event {
29        if m.sender() == Some(bot.username()) {
30            return Ok(());
31        };
32        if m.content() != "go" {
33            return Ok(());
34        }
35
36        steal(bot, state).await?;
37    }
38
39    Ok(())
40}
More examples
Hide additional examples
azalea/examples/testbot/commands.rs (line 28)
23    pub fn reply(&self, message: impl Into<String>) {
24        let message = message.into();
25        if self.chat.is_whisper() {
26            // /msg instead of /w for compat with custom servers
27            self.bot
28                .chat(format!("/msg {} {message}", self.chat.sender().unwrap()));
29        } else {
30            self.bot.chat(message);
31        }
32    }
33
34    pub fn entity(&self) -> Option<azalea::EntityRef> {
35        let username = self.chat.sender()?;
36        self.bot
37            .any_entity_by::<&GameProfileComponent, With<Player>>(
38                |profile: &GameProfileComponent| profile.name == username,
39            )
40    }

pub fn sender_uuid(&self) -> Option<Uuid>

Get the UUID of the sender of the message.

If it’s not a player-sent chat message, this will be None (this is sometimes the case when a server uses a plugin to modify chat messages).

pub fn content(&self) -> String

Get the content part of the message as a string.

This does not preserve formatting codes. If it’s not a player-sent chat message or the sender couldn’t be determined, this will contain the entire message.

Examples found in repository?
azalea/examples/steal.rs (line 32)
27async fn handle(bot: Client, event: Event, state: State) -> eyre::Result<()> {
28    if let Event::Chat(m) = event {
29        if m.sender() == Some(bot.username()) {
30            return Ok(());
31        };
32        if m.content() != "go" {
33            return Ok(());
34        }
35
36        steal(bot, state).await?;
37    }
38
39    Ok(())
40}

pub fn new(message: &str) -> ChatPacket

Create a new ChatPacket from a string. This is meant to be used as a convenience function for testing.

pub fn is_whisper(&self) -> bool

Whether this message is an incoming whisper message (i.e. someone else messaged the bot with /msg).

This is not guaranteed to work correctly on custom servers.

Examples found in repository?
azalea/examples/testbot/commands.rs (line 25)
23    pub fn reply(&self, message: impl Into<String>) {
24        let message = message.into();
25        if self.chat.is_whisper() {
26            // /msg instead of /w for compat with custom servers
27            self.bot
28                .chat(format!("/msg {} {message}", self.chat.sender().unwrap()));
29        } else {
30            self.bot.chat(message);
31        }
32    }
More examples
Hide additional examples
azalea/examples/testbot/main.rs (line 158)
132async fn handle(bot: Client, event: azalea::Event, state: State) -> eyre::Result<()> {
133    let swarm = bot.resource::<SwarmState>();
134
135    match event {
136        azalea::Event::Init => {
137            bot.set_client_information(ClientInformation {
138                view_distance: 32,
139                ..Default::default()
140            });
141            if swarm.args.pathfinder_debug_particles {
142                bot.ecs
143                    .write()
144                    .entity_mut(bot.entity)
145                    .insert(PathfinderDebugParticles);
146            }
147        }
148        azalea::Event::Chat(chat) => {
149            let (Some(username), content) = chat.split_sender_and_content() else {
150                return Ok(());
151            };
152            if username != swarm.args.owner_username {
153                return Ok(());
154            }
155
156            println!("{:?}", chat.message());
157
158            let command = if chat.is_whisper() {
159                Some(content)
160            } else {
161                content.strip_prefix('!').map(|s| s.to_owned())
162            };
163            if let Some(command) = command {
164                match swarm.commands.execute(
165                    command,
166                    Mutex::new(CommandSource {
167                        bot: bot.clone(),
168                        chat: chat.clone(),
169                        state: state.clone(),
170                    }),
171                ) {
172                    Ok(_) => {}
173                    Err(err) => {
174                        eprintln!("{err:?}");
175                        let command_source = CommandSource {
176                            bot,
177                            chat: chat.clone(),
178                            state: state.clone(),
179                        };
180                        command_source.reply(format!("{err:?}"));
181                    }
182                }
183            }
184        }
185        azalea::Event::Tick => {
186            killaura::tick(bot.clone(), state.clone())?;
187
188            if bot.ticks_connected().is_multiple_of(5) {
189                if let Some(following) = &*state.following_entity.lock()
190                    && following.is_alive()
191                {
192                    let goal = RadiusGoal::new(following.position(), 3.);
193                    if bot.is_calculating_path() {
194                        // keep waiting
195                    } else if !goal.success(bot.position().into()) || bot.is_executing_path() {
196                        bot.start_goto_with_opts(
197                            goal,
198                            PathfinderOpts::new()
199                                .retry_on_no_path(false)
200                                .max_timeout(Duration::from_secs(1)),
201                        );
202                    } else {
203                        following.look_at();
204                    }
205                }
206            }
207        }
208        azalea::Event::Login => {
209            println!("Got login event")
210        }
211        _ => {}
212    }
213
214    Ok(())
215}

Trait Implementations§

§

impl Clone for ChatPacket

§

fn clone(&self) -> ChatPacket

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for ChatPacket

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl PartialEq for ChatPacket

§

fn eq(&self, other: &ChatPacket) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl StructuralPartialEq for ChatPacket

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> CompatExt for T

§

fn compat(self) -> Compat<T>

Applies the [Compat] adapter by value. Read more
§

fn compat_ref(&self) -> Compat<&T>

Applies the [Compat] adapter by shared reference. Read more
§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the [Compat] adapter by mutable reference. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSend for T
where T: Any + Send,

§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> IntoResult<T> for T

§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
§

impl<A> Is for A
where A: Any,

§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> TypeData for T
where T: 'static + Send + Sync + Clone,

§

fn clone_type_data(&self) -> Box<dyn TypeData>

Creates a type-erased clone of this value.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ConditionalSend for T
where T: Send,