Skip to content

Commit 2369544

Browse files
authored
Improve token semantics and clean up gateway feature gates (#3052)
1 parent 1a0a809 commit 2369544

File tree

39 files changed

+434
-378
lines changed

39 files changed

+434
-378
lines changed

examples/e01_basic_ping_bot/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::env;
2-
31
use serenity::async_trait;
42
use serenity::model::channel::Message;
53
use serenity::model::gateway::Ready;
@@ -37,7 +35,8 @@ impl EventHandler for Handler {
3735
#[tokio::main]
3836
async fn main() {
3937
// Configure the client with your Discord bot token in the environment.
40-
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
38+
let token =
39+
Token::from_env("DISCORD_TOKEN").expect("Expected a valid token in the environment");
4140
// Set gateway intents, which decides what events the bot will be notified about
4241
let intents = GatewayIntents::GUILD_MESSAGES
4342
| GatewayIntents::DIRECT_MESSAGES
@@ -46,7 +45,7 @@ async fn main() {
4645
// Create a new instance of the Client, logging in as a bot. This will automatically prepend
4746
// your bot token with "Bot ", which is a requirement by Discord for bot users.
4847
let mut client =
49-
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
48+
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");
5049

5150
// Finally, start a single shard, and start listening to events.
5251
//

examples/e02_transparent_guild_sharding/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::env;
2-
31
use serenity::async_trait;
42
use serenity::model::channel::Message;
53
use serenity::model::gateway::Ready;
@@ -42,12 +40,13 @@ impl EventHandler for Handler {
4240
#[tokio::main]
4341
async fn main() {
4442
// Configure the client with your Discord bot token in the environment.
45-
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
43+
let token =
44+
Token::from_env("DISCORD_TOKEN").expect("Expected a valid token in the environment");
4645
let intents = GatewayIntents::GUILD_MESSAGES
4746
| GatewayIntents::DIRECT_MESSAGES
4847
| GatewayIntents::MESSAGE_CONTENT;
4948
let mut client =
50-
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
49+
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");
5150

5251
// The total number of shards to use. The "current shard number" of a shard - that is, the
5352
// shard it is assigned to - is indexed at 0, while the total shard count is indexed at 1.

examples/e03_struct_utilities/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::env;
2-
31
use serenity::async_trait;
42
use serenity::builder::CreateMessage;
53
use serenity::model::channel::Message;
@@ -35,12 +33,13 @@ impl EventHandler for Handler {
3533
#[tokio::main]
3634
async fn main() {
3735
// Configure the client with your Discord bot token in the environment.
38-
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
36+
let token =
37+
Token::from_env("DISCORD_TOKEN").expect("Expected a valid token in the environment");
3938
let intents = GatewayIntents::GUILD_MESSAGES
4039
| GatewayIntents::DIRECT_MESSAGES
4140
| GatewayIntents::MESSAGE_CONTENT;
4241
let mut client =
43-
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
42+
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");
4443

4544
if let Err(why) = client.start().await {
4645
println!("Client error: {why:?}");

examples/e04_message_builder/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::env;
2-
31
use serenity::async_trait;
42
use serenity::model::channel::Message;
53
use serenity::model::gateway::Ready;
@@ -46,12 +44,13 @@ impl EventHandler for Handler {
4644
#[tokio::main]
4745
async fn main() {
4846
// Configure the client with your Discord bot token in the environment.
49-
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
47+
let token =
48+
Token::from_env("DISCORD_TOKEN").expect("Expected a valid token in the environment");
5049
let intents = GatewayIntents::GUILD_MESSAGES
5150
| GatewayIntents::DIRECT_MESSAGES
5251
| GatewayIntents::MESSAGE_CONTENT;
5352
let mut client =
54-
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
53+
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");
5554

5655
if let Err(why) = client.start().await {
5756
println!("Client error: {why:?}");

examples/e05_sample_bot_structure/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ impl EventHandler for Handler {
7272
#[tokio::main]
7373
async fn main() {
7474
// Configure the client with your Discord bot token in the environment.
75-
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");
7677

7778
// Build our client.
78-
let mut client = Client::builder(&token, GatewayIntents::empty())
79+
let mut client = Client::builder(token, GatewayIntents::empty())
7980
.event_handler(Handler)
8081
.await
8182
.expect("Error creating client");

examples/e06_env_logging/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::env;
2-
31
use serenity::async_trait;
42
use serenity::model::event::ResumedEvent;
53
use serenity::model::gateway::Ready;
@@ -42,14 +40,15 @@ async fn main() {
4240
tracing_subscriber::fmt::init();
4341

4442
// Configure the client with your Discord bot token in the environment.
45-
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
43+
let token =
44+
Token::from_env("DISCORD_TOKEN").expect("Expected a valid token in the environment");
4645

4746
let intents = GatewayIntents::GUILD_MESSAGES
4847
| GatewayIntents::DIRECT_MESSAGES
4948
| GatewayIntents::MESSAGE_CONTENT;
5049

5150
let mut client =
52-
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
51+
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");
5352

5453
if let Err(why) = client.start().await {
5554
error!("Client error: {:?}", why);

examples/e07_shard_manager/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
//!
2020
//! Note that it may take a minute or more for a latency to be recorded or to update, depending on
2121
//! how often Discord tells the client to send a heartbeat.
22-
use std::env;
2322
use std::time::Duration;
2423

2524
use serenity::async_trait;
@@ -44,13 +43,14 @@ impl EventHandler for Handler {
4443
#[tokio::main]
4544
async fn main() {
4645
// Configure the client with your Discord bot token in the environment.
47-
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
46+
let token =
47+
Token::from_env("DISCORD_TOKEN").expect("Expected a valid token in the environment");
4848

4949
let intents = GatewayIntents::GUILD_MESSAGES
5050
| GatewayIntents::DIRECT_MESSAGES
5151
| GatewayIntents::MESSAGE_CONTENT;
5252
let mut client =
53-
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
53+
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");
5454

5555
// Here we clone a lock to the Shard Manager, and then move it into a new thread. The thread
5656
// will unlock the manager and print shards' status on a loop.

examples/e08_create_message_builder/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::env;
2-
31
use serenity::async_trait;
42
use serenity::builder::{CreateAttachment, CreateEmbed, CreateEmbedFooter, CreateMessage};
53
use serenity::model::channel::Message;
@@ -51,12 +49,13 @@ impl EventHandler for Handler {
5149
#[tokio::main]
5250
async fn main() {
5351
// Configure the client with your Discord bot token in the environment.
54-
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
52+
let token =
53+
Token::from_env("DISCORD_TOKEN").expect("Expected a valid token in the environment");
5554
let intents = GatewayIntents::GUILD_MESSAGES
5655
| GatewayIntents::DIRECT_MESSAGES
5756
| GatewayIntents::MESSAGE_CONTENT;
5857
let mut client =
59-
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
58+
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");
6059

6160
if let Err(why) = client.start().await {
6261
println!("Client error: {why:?}");

examples/e09_collectors/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! This example will showcase the beauty of collectors. They allow to await messages or reactions
22
//! from a user in the middle of a control flow, one being a command.
33
use std::collections::HashSet;
4-
use std::env;
54
use std::time::Duration;
65

76
use serenity::async_trait;
@@ -136,15 +135,16 @@ impl EventHandler for Handler {
136135
#[tokio::main]
137136
async fn main() {
138137
// Configure the client with your Discord bot token in the environment.
139-
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
138+
let token =
139+
Token::from_env("DISCORD_TOKEN").expect("Expected a valid token in the environment");
140140

141141
let intents = GatewayIntents::GUILD_MESSAGES
142142
| GatewayIntents::DIRECT_MESSAGES
143143
| GatewayIntents::MESSAGE_CONTENT
144144
| GatewayIntents::GUILD_MESSAGE_REACTIONS;
145145

146146
let mut client =
147-
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
147+
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");
148148

149149
if let Err(why) = client.start().await {
150150
println!("Client error: {why:?}");

examples/e10_gateway_intents/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::env;
2-
31
use serenity::async_trait;
42
use serenity::model::channel::Message;
53
use serenity::model::gateway::{Presence, Ready};
@@ -33,13 +31,14 @@ impl EventHandler for Handler {
3331
#[tokio::main]
3432
async fn main() {
3533
// Configure the client with your Discord bot token in the environment.
36-
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
34+
let token =
35+
Token::from_env("DISCORD_TOKEN").expect("Expected a valid token in the environment");
3736

3837
// Intents are a bitflag, bitwise operations can be used to dictate which intents to use
3938
let intents =
4039
GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT;
4140
// Build our client.
42-
let mut client = Client::builder(&token, intents)
41+
let mut client = Client::builder(token, intents)
4342
.event_handler(Handler)
4443
.await
4544
.expect("Error creating client");

0 commit comments

Comments
 (0)