Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port poise to use serenity data generic #225

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ parking_lot = "0.12.1"
[dependencies.serenity]
default-features = false
features = ["builder", "client", "gateway", "model", "utils", "collector", "framework"]
version = "0.12.0"
git = "https://github.com/gnomeddev/serenity"
branch = "typemap-be-gone"

[dev-dependencies]
# For the examples
Expand Down
9 changes: 5 additions & 4 deletions examples/advanced_cooldowns/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ async fn main() {
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
Ok(())
})
})
.build();

let client = serenity::Client::builder(token, serenity::GatewayIntents::non_privileged())
.framework(framework)
.await;
let client =
serenity::Client::builder(token, serenity::GatewayIntents::non_privileged(), Data {})
.framework(framework)
.await;

client.unwrap().start().await.unwrap();
}
12 changes: 7 additions & 5 deletions examples/basic_structure/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async fn main() {
// Enforce command checks even for owners (enforced by default)
// Set to true to bypass checks, which is useful for testing
skip_checks_for_owners: false,
event_handler: |_ctx, event, _framework, _data| {
event_handler: |_ctx, event, _framework| {
Box::pin(async move {
println!(
"Got an event in event handler: {:?}",
Expand All @@ -93,14 +93,16 @@ async fn main() {
..Default::default()
};

let data = Data {
votes: Mutex::new(HashMap::new()),
};

let framework = poise::Framework::builder()
.setup(move |ctx, _ready, framework| {
Box::pin(async move {
println!("Logged in as {}", _ready.user.name);
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {
votes: Mutex::new(HashMap::new()),
})
Ok(())
})
})
.options(options)
Expand All @@ -111,7 +113,7 @@ async fn main() {
let intents =
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;

let client = serenity::ClientBuilder::new(token, intents)
let client = serenity::ClientBuilder::new(token, intents, data)
.framework(framework)
.await;

Expand Down
24 changes: 9 additions & 15 deletions examples/event_handler/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,42 @@ pub struct Data {
async fn main() {
env_logger::init();

let data = Data {
poise_mentions: AtomicU32::new(0),
};

let token = var("DISCORD_TOKEN")
.expect("Missing `DISCORD_TOKEN` env var, see README for more information.");
let intents =
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;

let framework = poise::Framework::builder()
.setup(move |_ctx, _ready, _framework| {
Box::pin(async move {
Ok(Data {
poise_mentions: AtomicU32::new(0),
})
})
})
.options(poise::FrameworkOptions {
event_handler: |ctx, event, framework, data| {
Box::pin(event_handler(ctx, event, framework, data))
},
event_handler: |ctx, event, framework| Box::pin(event_handler(ctx, event, framework)),
..Default::default()
})
.build();

let client = serenity::ClientBuilder::new(token, intents)
let client = serenity::ClientBuilder::new(token, intents, data)
.framework(framework)
.await;

client.unwrap().start().await.unwrap();
}

async fn event_handler(
ctx: &serenity::Context,
ctx: &serenity::Context<Data>,
event: &serenity::FullEvent,
_framework: poise::FrameworkContext<'_, Data, Error>,
data: &Data,
) -> Result<(), Error> {
match event {
serenity::FullEvent::Ready { data_about_bot, .. } => {
println!("Logged in as {}", data_about_bot.user.name);
}
serenity::FullEvent::Message { new_message } => {
if new_message.content.to_lowercase().contains("poise") {
let mentions = data.poise_mentions.load(Ordering::SeqCst) + 1;
data.poise_mentions.store(mentions, Ordering::SeqCst);
let mentions = ctx.data.poise_mentions.load(Ordering::SeqCst) + 1;
ctx.data.poise_mentions.store(mentions, Ordering::SeqCst);
new_message
.reply(ctx, format!("Poise has been mentioned {} times", mentions))
.await?;
Expand Down
4 changes: 2 additions & 2 deletions examples/feature_showcase/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async fn main() {
.setup(move |ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
Ok(())
})
})
.build();
Expand All @@ -103,7 +103,7 @@ async fn main() {
let intents =
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;

let client = serenity::ClientBuilder::new(token, intents)
let client = serenity::ClientBuilder::new(token, intents, Data {})
.framework(framework)
.await;

Expand Down
3 changes: 2 additions & 1 deletion examples/feature_showcase/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub async fn component_modal(ctx: crate::Context<'_>) -> Result<(), Error> {
.await
{
let data =
poise::execute_modal_on_component_interaction::<MyModal>(ctx, mci, None, None).await?;
poise::execute_modal_on_component_interaction::<MyModal, _>(ctx, mci, None, None)
.await?;
println!("Got data: {:?}", data);
}
Ok(())
Expand Down
3 changes: 1 addition & 2 deletions examples/fluent_localization/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ async fn main() {
commands,
..Default::default()
})
.setup(move |_, _, _| Box::pin(async move { Ok(Data { translations }) }))
.build();

let client = serenity::ClientBuilder::new(token, intents)
let client = serenity::ClientBuilder::new(token, intents, Data { translations })
.framework(framework)
.await;

Expand Down
2 changes: 1 addition & 1 deletion examples/generic_commands/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! The original use case for this feature was to have the same command in two different bots

#[poise::command(slash_command)]
pub async fn example<U: Sync, E>(ctx: poise::Context<'_, U, E>) -> Result<(), E> {
pub async fn example<U: Sync + Send, E>(ctx: poise::Context<'_, U, E>) -> Result<(), E> {
ctx.say(format!(
"My user data type is {} and the error type is {}",
std::any::type_name::<U>(),
Expand Down
4 changes: 2 additions & 2 deletions examples/help_generation/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,12 @@ async fn main() {
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
Ok(())
})
})
.build();

let client = serenity::ClientBuilder::new(token, intents)
let client = serenity::ClientBuilder::new(token, intents, Data {})
.framework(framework)
.await;

Expand Down
2 changes: 1 addition & 1 deletion examples/invocation_data/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async fn main() {
})
.build();

let client = serenity::ClientBuilder::new(token, intents)
let client = serenity::ClientBuilder::new(token, intents, ())
.framework(framework)
.await;

Expand Down
8 changes: 4 additions & 4 deletions examples/manual_dispatch/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use poise::serenity_prelude as serenity;

type Data = ();
type Error = serenity::Error;

#[poise::command(prefix_command)]
Expand All @@ -19,14 +20,13 @@ struct Handler {
shard_manager: std::sync::Mutex<Option<std::sync::Arc<serenity::ShardManager>>>,
}
#[serenity::async_trait]
impl serenity::EventHandler for Handler {
async fn message(&self, ctx: serenity::Context, new_message: serenity::Message) {
impl serenity::EventHandler<Data> for Handler {
async fn message(&self, ctx: serenity::Context<Data>, new_message: serenity::Message) {
// FrameworkContext contains all data that poise::Framework usually manages
let shard_manager = (*self.shard_manager.lock().unwrap()).clone().unwrap();
let framework_data = poise::FrameworkContext {
bot_id: serenity::UserId::new(846453852164587620),
options: &self.options,
user_data: &(),
shard_manager: &shard_manager,
};

Expand All @@ -51,7 +51,7 @@ async fn main() -> Result<(), Error> {
poise::set_qualified_names(&mut handler.options.commands); // some setup

let handler = std::sync::Arc::new(handler);
let mut client = serenity::Client::builder(token, intents)
let mut client = serenity::Client::builder(token, intents, ())
.event_handler_arc(handler.clone())
.await?;

Expand Down
4 changes: 2 additions & 2 deletions examples/quickstart/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ async fn main() {
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
Ok(())
})
})
.build();

let client = serenity::ClientBuilder::new(token, intents)
let client = serenity::ClientBuilder::new(token, intents, Data {})
.framework(framework)
.await;
client.unwrap().start().await.unwrap();
Expand Down
20 changes: 12 additions & 8 deletions src/builtins/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ impl TwoColumnList {
}

/// Get the prefix from options
async fn get_prefix_from_options<U, E>(ctx: crate::Context<'_, U, E>) -> Option<String> {
async fn get_prefix_from_options<U: Send + Sync + 'static, E>(
ctx: crate::Context<'_, U, E>,
) -> Option<String> {
let options = &ctx.framework().options().prefix_options;
match &options.prefix {
Some(fixed_prefix) => Some(fixed_prefix.clone()),
Expand All @@ -102,7 +104,9 @@ async fn get_prefix_from_options<U, E>(ctx: crate::Context<'_, U, E>) -> Option<
}

/// Format context menu command name
fn format_context_menu_name<U, E>(command: &crate::Command<U, E>) -> Option<String> {
fn format_context_menu_name<U: Send + Sync + 'static, E>(
command: &crate::Command<U, E>,
) -> Option<String> {
let kind = match command.context_menu_action {
Some(crate::ContextMenuCommandAction::User(_)) => "user",
Some(crate::ContextMenuCommandAction::Message(_)) => "message",
Expand All @@ -120,7 +124,7 @@ fn format_context_menu_name<U, E>(command: &crate::Command<U, E>) -> Option<Stri
}

/// Code for printing help of a specific command (e.g. `~help my_command`)
async fn help_single_command<U, E>(
async fn help_single_command<U: Send + Sync + 'static, E>(
ctx: crate::Context<'_, U, E>,
command_name: &str,
config: HelpConfiguration<'_>,
Expand Down Expand Up @@ -234,7 +238,7 @@ async fn help_single_command<U, E>(
}

/// Recursively formats all subcommands
fn preformat_subcommands<U, E>(
fn preformat_subcommands<U: Send + Sync + 'static, E>(
commands: &mut TwoColumnList,
command: &crate::Command<U, E>,
prefix: &str,
Expand All @@ -259,7 +263,7 @@ fn preformat_subcommands<U, E>(
}

/// Preformat lines (except for padding,) like `(" /ping", "Emits a ping message")`
fn preformat_command<U, E>(
fn preformat_command<U: Send + Sync + 'static, E>(
commands: &mut TwoColumnList,
config: &HelpConfiguration<'_>,
command: &crate::Command<U, E>,
Expand Down Expand Up @@ -289,7 +293,7 @@ fn preformat_command<U, E>(
/// Create help text for `help_all_commands`
///
/// This is a separate function so we can have tests for it
async fn generate_all_commands<U, E>(
async fn generate_all_commands<U: Send + Sync + 'static, E>(
ctx: crate::Context<'_, U, E>,
config: &HelpConfiguration<'_>,
) -> Result<String, serenity::Error> {
Expand Down Expand Up @@ -348,7 +352,7 @@ async fn generate_all_commands<U, E>(
}

/// Code for printing an overview of all commands (e.g. `~help`)
async fn help_all_commands<U, E>(
async fn help_all_commands<U: Send + Sync + 'static, E>(
ctx: crate::Context<'_, U, E>,
config: HelpConfiguration<'_>,
) -> Result<(), serenity::Error> {
Expand Down Expand Up @@ -414,7 +418,7 @@ async fn help_all_commands<U, E>(
/// Type ?help command for more info on a command.
/// You can edit your message to the bot and the bot will edit its response.
/// ```
pub async fn help<U, E>(
pub async fn help<U: Send + Sync + 'static, E>(
ctx: crate::Context<'_, U, E>,
command: Option<&str>,
config: HelpConfiguration<'_>,
Expand Down
8 changes: 5 additions & 3 deletions src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{serenity_prelude as serenity, CreateReply};
/// }
/// # };
/// ```
pub async fn on_error<U, E: std::fmt::Display + std::fmt::Debug>(
pub async fn on_error<U: Send + Sync + 'static, E: std::fmt::Display + std::fmt::Debug>(
error: crate::FrameworkError<'_, U, E>,
) -> Result<(), serenity::Error> {
match error {
Expand Down Expand Up @@ -199,7 +199,7 @@ pub async fn on_error<U, E: std::fmt::Display + std::fmt::Debug>(
///
/// See `examples/feature_showcase` for an example
#[allow(clippy::unused_async)] // Required for the return type
pub async fn autocomplete_command<'a, U, E>(
pub async fn autocomplete_command<'a, U: Send + Sync + 'static, E>(
ctx: crate::Context<'a, U, E>,
partial: &'a str,
) -> impl Iterator<Item = String> + 'a {
Expand All @@ -224,7 +224,9 @@ pub async fn autocomplete_command<'a, U, E>(
/// > - **A public server** (7123 members)
/// > - [3 private servers with 456 members total]
#[cfg(feature = "cache")]
pub async fn servers<U, E>(ctx: crate::Context<'_, U, E>) -> Result<(), serenity::Error> {
pub async fn servers<U: Send + Sync + 'static, E>(
ctx: crate::Context<'_, U, E>,
) -> Result<(), serenity::Error> {
use std::fmt::Write as _;

let show_private_guilds = ctx.framework().options().owners.contains(&ctx.author().id);
Expand Down
Loading
Loading