1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use crate::core::MessageHandle;
use crate::error::{Error, Result};
use crate::menu::controls::{close_menu, next_page, previous_page, toggle_help};
use crate::menu::traits::EventDrivenMessage;
use crate::menu::typedata::HelpActiveContainer;
use crate::menu::{get_listeners_from_context, EventDrivenMessagesRef, Page};
use futures::FutureExt;
use serenity::async_trait;
use serenity::client::Context;
use serenity::http::Http;
use serenity::model::channel::{Message, Reaction, ReactionType};
use serenity::model::id::{ChannelId, UserId};
use serenity::prelude::{TypeMap, TypeMapKey};
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{Mutex, RwLock};

pub static NEXT_PAGE_EMOJI: &str = "➡️";
pub static PREVIOUS_PAGE_EMOJI: &str = "⬅️";
pub static CLOSE_MENU_EMOJI: &str = "❌";
pub static HELP_EMOJI: &str = "❔";

pub type ControlActionResult<'b> = Pin<Box<dyn Future<Output = Result<()>> + Send + 'b>>;

pub type ControlActionArc = Arc<
    dyn for<'b> Fn(&'b Context, &'b mut Menu<'_>, Reaction) -> ControlActionResult<'b>
        + Send
        + Sync,
>;

#[derive(Clone)]
pub struct ActionContainer {
    inner: ControlActionArc,
    position: isize,
}

impl ActionContainer {
    /// Creates a new control action
    pub fn new<F: 'static>(position: isize, callback: F) -> Self
    where
        F: for<'b> Fn(&'b Context, &'b mut Menu<'_>, Reaction) -> ControlActionResult<'b>
            + Send
            + Sync,
    {
        Self {
            inner: Arc::new(callback),
            position,
        }
    }

    /// Runs the action
    pub async fn run(&self, ctx: &Context, menu: &mut Menu<'_>, reaction: Reaction) -> Result<()> {
        self.inner.clone()(ctx, menu, reaction).await?;
        Ok(())
    }

    /// Returns the position of the action
    pub fn position(&self) -> isize {
        self.position
    }
}

/// A menu message
pub struct Menu<'a> {
    pub message: Arc<RwLock<MessageHandle>>,
    pub pages: Vec<Page<'a>>,
    pub current_page: usize,
    pub(crate) controls: HashMap<String, ActionContainer>,
    pub timeout: Instant,
    pub sticky: bool,
    pub data: TypeMap,
    pub(crate) help_entries: HashMap<String, String>,
    owner: Option<UserId>,
    closed: bool,
    listeners: EventDrivenMessagesRef,
}

impl<'a> Menu<'a> {
    /// Returns the current page of the menu
    pub fn get_current_page(&self) -> Result<&Page<'a>> {
        self.pages
            .get(self.current_page)
            .ok_or(Error::PageNotFound(self.current_page))
    }

    /// Removes all reactions from the menu
    #[tracing::instrument(level = "debug", skip_all)]
    pub(crate) async fn close(&mut self, http: &Http) -> Result<()> {
        let handle = self.message.read().await;
        http.delete_message_reactions(handle.channel_id, handle.message_id)
            .await?;
        self.closed = true;
        Ok(())
    }

    /// Returns the message of the menu
    pub async fn get_message(&self, http: &Http) -> Result<Message> {
        let handle = self.message.read().await;
        let msg = http
            .get_message(handle.channel_id, handle.message_id)
            .await?;

        Ok(msg)
    }

    /// Recreates the message completely
    #[tracing::instrument(level = "debug", skip_all)]
    pub async fn recreate(&self, http: &Http) -> Result<()> {
        let old_handle = {
            let handle = self.message.read().await;
            (*handle).clone()
        };
        let current_page = self.get_current_page()?.get().await?;

        let message = http
            .send_message(
                old_handle.channel_id,
                &serde_json::to_value(current_page.0).unwrap(),
            )
            .await?;

        for control in &self.controls {
            http.create_reaction(
                message.channel_id.0,
                message.id.0,
                &ReactionType::Unicode(control.0.clone()),
            )
            .await?;
        }

        let new_handle = {
            let mut handle = self.message.write().await;
            handle.message_id = message.id.0;
            (*handle).clone()
        };
        {
            tracing::debug!("Changing key of message");
            let menu = self.listeners.remove(&old_handle).unwrap();
            self.listeners.insert(new_handle, menu.1);
        }
        tracing::debug!("Deleting original message");
        http.delete_message(old_handle.channel_id, old_handle.message_id)
            .await?;

        Ok(())
    }
}

