disruption/implementations/channel/message/
message.rs

1use disruption_types::channel::{MessageApiType, MessageReferenceApiType};
2
3use crate::{implementations::channel::Channel, internal::RestClient, Result};
4
5/// Struct representing a message send in a Discord channel.
6#[derive(Debug, Clone)]
7pub struct Message {
8    rest: RestClient,
9    msg: MessageApiType,
10    channel: Option<Channel>,
11}
12
13impl Message {
14    pub async fn new(rest: RestClient, msg: MessageApiType) -> Self {
15        let channel = Channel::from_id(rest.clone(), &msg.channel_id).await;
16        Message {
17            rest,
18            msg,
19            channel: channel.ok(),
20        }
21    }
22
23    /// Get the content of the message.
24    pub fn content(&self) -> &str {
25        self.msg.content.as_str()
26    }
27
28    /// Get the author of the message.
29    pub fn author(&self) -> &str {
30        // TODO: Return actual user object
31        self.msg.author.username.as_str()
32    }
33
34    pub fn channel(&self) -> &Option<Channel> {
35        &self.channel
36    }
37
38    /// Reply to this message.
39    pub async fn reply(&self, content: &str) -> Result<()> {
40        match self.channel() {
41            None => (),
42            Some(channel) => {
43                channel
44                    .send(MessageApiType {
45                        content: content.to_owned(),
46                        message_reference: Some(MessageReferenceApiType {
47                            message_id: Some(self.msg.id.clone()),
48                            ..Default::default()
49                        }),
50                        ..Default::default()
51                    })
52                    .await?;
53            }
54        }
55        Ok(())
56    }
57}