From e2cc2e1e802697d4c9cf2ba600f4afca8417e016 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 22 Apr 2021 12:55:19 +0200 Subject: [PATCH] Update readme and add simple registration trait Signed-off-by: trivernis --- Cargo.lock | 2 +- Cargo.toml | 3 +- README.md | 73 +++++++++++++++++++++++++++++++++++++++++++ src/core.rs | 23 ++++++++++++++ src/events/handler.rs | 3 +- src/lib.rs | 1 + src/menu/menu.rs | 2 +- src/menu/page.rs | 3 +- 8 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 README.md diff --git a/Cargo.lock b/Cargo.lock index a767e86..1315134 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1033,7 +1033,7 @@ dependencies = [ [[package]] name = "serenity-rich-interaction" -version = "0.2.1" +version = "0.2.2" dependencies = [ "futures", "log", diff --git a/Cargo.toml b/Cargo.toml index a46c2b8..5f5f4e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,12 @@ [package] name = "serenity-rich-interaction" -version = "0.2.1" +version = "0.2.2" authors = ["trivernis "] edition = "2018" description = "Menus and self deleting messages for the serenity discord framework" license = "Apache-2.0" repository = "https://github.com/Trivernis/serenity-rich-interactions" +readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..cb86dfc --- /dev/null +++ b/README.md @@ -0,0 +1,73 @@ +# Serenity Rich Interaction + +This crate provides some types for rich interactions with serenity such as Menus and Ephemeral (self deleting) Messages. + +## Usage + +You have to register the module in the serenity client builder. +```rust +use serenity::client::Client; +use serenity_rich_interaction::RegisterRichInteractions; + +#[tokio::main] +async fn get_client { + // stuff + let client = Client::builder("TOKEN").register_rich_interactions().await?; + // stuff +} +``` + +## Menu + +```rust +use serenity::builder::CreateMessage; +use serenity::client::Context; +use serenity::model::id::ChannelId; +use serenity_rich_interaction::menu::{MenuBuilder, Page}; +use std::time::Duration; +use serenity_rich_interaction::Result; + +pub async fn create_menu( + ctx: &Context, + channel_id: ChannelId, +) -> Result<()> { + let mut message1 = CreateMessage::default(); + message1.content("Hello"); + let mut message2 = CreateMessage::default(); + message2.content("World"); + + MenuBuilder::new_paginator() + .timeout(Duration::from_secs(120)) + .add_page(Page::new_static(message1)) + .add_page(Page::new_static(message2)) + .show_help() + .build(ctx, channel_id) + .await?; + + Ok(()) +} +``` + +## Ephemeral Message + +```rust +use serenity_rich_interaction::core::SHORT_TIMEOUT; +use serenity_rich_interaction::ephemeral_message::EphemeralMessage; +use serenity_rich_interaction::Result; +use serenity::client::Context; +use serenity::model::id::ChannelId; + +pub async fn create_ephemeral_message(ctx: &Context, channel_id: ChannelId) -> Result<()> { + EphemeralMessage::create(&ctx.http, channel_id, SHORT_TIMEOUT, |m| { + m.content("Hello World") + }).await?; + + Ok(()) +} + +``` + + +## License + +MIT \ No newline at end of file diff --git a/src/core.rs b/src/core.rs index bd12ad1..e12ab55 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,10 +1,15 @@ use crate::error::Result; +use crate::events::RichEventHandler; use crate::menu::traits::EventDrivenMessage; +use crate::menu::EventDrivenMessageContainer; +use serenity::client::ClientBuilder; use serenity::http::Http; use serenity::model::channel::Message; use serenity::model::id::{ChannelId, MessageId}; +use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; +use tokio::sync::Mutex; pub static SHORT_TIMEOUT: Duration = Duration::from_secs(5); pub static MEDIUM_TIMEOUT: Duration = Duration::from_secs(20); @@ -42,3 +47,21 @@ impl MessageHandle { Ok(msg) } } + +pub trait RegisterRichInteractions { + fn register_rich_interactions(self) -> Self; + fn register_rich_interactions_with(self, rich_handler: RichEventHandler) -> Self; +} + +impl<'a> RegisterRichInteractions for ClientBuilder<'a> { + /// Registers the rich interactions configuration on the client + fn register_rich_interactions(self) -> Self { + self.register_rich_interactions_with(RichEventHandler::default()) + } + + /// Registers the rich interactions with a custom rich event handler + fn register_rich_interactions_with(self, rich_handler: RichEventHandler) -> Self { + self.type_map_insert::(Arc::new(Mutex::new(HashMap::new()))) + .raw_event_handler(rich_handler) + } +} diff --git a/src/events/handler.rs b/src/events/handler.rs index 996811d..00907be 100644 --- a/src/events/handler.rs +++ b/src/events/handler.rs @@ -33,13 +33,14 @@ impl EventCallback { /// use serenity_rich_interaction::events::RichEventHandler; /// use serenity::model::event; /// use serenity::client::Client; +/// use serenity_rich_interaction::RegisterRichInteractions; /// /// let mut handler = RichEventHandler::default(); /// handler.add_event(|ctx, e: &event::ReadyEvent| Box::pin(async move { /// println!("Ready event received"); /// Ok(()) /// })); -/// let client = Client::builder("TOKEN").raw_event_handler(handler).await?; +/// let client = Client::builder("TOKEN").register_rich_interactions_with(handler).await?; /// // ... /// ``` pub struct RichEventHandler { diff --git a/src/lib.rs b/src/lib.rs index 5f345fb..2fb84a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,4 +5,5 @@ pub mod events; pub mod menu; pub static VERSION: &str = env!("CARGO_PKG_VERSION"); +pub use crate::core::RegisterRichInteractions; pub use error::*; diff --git a/src/menu/menu.rs b/src/menu/menu.rs index 3147f15..b0373dd 100644 --- a/src/menu/menu.rs +++ b/src/menu/menu.rs @@ -239,7 +239,7 @@ impl Default for MenuBuilder { } impl MenuBuilder { - /// Creates a new paginaton menu + /// Creates a new pagination menu pub fn new_paginator() -> Self { log::debug!("Creating new paginator"); let mut controls = HashMap::new(); diff --git a/src/menu/page.rs b/src/menu/page.rs index e316449..c37b0a6 100644 --- a/src/menu/page.rs +++ b/src/menu/page.rs @@ -17,7 +17,8 @@ pub enum Page<'b> { } impl<'b> Page<'b> { - /// Creates a new page with the given builder function + /// Creates a new page with the given builder function that creates a page + /// each time it is accessed pub fn new_builder(builder_fn: F) -> Self where F: Fn() -> MessageBuildOutput<'b> + Send + Sync,