azalea/client_impl/chat.rs
1use azalea_client::client_chat::{ChatKind, SendChatEvent, handler::SendChatKindEvent};
2
3use crate::Client;
4
5impl Client {
6 /// Send a chat message to the server.
7 ///
8 /// This only sends the chat packet and not the command packet, which means
9 /// on some servers you can use this to send chat messages that start
10 /// with a `/`. The [`Client::chat`] function handles checking whether
11 /// the message is a command and using the proper packet for you, so you
12 /// should use that instead.
13 pub fn write_chat_packet(&self, message: &str) {
14 self.ecs.write().write_message(SendChatKindEvent {
15 entity: self.entity,
16 content: message.to_owned(),
17 kind: ChatKind::Message,
18 });
19 }
20
21 /// Send a command packet to the server. The `command` argument should not
22 /// include the slash at the front.
23 ///
24 /// You can also just use [`Client::chat`] and start your message with a `/`
25 /// to send a command.
26 pub fn write_command_packet(&self, command: &str) {
27 self.ecs.write().write_message(SendChatKindEvent {
28 entity: self.entity,
29 content: command.to_owned(),
30 kind: ChatKind::Command,
31 });
32 }
33
34 /// Send a message in chat.
35 ///
36 /// If the content starts with a `/`, a command is sent.
37 ///
38 /// ```rust,no_run
39 /// # use azalea::Client;
40 /// # async fn example(bot: Client) -> eyre::Result<()> {
41 /// bot.chat("Hello, world!");
42 /// # Ok(())
43 /// # }
44 /// ```
45 pub fn chat(&self, content: impl Into<String>) {
46 self.ecs.write().write_message(SendChatEvent {
47 entity: self.entity,
48 content: content.into(),
49 });
50 }
51}