serenity_rich_interaction/
core.rs1use crate::error::Result;
2use crate::events::RichEventHandler;
3use crate::menu::traits::EventDrivenMessage;
4use crate::menu::EventDrivenMessageContainer;
5use dashmap::DashMap;
6use serenity::client::ClientBuilder;
7use serenity::http::Http;
8use serenity::model::channel::Message;
9use serenity::model::id::{ChannelId, MessageId};
10use std::ops::{Deref, DerefMut};
11use std::sync::Arc;
12use std::time::Duration;
13
14pub static SHORT_TIMEOUT: Duration = Duration::from_secs(5);
15pub static MEDIUM_TIMEOUT: Duration = Duration::from_secs(20);
16pub static LONG_TIMEOUT: Duration = Duration::from_secs(60);
17pub static EXTRA_LONG_TIMEOUT: Duration = Duration::from_secs(600);
18
19pub type BoxedEventDrivenMessage = Box<dyn EventDrivenMessage>;
20
21pub struct BoxedMessage(pub BoxedEventDrivenMessage);
22
23impl<T: EventDrivenMessage + 'static> From<Box<T>> for BoxedMessage {
24 fn from(m: Box<T>) -> Self {
25 Self(m as Box<dyn EventDrivenMessage>)
26 }
27}
28
29impl Deref for BoxedMessage {
30 type Target = BoxedEventDrivenMessage;
31
32 fn deref(&self) -> &Self::Target {
33 &self.0
34 }
35}
36
37impl DerefMut for BoxedMessage {
38 fn deref_mut(&mut self) -> &mut Self::Target {
39 &mut self.0
40 }
41}
42
43#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Eq, Hash)]
44pub struct MessageHandle {
45 pub channel_id: u64,
46 pub message_id: u64,
47}
48
49impl MessageHandle {
50 pub fn new(channel_id: ChannelId, message_id: MessageId) -> Self {
52 Self {
53 message_id: message_id.0,
54 channel_id: channel_id.0,
55 }
56 }
57
58 pub fn from_raw_ids(channel_id: u64, message_id: u64) -> Self {
60 Self {
61 message_id,
62 channel_id,
63 }
64 }
65
66 pub async fn get_message(&self, http: &Arc<Http>) -> Result<Message> {
68 let msg = http.get_message(self.channel_id, self.message_id).await?;
69 Ok(msg)
70 }
71}
72
73pub trait RegisterRichInteractions {
74 fn register_rich_interactions(self) -> Self;
75 fn register_rich_interactions_with(self, rich_handler: RichEventHandler) -> Self;
76}
77
78impl<'a> RegisterRichInteractions for ClientBuilder<'a> {
79 fn register_rich_interactions(self) -> Self {
81 self.register_rich_interactions_with(RichEventHandler::default())
82 }
83
84 fn register_rich_interactions_with(self, rich_handler: RichEventHandler) -> Self {
86 self.type_map_insert::<EventDrivenMessageContainer>(Arc::new(DashMap::new()))
87 .raw_event_handler(rich_handler)
88 }
89}