Skip to content

Commit

Permalink
Swap to using serenity's data field
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Jan 19, 2024
1 parent cbfbc93 commit 53a4082
Show file tree
Hide file tree
Showing 28 changed files with 116 additions and 124 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions examples/advanced_cooldowns/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use poise::serenity_prelude as serenity;

struct Data {} // User data, which is stored and accessible in all command invocations
type Data = (); // User data, which is stored and accessible in all command invocations
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;

Expand Down Expand Up @@ -41,7 +41,7 @@ async fn main() {
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
Ok(())
})
})
.build();
Expand Down
7 changes: 4 additions & 3 deletions examples/basic_structure/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ async fn main() {
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 @@ -112,6 +110,9 @@ async fn main() {
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;

let client = serenity::ClientBuilder::new(&token, intents)
.data(Arc::new(Data {
votes: Mutex::new(HashMap::new()),
}) as _)
.framework(framework)
.await;

Expand Down
13 changes: 5 additions & 8 deletions examples/event_handler/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env::var;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;

use poise::serenity_prelude as serenity;

Expand All @@ -23,20 +24,16 @@ async fn main() {
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: |framework, event| Box::pin(event_handler(framework, event)),
..Default::default()
})
.build();

let client = serenity::ClientBuilder::new(&token, intents)
.data(Arc::new(Data {
poise_mentions: AtomicU32::new(0),
}) as _)
.framework(framework)
.await;

Expand All @@ -47,7 +44,7 @@ async fn event_handler(
framework: poise::FrameworkContext<'_, Data, Error>,
event: &serenity::FullEvent,
) -> Result<(), Error> {
let data = framework.user_data;
let data = framework.user_data();
let ctx = framework.serenity_context;

match event {
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 @@ -24,7 +24,7 @@ use poise::serenity_prelude as serenity;
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
// User data, which is stored and accessible in all command invocations
pub struct Data {}
pub type Data = ();

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -105,7 +105,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 Down
4 changes: 3 additions & 1 deletion examples/fluent_localization/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod translation;

use std::sync::Arc;

use poise::serenity_prelude as serenity;
use translation::tr;

Expand Down Expand Up @@ -72,10 +74,10 @@ async fn main() {
commands,
..Default::default()
})
.setup(move |_, _, _| Box::pin(async move { Ok(Data { translations }) }))
.build();

let client = serenity::ClientBuilder::new(&token, intents)
.data(Arc::new(Data { translations }) as _)
.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: Send + Sync + 'static, 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 @@ -2,7 +2,7 @@ use ::serenity::small_fixed_array::FixedString;
use poise::{samples::HelpConfiguration, serenity_prelude as serenity};
use rand::Rng;

struct Data {} // User data, which is stored and accessible in all command invocations
type Data = (); // User data, which is stored and accessible in all command invocations
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;

Expand Down Expand Up @@ -349,7 +349,7 @@ async fn main() {
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
Ok(())
})
})
.build();
Expand Down
1 change: 0 additions & 1 deletion examples/manual_dispatch/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl serenity::EventHandler for Handler {
let framework_data = poise::FrameworkContext {
serenity_context: &ctx,
options: &self.options,
user_data: &(),
shard_manager: &shard_manager,
};

Expand Down
5 changes: 3 additions & 2 deletions examples/quickstart/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use poise::serenity_prelude as serenity;

struct Data {} // User data, which is stored and accessible in all command invocations
type Data = (); // User data, which is stored and accessible in all command invocations
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;

Expand Down Expand Up @@ -29,13 +29,14 @@ 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)
.framework(framework)
.await;

client.unwrap().start().await.unwrap();
}
12 changes: 7 additions & 5 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 Down Expand Up @@ -120,7 +122,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 @@ -289,7 +291,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 +350,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 +416,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
14 changes: 9 additions & 5 deletions src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ use crate::{serenity_prelude as serenity, CreateReply};
/// }
/// # };
/// ```
pub async fn on_error<U, E: std::fmt::Display + std::fmt::Debug>(
error: crate::FrameworkError<'_, U, E>,
) -> Result<(), serenity::Error> {
pub async fn on_error<U, E>(error: crate::FrameworkError<'_, U, E>) -> Result<(), serenity::Error>
where
U: Send + Sync + 'static,
E: std::fmt::Display + std::fmt::Debug,
{
match error {
crate::FrameworkError::Setup { error, .. } => {
eprintln!("Error in user data setup: {}", error);
Expand Down Expand Up @@ -202,7 +204,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 @@ -227,7 +229,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
2 changes: 1 addition & 1 deletion src/builtins/paginate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::serenity_prelude as serenity;
/// ```
///
/// ![Screenshot of output](https://i.imgur.com/JGFDveA.png)
pub async fn paginate<U, E>(
pub async fn paginate<U: Send + Sync + 'static, E>(
ctx: crate::Context<'_, U, E>,
pages: &[&str],
) -> Result<(), serenity::Error> {
Expand Down
4 changes: 2 additions & 2 deletions src/builtins/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub async fn register_in_guild<U, E>(
///
/// Run with no arguments to register in guild, run with argument "global" to register globally.
/// ```
pub async fn register_application_commands<U, E>(
pub async fn register_application_commands<U: Send + Sync + 'static, E>(
ctx: crate::Context<'_, U, E>,
global: bool,
) -> Result<(), serenity::Error> {
Expand Down Expand Up @@ -148,7 +148,7 @@ pub async fn register_application_commands<U, E>(
/// ```
///
/// Which you can call like any prefix command, for example `@your_bot register`.
pub async fn register_application_commands_buttons<U, E>(
pub async fn register_application_commands_buttons<U: Send + Sync + 'static, E>(
ctx: crate::Context<'_, U, E>,
) -> Result<(), serenity::Error> {
let create_commands = create_application_commands(&ctx.framework().options().commands);
Expand Down
6 changes: 3 additions & 3 deletions src/dispatch/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn user_permissions(
/// Retrieves the set of permissions that are lacking, relative to the given required permission set
///
/// Returns None if permissions couldn't be retrieved
async fn missing_permissions<U, E>(
async fn missing_permissions<U: Send + Sync + 'static, E>(
ctx: crate::Context<'_, U, E>,
user: serenity::UserId,
required_permissions: serenity::Permissions,
Expand All @@ -58,7 +58,7 @@ async fn missing_permissions<U, E>(

/// See [`check_permissions_and_cooldown`]. Runs the check only for a single command. The caller
/// should call this multiple time for each parent command to achieve the check inheritance logic.
async fn check_permissions_and_cooldown_single<'a, U, E>(
async fn check_permissions_and_cooldown_single<'a, U: Send + Sync + 'static, E>(
ctx: crate::Context<'a, U, E>,
cmd: &'a crate::Command<U, E>,
) -> Result<(), crate::FrameworkError<'a, U, E>> {
Expand Down Expand Up @@ -178,7 +178,7 @@ async fn check_permissions_and_cooldown_single<'a, U, E>(
/// argument parsing.
/// (A command that didn't even get past argument parsing shouldn't trigger cooldowns)
#[allow(clippy::needless_lifetimes)] // false positive (clippy issue 7271)
pub async fn check_permissions_and_cooldown<'a, U, E>(
pub async fn check_permissions_and_cooldown<'a, U: Send + Sync + 'static, E>(
ctx: crate::Context<'a, U, E>,
) -> Result<(), crate::FrameworkError<'a, U, E>> {
for parent_command in ctx.parent_commands() {
Expand Down
Loading

0 comments on commit 53a4082

Please sign in to comment.