Skip to content

Commit

Permalink
Improve token semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Nov 19, 2024
1 parent a1450a5 commit 5de6e13
Show file tree
Hide file tree
Showing 29 changed files with 273 additions and 215 deletions.
7 changes: 5 additions & 2 deletions examples/e01_basic_ping_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ impl EventHandler for Handler {
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");
// Set gateway intents, which decides what events the bot will be notified about
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
Expand All @@ -46,7 +49,7 @@ async fn main() {
// Create a new instance of the Client, logging in as a bot. This will automatically prepend
// your bot token with "Bot ", which is a requirement by Discord for bot users.
let mut client =
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");

// Finally, start a single shard, and start listening to events.
//
Expand Down
7 changes: 5 additions & 2 deletions examples/e02_transparent_guild_sharding/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ impl EventHandler for Handler {
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client =
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");

// The total number of shards to use. The "current shard number" of a shard - that is, the
// shard it is assigned to - is indexed at 0, while the total shard count is indexed at 1.
Expand Down
7 changes: 5 additions & 2 deletions examples/e03_struct_utilities/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ impl EventHandler for Handler {
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client =
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");

if let Err(why) = client.start().await {
println!("Client error: {why:?}");
Expand Down
7 changes: 5 additions & 2 deletions examples/e04_message_builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ impl EventHandler for Handler {
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client =
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");

if let Err(why) = client.start().await {
println!("Client error: {why:?}");
Expand Down
7 changes: 5 additions & 2 deletions examples/e05_sample_bot_structure/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ impl EventHandler for Handler {
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");

// Build our client.
let mut client = Client::builder(&token, GatewayIntents::empty())
let mut client = Client::builder(token, GatewayIntents::empty())
.event_handler(Handler)
.await
.expect("Error creating client");
Expand Down
7 changes: 5 additions & 2 deletions examples/e06_env_logging/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ async fn main() {
tracing_subscriber::fmt::init();

// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");

let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;

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

if let Err(why) = client.start().await {
error!("Client error: {:?}", why);
Expand Down
7 changes: 5 additions & 2 deletions examples/e07_shard_manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ impl EventHandler for Handler {
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");

let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client =
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");

// Here we clone a lock to the Shard Manager, and then move it into a new thread. The thread
// will unlock the manager and print shards' status on a loop.
Expand Down
7 changes: 5 additions & 2 deletions examples/e08_create_message_builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ impl EventHandler for Handler {
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client =
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
Client::builder(token, intents).event_handler(Handler).await.expect("Err creating client");

if let Err(why) = client.start().await {
println!("Client error: {why:?}");
Expand Down
7 changes: 5 additions & 2 deletions examples/e09_collectors/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,18 @@ impl EventHandler for Handler {
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");

let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT
| GatewayIntents::GUILD_MESSAGE_REACTIONS;

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

if let Err(why) = client.start().await {
println!("Client error: {why:?}");
Expand Down
7 changes: 5 additions & 2 deletions examples/e10_gateway_intents/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ impl EventHandler for Handler {
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");

// Intents are a bitflag, bitwise operations can be used to dictate which intents to use
let intents =
GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT;
// Build our client.
let mut client = Client::builder(&token, intents)
let mut client = Client::builder(token, intents)
.event_handler(Handler)
.await
.expect("Error creating client");
Expand Down
7 changes: 5 additions & 2 deletions examples/e11_global_data/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ impl EventHandler for Handler {

#[tokio::main]
async fn main() {
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");

// We setup the initial value for our user data, which we will use throughout the rest of our
// program.
Expand All @@ -72,7 +75,7 @@ async fn main() {
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(&token, intents)
let mut client = Client::builder(token, intents)
// Specifying the data type as a type argument here is optional, but if done, you can
// guarantee that Context::data will not panic if the same type is given, as providing the
// incorrect type will lead to a compiler error, rather than a runtime panic.
Expand Down
7 changes: 5 additions & 2 deletions examples/e12_parallel_loops/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ fn set_activity_to_current_time(ctx: &Context) {

#[tokio::main]
async fn main() {
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");

let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::GUILDS
| GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(&token, intents)
let mut client = Client::builder(token, intents)
.event_handler(Handler {
is_loop_running: AtomicBool::new(false),
})
Expand Down
7 changes: 5 additions & 2 deletions examples/e13_sqlite_database/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ impl EventHandler for Bot {
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.
let token = std::env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = std::env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");

// Initiate a connection to the database file, creating the file if required.
let database = sqlx::sqlite::SqlitePoolOptions::new()
Expand All @@ -96,6 +99,6 @@ async fn main() {
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client =
Client::builder(&token, intents).event_handler(bot).await.expect("Err creating client");
Client::builder(token, intents).event_handler(bot).await.expect("Err creating client");
client.start().await.unwrap();
}
7 changes: 5 additions & 2 deletions examples/e14_message_components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,16 @@ impl EventHandler for Handler {
async fn main() {
dotenv().ok();
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");

// Build our client.
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(&token, intents)
let mut client = Client::builder(token, intents)
.event_handler(Handler)
.await
.expect("Error creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/e15_webhook/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serenity::model::webhook::Webhook;
#[tokio::main]
async fn main() {
// You don't need a token when you are only dealing with webhooks.
let http = Http::new("");
let http = Http::new();
let webhook = Webhook::from_url(&http, "https://discord.com/api/webhooks/133742013374206969/hello-there-oPNtRN5UY5DVmBe7m1N0HE-replace-me-Dw9LRkgq3zI7LoW3Rb-k-q")
.await
.expect("Replace the webhook with your own");
Expand Down
7 changes: 5 additions & 2 deletions examples/testing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,10 @@ async fn main() -> Result<(), serenity::Error> {
}

env_logger::init();
let token = std::env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = std::env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment")
.parse()
.expect("Invalid token");
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
Client::builder(&token, intents).event_handler(Handler).await?.start().await
Client::builder(token, intents).event_handler(Handler).await?.start().await
}
Loading

0 comments on commit 5de6e13

Please sign in to comment.