mutiny_rs/model/
message.rs1use crate::builders::edit_message::EditMessageBuilder;
2use crate::context::Context;
3use crate::http::HttpError;
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6use crate::model::channel::{ChannelId, PendingSend};
7use crate::model::embed::Embed;
8use crate::model::ready::Member;
9use crate::model::user::User;
10
11#[derive(Debug, Default)]
12pub struct SendMessage {
13 pub content: String,
14 pub nonce: Option<String>,
15 pub attachments: Vec<String>,
16 pub replies: HashMap<String, bool>,
17 pub embeds: Vec<Embed>,
18}
19
20#[derive(Debug, Serialize, Deserialize, Clone)]
21pub struct Message {
22 #[serde(rename = "_id")]
23 pub id: String,
24 pub nonce: Option<String>,
25 pub channel: ChannelId,
26 pub author: String,
27 pub user: Option<User>,
28 pub member: Option<Member>,
29 #[serde(default)]
30 pub content: String,
31 pub mentions: Option<Vec<String>>,
32 pub attachments: Option<Vec<MessageAttachments>>,
33 pub edited: Option<String>
34}
35
36#[derive(Debug, Serialize, Deserialize, Clone)]
37pub struct MessageAttachments {
38 #[serde(rename = "_id")]
39 pub id: String,
40 pub tag: String,
41 pub filename: String,
42 pub metadata: MessageMetadata,
43 pub content_type: String,
44 pub size: usize
45}
46
47#[derive(Debug, Serialize, Deserialize, Clone)]
48pub struct MessageMetadata {
49 #[serde(rename = "type")]
50 pub _type: String,
51 pub width: usize,
52 pub height: usize,
53}
54
55#[derive(Debug, Serialize, Deserialize, Clone)]
56pub struct Replies {
57 pub id: String,
58 pub mention: bool,
59 pub fail_if_not_exists: Option<bool>,
60}
61
62impl Message {
63 pub fn reply<'a>(&'a self, ctx: &'a Context) -> PendingSend<'a> {
64 self.channel.send(ctx).replies(vec![
65 Replies {
66 id: self.id.clone(),
67 mention: true,
68 fail_if_not_exists: Some(true),
69 }
70 ])
71 }
72 pub async fn author(&self, ctx: &Context) -> Option<User> {
73 ctx.cache.users.get(&self.author).await
74 }
75
76 pub async fn fetch_author(&self, ctx: &Context) -> Result<User, HttpError> {
77 if let Some(user) = self.author(ctx).await {
78 return Ok(user);
79 }
80
81 let user = ctx.http.fetch_user(&self.author).await?;
82
83 ctx.cache.users.insert(user.id.clone(), user.clone()).await;
84
85 Ok(user)
86 }
87
88 pub fn edit<'a>(&'a self, ctx: &'a Context) -> EditMessageBuilder<'a> {
89 EditMessageBuilder {
90 message: self,
91 ctx,
92 content: None,
93 embeds: None,
94 }
95 }
96}