#[async_trait]
impl<'a> EventDrivenMessage for Menu<'a> {
    fn is_frozen(&self) -> bool {
        self.closed
    }

    #[tracing::instrument(level = "debug", skip_all)]
    async fn update(&mut self, http: &Http) -> Result<()> {
        tracing::trace!("Checking for menu timeout");

        if Instant::now() >= self.timeout {
            tracing::debug!("Menu timout reached. Closing menu.");
            self.close(http).await?;
        } else if self.sticky {
            tracing::debug!("Message is sticky. Checking for new messages in channel...");

            let handle = {
                let handle = self.message.read().await;
                (*handle).clone()
            };
            let channel_id = ChannelId(handle.channel_id);
            let messages = channel_id
                .messages(http, |p| p.after(handle.message_id).limit(1))
                .await?;
            if messages.len() > 0 {
                tracing::debug!("New messages in channel. Recreating...");
                self.recreate(http).await?;
            }
        }

        Ok(())
    }

    #[tracing::instrument(level = "debug", skip_all)]
    async fn on_reaction_add(&mut self, ctx: &Context, reaction: Reaction) -> Result<()> {
        let current_user = ctx.http.get_current_user().await?;
        let reaction_user_id = reaction.user_id.ok_or_else(|| Error::NoCache)?;

        if reaction_user_id.0 == current_user.id.0 {
            tracing::debug!("Reaction is from current user.");
            return Ok(());
        }
        let emoji_string = reaction.emoji.as_data();

        tracing::debug!("Deleting user reaction.");
        reaction.delete(ctx).await?;

        if let Some(owner) = self.owner {
            if owner != reaction_user_id {
                tracing::debug!(
                    "Menu has an owner and the reaction is not from the owner of the menu"
                );
                return Ok(());
            }
        }
        if let Some(control) = self.controls.get(&emoji_string).cloned() {
            tracing::debug!("Running control");
            control.run(ctx, self, reaction).await?;
        }

        Ok(())
    }
}

/// A builder for messages
pub struct MenuBuilder {
    pages: Vec<Page<'static>>,
    current_page: usize,
    controls: HashMap<String, ActionContainer>,
    timeout: Duration,
    sticky: bool,
    data: TypeMap,
    help_entries: HashMap<String, String>,
    owner: Option<UserId>,
}

impl Default for MenuBuilder {
    fn default() -> Self {
        Self {
            pages: vec![],
            current_page: 0,
            controls: HashMap::new(),
            timeout: Duration::from_secs(60),
            sticky: false,
            data: TypeMap::new(),
            help_entries: HashMap::new(),
            owner: None,
        }
    }
}

impl MenuBuilder {
    /// Creates a new pagination menu
    #[tracing::instrument(level = "debug", skip_all)]
    pub fn new_paginator() -> Self {
        let mut controls = HashMap::new();
        let mut help_entries = HashMap::new();
        controls.insert(
            PREVIOUS_PAGE_EMOJI.to_string(),
            ActionContainer::new(0, |c, m, r| previous_page(c, m, r).boxed()),
        );
        help_entries.insert(
            PREVIOUS_PAGE_EMOJI.to_string(),
            "Displays the previous page".to_string(),
        );
        controls.insert(
            CLOSE_MENU_EMOJI.to_string(),
            ActionContainer::new(1, |c, m, r| close_menu(c, m, r).boxed()),
        );
        help_entries.insert(
            CLOSE_MENU_EMOJI.to_string(),
            "Closes the menu buttons".to_string(),
        );
        controls.insert(
            NEXT_PAGE_EMOJI.to_string(),
            ActionContainer::new(2, |c, m, r| next_page(c, m, r).boxed()),
        );
        help_entries.insert(
            NEXT_PAGE_EMOJI.to_string(),
            "Displays the next page".to_string(),
        );

        Self {
            controls,
            help_entries,
            ..Default::default()
        }
    }

