-
Notifications
You must be signed in to change notification settings - Fork 117
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
feat(macro): add group macro #267
Open
TitaniumBrain
wants to merge
16
commits into
serenity-rs:current
Choose a base branch
from
TitaniumBrain:current
base: current
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
93caa09
feat(macro): add group macro
TitaniumBrain 8aaaf6c
Remove unnecessary use statement
TitaniumBrain 6da3446
Refactor group for readability
TitaniumBrain 26498bf
Refactor is_command_attr
TitaniumBrain 36bf529
Refactor is_command_attr for simplicity
TitaniumBrain 5203830
Replace empty quote! with constructor
TitaniumBrain 3e7ca23
Change comment style
TitaniumBrain 8874415
Use full path for darling::Error
TitaniumBrain fb18eee
Call .to_token_stream directly instead of using macro
TitaniumBrain 90a243f
Add example group test
TitaniumBrain a1cd35c
fix: change CommandGroup to use associated types
TitaniumBrain ff03d77
Update group_testing example
TitaniumBrain ecd5725
Update CommandGroup doc
TitaniumBrain 5e8c898
Change None check style
TitaniumBrain 3687fd8
Change example to register commands globally
TitaniumBrain bb4665c
Merge branch 'serenity-rs:current' into current
TitaniumBrain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use poise::{serenity_prelude as serenity, Command, CommandGroup}; | ||
use std::{env::var, sync::Arc, time::Duration, vec}; | ||
// Types used by all command functions | ||
type Error = Box<dyn std::error::Error + Send + Sync>; | ||
type Context<'a> = poise::Context<'a, Data, Error>; | ||
|
||
// Custom user data passed to all command functions | ||
pub struct Data {} | ||
|
||
// Group struct | ||
struct Test {} | ||
|
||
#[poise::group(category = "Foo")] | ||
impl Test { | ||
// Just a test | ||
#[poise::command(slash_command, prefix_command, rename = "test")] | ||
async fn test_command(ctx: Context<'_>) -> Result<(), Error> { | ||
let name = ctx.author(); | ||
ctx.say(format!("Hello, {}", name)).await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
// Handlers | ||
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) { | ||
// This is our custom error handler | ||
// 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,); | ||
} | ||
error => { | ||
if let Err(e) = poise::builtins::on_error(error).await { | ||
println!("Error while handling error: {}", e) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// FrameworkOptions contains all of poise's configuration option in one struct | ||
// Every option can be omitted to use its default value | ||
// println!("{:#?}", Test::commands()); | ||
let commands: Vec<Command<Data, Error>> = Test::commands(); | ||
|
||
let options = poise::FrameworkOptions { | ||
commands: commands, | ||
prefix_options: poise::PrefixFrameworkOptions { | ||
prefix: Some("--".into()), | ||
edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan( | ||
Duration::from_secs(3600), | ||
))), | ||
..Default::default() | ||
}, | ||
// The global error handler for all error cases that may occur | ||
on_error: |error| Box::pin(on_error(error)), | ||
// This code is run before every command | ||
pre_command: |ctx| { | ||
Box::pin(async move { | ||
println!("Executing command {}...", ctx.command().qualified_name); | ||
}) | ||
}, | ||
// This code is run after a command if it was successful (returned Ok) | ||
post_command: |ctx| { | ||
Box::pin(async move { | ||
println!("Executed command {}!", ctx.command().qualified_name); | ||
}) | ||
}, | ||
// Every command invocation must pass this check to continue execution | ||
command_check: Some(|ctx| { | ||
Box::pin(async move { | ||
if ctx.author().id == 123456789 { | ||
return Ok(false); | ||
} | ||
Ok(true) | ||
}) | ||
}), | ||
// 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| { | ||
Box::pin(async move { | ||
println!( | ||
"Got an event in event handler: {:?}", | ||
event.snake_case_name() | ||
); | ||
Ok(()) | ||
}) | ||
}, | ||
..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_in_guild( | ||
ctx, | ||
&framework.options().commands, | ||
serenity::GuildId::new(308744621616529410), | ||
) | ||
.await?; | ||
Ok(Data {}) | ||
}) | ||
}) | ||
.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(framework) | ||
.await; | ||
|
||
client.unwrap().start().await.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can your add a test for multiple commands, and a test for no commands in a group?