@@ -43,37 +43,45 @@ docs.
4343A basic ping-pong bot looks like:
4444
4545``` rust
46- use std :: env ;
46+ use std :: sync :: Arc ;
4747
4848use serenity :: async_trait;
49- use serenity :: model :: channel :: Message ;
49+ use serenity :: gateway :: client :: FullEvent ;
5050use serenity :: prelude :: * ;
5151
5252struct Handler ;
5353
5454#[async_trait]
5555impl EventHandler for Handler {
56- async fn message (& self , ctx : & Context , msg : & Message ) {
57- if msg . content == " !ping" {
58- if let Err (why ) = msg . channel_id. say (& ctx . http, " Pong!" ). await {
59- println! (" Error sending message: {why:?}" );
60- }
56+ async fn dispatch (& self , ctx : & Context , event : & FullEvent ) {
57+ match event {
58+ FullEvent :: Message {
59+ new_message , ..
60+ } => {
61+ if new_message . content == " !ping" {
62+ if let Err (why ) = new_message . channel_id. say (& ctx . http, " Pong!" ). await {
63+ println! (" Error sending message: {why:?}" );
64+ }
65+ }
66+ },
67+ _ => {},
6168 }
6269 }
6370}
6471
6572#[tokio:: main]
6673async fn main () {
6774 // Login with a bot token from the environment
68- let token = env :: var (" DISCORD_TOKEN" ). expect (" Expected a token in the environment" );
75+ let token =
76+ Token :: from_env (" DISCORD_TOKEN" ). expect (" Expected a valid token in the environment" );
6977 // Set gateway intents, which decides what events the bot will be notified about
7078 let intents = GatewayIntents :: GUILD_MESSAGES
7179 | GatewayIntents :: DIRECT_MESSAGES
7280 | GatewayIntents :: MESSAGE_CONTENT ;
7381
7482 // Create a new instance of the Client, logging in as a bot.
75- let mut client = Client :: builder (& token , intents )
76- . event_handler (Handler )
83+ let mut client = Client :: builder (token , intents )
84+ . event_handler (Arc :: new ( Handler ) )
7785 . await
7886 . expect (" Error creating client" );
7987
0 commit comments