Update readme and add simple registration trait

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 4 years ago
parent 7eb362e8bc
commit e6439a82ee
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

2
Cargo.lock generated

@ -1033,7 +1033,7 @@ dependencies = [
[[package]]
name = "serenity-rich-interaction"
version = "0.2.1"
version = "0.2.2"
dependencies = [
"futures",
"log",

@ -1,11 +1,12 @@
[package]
name = "serenity-rich-interaction"
version = "0.2.1"
version = "0.2.2"
authors = ["trivernis <trivernis@protonmail.com>"]
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

@ -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

@ -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::<EventDrivenMessageContainer>(Arc::new(Mutex::new(HashMap::new())))
.raw_event_handler(rich_handler)
}
}

@ -33,13 +33,14 @@ impl<T> EventCallback<T> {
/// 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 {

@ -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::*;

@ -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();

@ -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<F: 'static>(builder_fn: F) -> Self
where
F: Fn() -> MessageBuildOutput<'b> + Send + Sync,

Loading…
Cancel
Save