1use zaun_community_protocol::command::object::{Embed, Mentions};
5use zaun_community_protocol::event::{Event as Catalogue, RemovalReason, VoiceLeftReason};
6use zaun_community_protocol::ids::HostId;
7
8use crate::bots::{EphemeralReply, Interaction, InteractionFailure, ModalOpen, Suggestions};
9use crate::client::Client;
10use crate::event::Event as Inner;
11use crate::objects::{Channel, Community, Member, Message, PartialMessage, Role};
12
13#[derive(Debug, Clone)]
15pub enum Event {
16 Ready(Community),
19 AppsReady(Community),
21 PresencesReady(Community),
23 PresenceUpdate {
26 community: Community,
28 presence: crate::Presence,
30 },
31 CommandsUpdate {
33 community: Community,
35 bot: zaun_community_protocol::ids::BotId,
37 },
38 BotUpdate {
40 community: Community,
42 card: Box<zaun_community_protocol::command::object::BotCard>,
44 },
45 BotRemove {
47 community: Community,
49 bot: zaun_community_protocol::ids::BotId,
51 member: Member,
53 },
54 InteractionCreate(Box<Interaction>),
56 InteractionReply(EphemeralReply),
58 ModalOpen(ModalOpen),
60 Autocomplete(Suggestions),
62 InteractionFail(InteractionFailure),
64 MessageCreate(Message),
66 MessageUpdate {
68 message: PartialMessage,
70 author: Option<Member>,
72 content: String,
74 embeds: Option<Vec<Embed>>,
77 mentions: Option<Mentions>,
80 edited_at: u64,
82 },
83 MessageDelete {
85 message: PartialMessage,
87 by: Option<Member>,
89 },
90 MessageDeleteBulk {
92 channel: Channel,
94 messages: Vec<PartialMessage>,
96 },
97 ReactionAdd(Reaction),
99 ReactionRemove(Reaction),
101 ChannelCreate(Channel),
103 ChannelUpdate(Channel),
105 ChannelDelete(Channel),
107 RoleCreate(Role),
109 RoleUpdate(Role),
111 RoleDelete(Role),
113 MemberAdd(Member),
115 MemberUpdate(Member),
117 MemberRemove(Member),
119 VoiceStateUpdate(VoiceState),
121 CommunityUpdate(Community),
123 CommunityDelete {
128 community: Community,
130 reason: RemovalReason,
132 },
133 CommunityMoving {
135 community: Community,
137 host: HostId,
139 },
140 CommunityMoved {
142 community: Community,
144 host: HostId,
146 },
147 CommunityMoveRefused {
149 community: Community,
151 code: String,
153 },
154 Disconnect(Community),
156 Offline,
158 Online,
160 Warning(String),
162 DmCreate {
165 channel: Channel,
167 recipient: Member,
169 },
170 DmDelete(Channel),
172 MessageSent {
176 pending: u64,
178 message: Message,
180 },
181 MessageSendFailed {
184 pending: u64,
186 channel: Channel,
188 error: String,
190 },
191 TypingStart {
195 channel: Channel,
197 typing: crate::TypingNow,
199 },
200 Raw {
202 community: Community,
204 channel: Option<Channel>,
206 actor: Option<Member>,
208 event: Box<Catalogue>,
210 },
211}
212
213#[derive(Debug, Clone)]
215pub struct Reaction {
216 pub message: PartialMessage,
218 pub emoji: String,
220 pub member: Member,
222}
223
224#[derive(Debug, Clone)]
226pub struct VoiceState {
227 pub member: Member,
229 pub channel: Channel,
231 pub joined: bool,
233 pub left: Option<VoiceLeave>,
235 pub mute: Option<VoiceMute>,
237}
238
239#[derive(Debug, Clone)]
241pub struct VoiceLeave {
242 pub reason: VoiceLeftReason,
244 pub moved_to: Option<Channel>,
246}
247
248#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
250#[allow(
251 clippy::struct_excessive_bools,
252 reason = "four independent flags, as the host sends them"
253)]
254pub struct VoiceMute {
255 pub self_mute: bool,
257 pub self_deaf: bool,
259 pub server_mute: bool,
261 pub server_deaf: bool,
263}
264
265impl Client {
266 pub(crate) fn wrap(&self, e: Inner) -> Event {
268 let community = |id| self.community(id);
269 match e {
270 Inner::Ready(c) => Event::Ready(community(c)),
271 Inner::Apps(c) => Event::AppsReady(community(c)),
272 Inner::Presences(c) => Event::PresencesReady(community(c)),
273 Inner::Message(m) => {
274 let channel = community(m.community).channel(m.channel);
275 Event::MessageCreate(Message {
276 channel,
277 id: m.id,
278 sequence: m.sequence,
279 author: m.author,
280 content: m.content,
281 embeds: m.embeds,
282 components: m.components,
283 flags: m.flags,
284 interaction: m.interaction,
285 reply_to: m.reply_to,
286 mentions: m.mentions,
287 attachments: m.attachments,
288 timestamp: m.timestamp,
289 })
290 }
291 Inner::Update {
292 community: c,
293 channel,
294 actor,
295 event,
296 } => {
297 let c = community(c);
298 let channel = channel.map(|ch| c.channel(ch));
299 let actor = actor.map(|a| c.member(a));
300 self.update(c, channel, actor, *event)
301 }
302 Inner::Removed {
303 community: c,
304 reason,
305 } => Event::CommunityDelete {
306 community: community(c),
307 reason,
308 },
309 Inner::Moving { community: c, host } => Event::CommunityMoving {
310 community: community(c),
311 host,
312 },
313 Inner::Moved { community: c, host } => Event::CommunityMoved {
314 community: community(c),
315 host,
316 },
317 Inner::MoveRefused { community: c, code } => Event::CommunityMoveRefused {
318 community: community(c),
319 code,
320 },
321 Inner::Disconnected(c) => Event::Disconnect(community(c)),
322 Inner::Offline(true) => Event::Offline,
323 Inner::Offline(false) => Event::Online,
324 Inner::Warning(w) => Event::Warning(w),
325 Inner::DmCreated { community: c, dm } => {
326 let community = community(c);
327 let me = community.me().map_or(dm.participants[0], |m| m.id());
328 Event::DmCreate {
329 channel: community.channel(dm.channel_id),
330 recipient: community.member(dm.other(me)),
331 }
332 }
333 Inner::DmClosed {
334 community: c,
335 channel,
336 } => Event::DmDelete(community(c).channel(channel)),
337 Inner::Sent {
338 pending,
339 message: m,
340 } => {
341 let channel = community(m.community).channel(m.channel);
342 Event::MessageSent {
343 pending,
344 message: Message {
345 channel,
346 id: m.id,
347 sequence: m.sequence,
348 author: m.author,
349 content: m.content,
350 embeds: m.embeds,
351 components: m.components,
352 flags: m.flags,
353 interaction: m.interaction,
354 reply_to: m.reply_to,
355 mentions: m.mentions,
356 attachments: m.attachments,
357 timestamp: m.timestamp,
358 },
359 }
360 }
361 Inner::SendFailed {
362 pending,
363 community: c,
364 channel,
365 error,
366 } => Event::MessageSendFailed {
367 pending,
368 channel: community(c).channel(channel),
369 error,
370 },
371 }
372 }
373
374 fn update(
375 &self,
376 community: Community,
377 channel: Option<Channel>,
378 actor: Option<Member>,
379 event: Catalogue,
380 ) -> Event {
381 let member = |id| community.member(id);
382 match (event, channel) {
383 (Catalogue::MessageUpdated(m), Some(channel)) => Event::MessageUpdate {
384 message: channel.message(m.message_id),
385 author: actor,
386 content: m.content,
387 embeds: m.embeds,
388 mentions: m.mentions,
389 edited_at: m.edited_at,
390 },
391 (Catalogue::MessageDeleted(m), Some(channel)) => Event::MessageDelete {
392 message: channel.message(m.message_id),
393 by: actor,
394 },
395 (Catalogue::MessagesBulkDeleted(m), Some(channel)) => Event::MessageDeleteBulk {
396 messages: m
397 .message_ids
398 .iter()
399 .map(|id| channel.message(*id))
400 .collect(),
401 channel,
402 },
403 (Catalogue::ReactionAdded(r), Some(channel)) => Event::ReactionAdd(Reaction {
404 member: member(r.member_id),
405 message: channel.message(r.message_id),
406 emoji: r.emoji,
407 }),
408 (Catalogue::ReactionRemoved(r), Some(channel)) => Event::ReactionRemove(Reaction {
409 member: member(r.member_id),
410 message: channel.message(r.message_id),
411 emoji: r.emoji,
412 }),
413 (Catalogue::CommunityUpdated(_), _) => Event::CommunityUpdate(community),
414 (Catalogue::ChannelCreated(c), _) => {
415 Event::ChannelCreate(community.channel(c.channel.channel_id))
416 }
417 (Catalogue::ChannelUpdated(c), _) => {
418 Event::ChannelUpdate(community.channel(c.channel_id))
419 }
420 (Catalogue::ChannelDeleted(c), _) => {
421 Event::ChannelDelete(community.channel(c.channel_id))
422 }
423 (Catalogue::RoleCreated(r), _) => Event::RoleCreate(community.role(r.role_id)),
424 (Catalogue::RoleUpdated(r), _) => Event::RoleUpdate(community.role(r.role_id)),
425 (Catalogue::RoleDeleted(r), _) => Event::RoleDelete(community.role(r.role_id)),
426 (Catalogue::MemberJoined(m), _) => Event::MemberAdd(member(m.member_id)),
427 (Catalogue::MemberUpdated(m), _) => Event::MemberUpdate(member(m.member_id)),
428 (Catalogue::MemberRolesUpdated(m), _) => Event::MemberUpdate(member(m.member_id)),
429 (Catalogue::MemberTimedOut(m), _) => Event::MemberUpdate(member(m.member_id)),
430 (Catalogue::MemberLeft(m), _) => Event::MemberRemove(member(m.member_id)),
431 (Catalogue::CommandUpdated(u), _) => Event::CommandsUpdate {
432 community,
433 bot: u.bot_id,
434 },
435 (Catalogue::BotUpdated(card), _) => Event::BotUpdate { community, card },
436 (Catalogue::BotRemoved(r), _) => Event::BotRemove {
437 member: member(r.member_id),
438 community,
439 bot: r.bot_id,
440 },
441 (Catalogue::InteractionCreated(data), _) => {
442 Event::InteractionCreate(Box::new(Interaction { community, data }))
443 }
444 (Catalogue::InteractionMessage(m), channel) => {
445 Event::InteractionReply(EphemeralReply {
446 channel,
447 interaction: m.interaction_id,
448 op: m.op,
449 message: m.message,
450 })
451 }
452 (Catalogue::InteractionModal(m), channel) => Event::ModalOpen(ModalOpen {
453 channel,
454 interaction: m.interaction_id,
455 custom_id: m.custom_id,
456 title: m.title,
457 components: m.components,
458 }),
459 (Catalogue::InteractionAutocomplete(a), channel) => Event::Autocomplete(Suggestions {
460 community,
461 channel,
462 interaction: a.interaction_id,
463 choices: a.choices,
464 }),
465 (Catalogue::InteractionFailed(f), channel) => {
466 Event::InteractionFail(InteractionFailure {
467 channel,
468 interaction: f.interaction_id,
469 reason: f.reason,
470 })
471 }
472 (Catalogue::VoiceJoined(v), _) => voice(&community, v.channel_id, &v.participant, true),
473 (Catalogue::VoiceUpdated(v), _) => {
474 voice(&community, v.channel_id, &v.participant, false)
475 }
476 (Catalogue::VoiceLeft(v), _) => Event::VoiceStateUpdate(VoiceState {
477 member: member(v.member_id),
478 channel: community.channel(v.channel_id),
479 joined: false,
480 left: Some(VoiceLeave {
481 reason: v.reason,
482 moved_to: v.moved_to.map(|c| community.channel(c)),
483 }),
484 mute: None,
485 }),
486 (Catalogue::SignalPresence(p), _) => Event::PresenceUpdate {
487 community,
488 presence: crate::Presence::from(&p),
489 },
490 (Catalogue::SignalTyping(t), _) => match crate::TypingNow::from_signal(&t) {
491 Some(typing) => Event::TypingStart {
492 channel: community.channel(t.channel_id),
493 typing,
494 },
495 None => Event::Warning("an empty typing signal".to_owned()),
496 },
497 (other, channel) => Event::Raw {
498 community,
499 channel,
500 actor,
501 event: Box::new(other),
502 },
503 }
504 }
505}
506
507fn voice(
508 community: &Community,
509 channel: zaun_community_protocol::ids::ChannelId,
510 p: &zaun_community_protocol::command::object::VoiceParticipant,
511 joined: bool,
512) -> Event {
513 Event::VoiceStateUpdate(VoiceState {
514 member: community.member(p.member_id),
515 channel: community.channel(channel),
516 joined,
517 left: None,
518 mute: Some(VoiceMute {
519 self_mute: p.self_mute,
520 self_deaf: p.self_deaf,
521 server_mute: p.server_mute,
522 server_deaf: p.server_deaf,
523 }),
524 })
525}