1use serde::Deserialize;
2
3use crate::bot::Bot;
4use crate::context::Context;
5use crate::error::FrameworkResult;
6use crate::message::MessageElement;
7use crate::types::{Channel, ChannelType, Guild, GuildMember, GuildRole, Login, Message, User};
8use std::sync::Arc;
9
10#[derive(Debug, Clone, Deserialize)]
11pub struct SessionEvent {
12 pub id: i64,
14 #[serde(rename = "type")]
16 pub ty: String,
17 pub platform: String,
19 pub self_id: String,
21 pub timestamp: i64,
23 pub channel: Channel,
25 pub guild: Guild,
27 pub login: Login,
29 pub member: GuildMember,
31 pub message: Message,
33 pub operator: User,
35 pub role: GuildRole,
37 pub user: User,
39}
40
41#[derive(Debug, Clone, Deserialize)]
42pub struct Author {
43 pub user: User,
45 pub nick: Option<String>,
47 pub avatar: Option<String>,
49 pub id: String,
51 pub name: Option<String>,
53 pub is_bot: Option<bool>,
55 #[serde(rename = "joined_at")]
57 pub joined_at_ms: Option<i64>,
58}
59
60pub struct Session {
62 pub app: Arc<Context>,
64 pub bot: Arc<Bot>,
66 pub channel: Channel,
68 pub event: SessionEvent,
70 pub user: User,
72 pub author: Author,
73 pub channel_id: String,
74 pub channel_name: String,
75 pub content: String,
76 pub elements: Vec<MessageElement>,
77 pub guild_id: String,
78 pub guild_name: String,
79 pub id: String,
80 pub is_direct: bool,
81 pub message_id: String,
82 pub platform: String,
83 pub quote: Option<Box<Message>>,
84 pub self_id: String,
85 pub timestamp: i64,
86 pub type_: String,
87 pub user_id: String,
88}
89
90impl Session {
91 pub fn new(bot: Arc<Bot>, event: SessionEvent) -> Self {
98 let app = bot.ctx.clone();
99 let channel = event.channel.clone();
100 let user = event.user.clone();
101 let nick = match event.member.nick.clone() {
102 Some(nick) => Some(nick),
103 None => event.user.name.clone(),
104 };
105 let avatar = match event.member.avatar.clone() {
106 Some(avatar) => Some(avatar),
107 None => event.user.avatar.clone(),
108 };
109 let author = Author {
110 user: event.user.clone(),
111 nick,
112 avatar,
113 id: event.user.id.clone(),
114 name: event.user.name.clone(),
115 is_bot: event.user.is_bot,
116 joined_at_ms: event.member.joined_at_ms,
117 };
118 let channel_id = event.channel.id.clone();
119 let channel_name = event.channel.name.clone();
120 let content = event.message.content.clone();
121 let elements = event.message.elements.clone();
122 let guild_id = event.guild.id.clone();
123 let guild_name = event.guild.name.clone();
124 let id = event.id.to_string();
125 let is_direct = event.channel.ty == ChannelType::Direct;
126 let message_id = event.message.id.to_string();
127 let platform = event.platform.clone();
128 let quote = event.message.quote.clone();
129 let self_id = event.self_id.clone();
130 let timestamp = event.timestamp;
131 let type_ = event.ty.clone();
132 let user_id = user.id.clone();
133
134 Session {
135 app,
136 bot,
137 channel,
138 event,
139 user,
140 author,
141 channel_id,
142 channel_name,
143 content,
144 elements,
145 guild_id,
146 guild_name,
147 id,
148 is_direct,
149 message_id,
150 platform,
151 quote,
152 self_id,
153 timestamp,
154 type_,
155 user_id,
156 }
157 }
158
159 pub async fn send(&self, elements: &[MessageElement]) -> FrameworkResult<Vec<String>> {
161 self.bot.send_message(&self.channel_id, elements).await
162 }
163}