    /// Adds a page to the message builder
    pub fn add_page(mut self, page: Page<'static>) -> Self {
        self.pages.push(page);

        self
    }

    /// Adds multiple pages to the message
    pub fn add_pages<I>(mut self, pages: I) -> Self
    where
        I: IntoIterator<Item = Page<'static>>,
    {
        let mut pages = pages.into_iter().collect();
        self.pages.append(&mut pages);

        self
    }

    /// Adds a single control to the message
    pub fn add_control<S, F: 'static>(mut self, position: isize, emoji: S, action: F) -> Self
    where
        S: ToString,
        F: for<'b> Fn(&'b Context, &'b mut Menu<'_>, Reaction) -> ControlActionResult<'b>
            + Send
            + Sync,
    {
        self.controls
            .insert(emoji.to_string(), ActionContainer::new(position, action));

        self
    }

    /// Adds a single control to the message
    pub fn add_controls<S, I>(mut self, controls: I) -> Self
    where
        S: ToString,
        I: IntoIterator<Item = (isize, S, ControlActionArc)>,
    {
        for (position, emoji, action) in controls {
            self.controls.insert(
                emoji.to_string(),
                ActionContainer {
                    position,
                    inner: action,
                },
            );
        }

        self
    }

    /// Sets the timeout for the message
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;

        self
    }

    /// Sets the start page of the message
    pub fn start_page(mut self, page: usize) -> Self {
        self.current_page = page;

        self
    }

    /// If the message should be sticky and always be
    /// the last one in the channel
    pub fn sticky(mut self, value: bool) -> Self {
        self.sticky = value;

        self
    }

    /// Adds data to the menu typemap
    pub fn add_data<T>(mut self, value: T::Value) -> Self
    where
        T: TypeMapKey,
    {
        self.data.insert::<T>(value);

        self
    }

    /// Adds a help entry
    pub fn add_help<S: ToString>(mut self, button: S, help: S) -> Self {
        self.help_entries
            .insert(button.to_string(), help.to_string());

        self
    }

    /// Turns showing help for buttons on
    pub fn show_help(self) -> Self {
        self.add_control(100, HELP_EMOJI, |c, m, r| Box::pin(toggle_help(c, m, r)))
            .add_data::<HelpActiveContainer>(Arc::new(AtomicBool::new(false)))
    }

    /// Sets the owner of the menu
    /// if it's set only the owner can interact with the menu
    pub fn owner(mut self, user_id: UserId) -> Self {
        self.owner = Some(user_id);

        self
    }

    /// builds the menu
    #[tracing::instrument(level = "debug", skip_all)]
    pub async fn build(
        self,
        ctx: &Context,
        channel_id: ChannelId,
    ) -> Result<Arc<RwLock<MessageHandle>>> {
        let mut current_page = self
            .pages
            .get(self.current_page)
            .ok_or(Error::PageNotFound(self.current_page))?
            .clone()
            .get()
            .await?;

        let message = channel_id.send_message(ctx, |_| &mut current_page).await?;

        tracing::debug!("Sorting controls...");
        let mut controls = self
            .controls
            .clone()
            .into_iter()
            .collect::<Vec<(String, ActionContainer)>>();
        controls.sort_by_key(|(_, a)| a.position);

        tracing::debug!("Creating menu...");
        let message_handle = MessageHandle::new(message.channel_id, message.id);
        let handle_lock = Arc::new(RwLock::new(message_handle));
        let listeners = get_listeners_from_context(ctx).await?;

        let menu = Menu {
            message: Arc::clone(&handle_lock),
            pages: self.pages,
            current_page: self.current_page,
            controls: self.controls,
            timeout: Instant::now() + self.timeout,
            closed: false,
            listeners: Arc::clone(&listeners),
            sticky: self.sticky,
            data: self.data,
            help_entries: self.help_entries,
            owner: self.owner,
        };

        tracing::debug!("Storing menu to listeners...");
        listeners.insert(message_handle, Arc::new(Mutex::new(Box::new(menu).into())));

        tracing::debug!("Adding controls...");
        for (emoji, _) in controls {
            message
                .react(ctx, ReactionType::Unicode(emoji.clone()))
                .await?;
        }

        Ok(handle_lock)
    }
}