azalea/client_impl/
chat.rs

1use azalea_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    /// ```rust,no_run
37    /// # use azalea::Client;
38    /// # async fn example(bot: Client) -> anyhow::Result<()> {
39    /// bot.chat("Hello, world!");
40    /// # Ok(())
41    /// # }
42    /// ```
43    pub fn chat(&self, content: impl Into<String>) {
44        self.ecs.write().write_message(SendChatEvent {
45            entity: self.entity,
46            content: content.into(),
47        });
48    }
49}