Skip to content

Commit

Permalink
Remove somewhat useless setup function
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Jan 21, 2024
1 parent aa3b5cf commit 3e26ef8
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 344 deletions.
34 changes: 18 additions & 16 deletions examples/advanced_cooldowns/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,29 @@ async fn dynamic_cooldowns(ctx: Context<'_>) -> Result<(), Error> {
Ok(())
}

#[poise::command(prefix_command, owners_only)]
async fn register_commands(ctx: Context<'_>) -> Result<(), Error> {
let commands = &ctx.framework().options().commands;
poise::builtins::register_globally(ctx, commands).await?;

ctx.say("Successfully registered slash commands!").await?;
Ok(())
}

#[tokio::main]
async fn main() {
let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![dynamic_cooldowns()],
// This is important! Or else, the command will be marked as invoked before our custom
// cooldown code has run - even if the command ends up not running!
manual_cooldowns: true,
..Default::default()
})
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(())
})
})
.build();

let options = poise::FrameworkOptions {
commands: vec![register_commands(), dynamic_cooldowns()],
// This is important! Or else, the command will be marked as invoked before our custom
// cooldown code has run - even if the command ends up not running!
manual_cooldowns: true,
..Default::default()
};

let client = serenity::Client::builder(&token, serenity::GatewayIntents::non_privileged())
.framework(framework)
.framework(poise::Framework::new(options))
.await;

client.unwrap().start().await.unwrap();
Expand Down
30 changes: 16 additions & 14 deletions examples/basic_structure/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
// They are many errors that can occur, so we only handle the ones we want to customize
// and forward the rest to the default handler
match error {
poise::FrameworkError::Setup { error, .. } => panic!("Failed to start bot: {:?}", error),
poise::FrameworkError::Command { error, ctx, .. } => {
println!("Error in command `{}`: {:?}", ctx.command().name, error,);
}
Expand All @@ -36,14 +35,28 @@ async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
}
}

#[poise::command(prefix_command, owners_only)]
async fn register_commands(ctx: Context<'_>) -> Result<(), Error> {
let commands = &ctx.framework().options().commands;
poise::builtins::register_globally(ctx, commands).await?;

ctx.say("Successfully registered slash commands!").await?;
Ok(())
}

#[tokio::main]
async fn main() {
env_logger::init();

// FrameworkOptions contains all of poise's configuration option in one struct
// Every option can be omitted to use its default value
let options = poise::FrameworkOptions {
commands: vec![commands::help(), commands::vote(), commands::getvotes()],
commands: vec![
register_commands(),
commands::help(),
commands::vote(),
commands::getvotes(),
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("~".into()),
edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan(
Expand Down Expand Up @@ -93,27 +106,16 @@ async fn main() {
..Default::default()
};

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(())
})
})
.options(options)
.build();

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 client = serenity::ClientBuilder::new(&token, intents)
.framework(poise::Framework::new(options))
.data(Arc::new(Data {
votes: Mutex::new(HashMap::new()),
}) as _)
.framework(framework)
.await;

client.unwrap().start().await.unwrap()
Expand Down
12 changes: 5 additions & 7 deletions examples/event_handler/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ async fn main() {
let intents =
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;

let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
event_handler: |framework, event| Box::pin(event_handler(framework, event)),
..Default::default()
})
.build();
let options = poise::FrameworkOptions {
event_handler: |framework, event| Box::pin(event_handler(framework, event)),
..Default::default()
};

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

client.unwrap().start().await.unwrap();
Expand Down
152 changes: 77 additions & 75 deletions examples/feature_showcase/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,96 +26,98 @@ type Context<'a> = poise::Context<'a, Data, Error>;
// User data, which is stored and accessible in all command invocations
pub type Data = ();

#[poise::command(prefix_command, owners_only)]
async fn register_commands(ctx: Context<'_>) -> Result<(), Error> {
let commands = &ctx.framework().options().commands;
poise::builtins::register_globally(ctx, commands).await?;

ctx.say("Successfully registered slash commands!").await?;
Ok(())
}

