Skip to content

Commit

Permalink
Change README example to not use standard framework (#2782)
Browse files Browse the repository at this point in the history
Updates the example to a minimal version of e01_basic_ping_bot.
  • Loading branch information
mkrasnitski authored Mar 1, 2024
1 parent c591d25 commit 50654db
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
52 changes: 23 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,39 @@ A basic ping-pong bot looks like:
use std::env;
use serenity::async_trait;
use serenity::prelude::*;
use serenity::model::channel::Message;
use serenity::framework::standard::macros::{command, group};
use serenity::framework::standard::{StandardFramework, Configuration, CommandResult};
#[group]
#[commands(ping)]
struct General;
use serenity::prelude::*;
struct Handler;
#[async_trait]
impl EventHandler for Handler {}
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!ping" {
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
println!("Error sending message: {why:?}");
}
}
}
}
#[tokio::main]
async fn main() {
let framework = StandardFramework::new().group(&GENERAL_GROUP);
framework.configure(Configuration::new().prefix("~")); // set the bot's prefix to "~"
// Login with a bot token from the environment
let token = env::var("DISCORD_TOKEN").expect("token");
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(token, intents)
.event_handler(Handler)
.framework(framework)
.await
.expect("Error creating client");
// start listening for events by starting a single shard
if let Err(why) = client.start().await {
println!("An error occurred while running the client: {:?}", why);
}
}
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
// Set gateway intents, which decides what events the bot will be notified about
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
#[command]
async fn ping(ctx: &Context, msg: &Message) -> CommandResult {
msg.reply(ctx, "Pong!").await?;
// Create a new instance of the Client, logging in as a bot.
let mut client =
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
Ok(())
// Start listening for events by starting a single shard
if let Err(why) = client.start().await {
println!("Client error: {why:?}");
}
}
```

Expand Down Expand Up @@ -157,7 +151,7 @@ the Discord gateway over a WebSocket client.
enough level that optional parameters can be provided at will via a JsonMap.
- **model**: Method implementations for models, acting as helper methods over
the HTTP functions.
- **standard_framework**: A standard, default implementation of the Framework
- **standard_framework**: A standard, default implementation of the Framework. **NOTE**: Deprecated as of v0.12.1. Using the [poise](https://github.com/serenity-rs/poise) framework is recommended instead.
- **utils**: Utility functions for common use cases by users.
- **voice**: Enables registering a voice plugin to the client, which will handle actual voice connections from Discord.
[lavalink-rs][project:lavalink-rs] or [Songbird][project:songbird] are recommended voice plugins.
Expand Down
7 changes: 3 additions & 4 deletions examples/e01_basic_ping_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ struct Handler;

#[async_trait]
impl EventHandler for Handler {
// Set a handler for the `message` event - so that whenever a new message is received - the
// closure (or function) passed will be called.
// Set a handler for the `message` event. This is called whenever a new message is received.
//
// Event handlers are dispatched through a threadpool, and so multiple events can be dispatched
// simultaneously.
// Event handlers are dispatched through a threadpool, and so multiple events can be
// dispatched simultaneously.
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!ping" {
// Sending a message can fail, due to a network error, an authentication error, or lack
Expand Down

0 comments on commit 50654db

Please sign in to comment.