#[tokio::main]
async fn main() {
env_logger::init();

let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![
attachment_parameter::file_details(),
attachment_parameter::totalsize(),
autocomplete::greet(),
bool_parameter::oracle(),
#[cfg(feature = "cache")]
builtins::servers(),
builtins::help(),
checks::shutdown(),
checks::modonly(),
checks::delete(),
checks::ferrisparty(),
checks::cooldowns(),
checks::minmax(),
checks::get_guild_name(),
checks::only_in_dms(),
checks::lennyface(),
checks::permissions_v2(),
choice_parameter::choice(),
choice_parameter::inline_choice(),
choice_parameter::inline_choice_int(),
code_block_parameter::code(),
collector::boop(),
context_menu::user_info(),
context_menu::echo(),
inherit_checks::parent_checks(),
localization::welcome(),
modal::modal(),
modal::component_modal(),
paginate::paginate(),
panic_handler::div(),
parameter_attributes::addmultiple(),
parameter_attributes::voiceinfo(),
parameter_attributes::say(),
parameter_attributes::punish(),
parameter_attributes::stringlen(),
raw_identifiers::r#move(),
response_with_reply::reply(),
subcommands::parent(),
subcommand_required::parent_subcommand_required(),
track_edits::test_reuse_response(),
track_edits::add(),
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("~".into()),
non_command_message: Some(|_, msg| {
Box::pin(async move {
println!("non command message!: {}", msg.content);
Ok(())
})
}),
..Default::default()
},
on_error: |error| {
let framework_options = poise::FrameworkOptions {
commands: vec![
register_commands(),
attachment_parameter::file_details(),
attachment_parameter::totalsize(),
autocomplete::greet(),
bool_parameter::oracle(),
#[cfg(feature = "cache")]
builtins::servers(),
builtins::help(),
checks::shutdown(),
checks::modonly(),
checks::delete(),
checks::ferrisparty(),
checks::cooldowns(),
checks::minmax(),
checks::get_guild_name(),
checks::only_in_dms(),
checks::lennyface(),
checks::permissions_v2(),
choice_parameter::choice(),
choice_parameter::inline_choice(),
choice_parameter::inline_choice_int(),
code_block_parameter::code(),
collector::boop(),
context_menu::user_info(),
context_menu::echo(),
inherit_checks::parent_checks(),
localization::welcome(),
modal::modal(),
modal::component_modal(),
paginate::paginate(),
panic_handler::div(),
parameter_attributes::addmultiple(),
parameter_attributes::voiceinfo(),
parameter_attributes::say(),
parameter_attributes::punish(),
parameter_attributes::stringlen(),
raw_identifiers::r#move(),
response_with_reply::reply(),
subcommands::parent(),
subcommand_required::parent_subcommand_required(),
track_edits::test_reuse_response(),
track_edits::add(),
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("~".into()),
non_command_message: Some(|_, msg| {
Box::pin(async move {
println!("what the hell");
match error {
poise::FrameworkError::ArgumentParse { error, .. } => {
if let Some(error) = error.downcast_ref::<serenity::RoleParseError>() {
println!("Found a RoleParseError: {:?}", error);
} else {
println!("Not a RoleParseError :(");
}
}
other => poise::builtins::on_error(other).await.unwrap(),
}
println!("non command message!: {}", msg.content);
Ok(())
})
},
}),
..Default::default()
})
.setup(move |ctx, _ready, framework| {
},
on_error: |error| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(())
println!("what the hell");
match error {
poise::FrameworkError::ArgumentParse { error, .. } => {
if let Some(error) = error.downcast_ref::<serenity::RoleParseError>() {
println!("Found a RoleParseError: {:?}", error);
} else {
println!("Not a RoleParseError :(");
}
}
other => poise::builtins::on_error(other).await.unwrap(),
}
})
})
.build();
},
..Default::default()
};

let token = std::env::var("DISCORD_TOKEN").unwrap();
let intents =
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;

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

client.unwrap().start().await.unwrap()
Expand Down
58 changes: 30 additions & 28 deletions examples/help_generation/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,43 +319,45 @@ You can edit your `?help` message to the bot and the bot will edit its response.
Ok(())
}

#[poise::command(prefix_command, owners_only)]
async fn register_commands(ctx: Context<'_>) -> Result<(), Error> {
let commands = &ctx.framework().options().commands;
poise::builtins::register_globally(ctx, commands).await?;

ctx.say("Successfully registered slash commands!").await?;
Ok(())
}

#[tokio::main]
async fn main() {
let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
let intents =
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;

let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![
fruit(),
vegetable(),
beer(),
meat(),
dairy(),
help(),
context_food(),
context_fruit(),
context_vegetable(),
context_meat(),
food_react(),
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("?".into()),
..Default::default()
},
let options = poise::FrameworkOptions {
commands: vec![
register_commands(),
fruit(),
vegetable(),
beer(),
meat(),
dairy(),
help(),
context_food(),
context_fruit(),
context_vegetable(),
context_meat(),
food_react(),
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("?".into()),
..Default::default()
})
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(())
})
})
.build();
},
..Default::default()
};

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

client.unwrap().start().await.unwrap();
Expand Down
Loading

0 comments on commit 3e26ef8

Please sign in to comment.