Skip to content

Commit

Permalink
Fix all clippy warnings and deprecate Client::new (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkerens authored Oct 1, 2020
1 parent 87df95f commit ffc2997
Show file tree
Hide file tree
Showing 42 changed files with 144 additions and 154 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Serenity is a Rust library for the Discord API.

View the [examples] on how to make and structure a bot.

Serenity supports bot login via the use of [`Client::new`].
Serenity supports bot login via the use of [`Client::builder`].

You may also check your tokens prior to login via the use of
[`validate_token`].
Expand Down Expand Up @@ -70,7 +70,7 @@ async fn main() {
// Login with a bot token from the environment
let token = env::var("DISCORD_TOKEN").expect("token");
let mut client = Client::new(token)
let mut client = Client::builder(token)
.event_handler(Handler)
.framework(framework)
.await
Expand Down Expand Up @@ -204,7 +204,7 @@ Voice + youtube-dl:
- [lavalink-rs][project:lavalink-rs]: An interface to [Lavalink][repo:lavalink], an audio sending node based on [Lavaplayer][repo:lavaplayer]

[`Cache`]: https://docs.rs/serenity/*/serenity/cache/struct.Cache.html
[`Client::new`]: https://docs.rs/serenity/*/serenity/client/struct.Client.html#method.new
[`Client::builder`]: https://docs.rs/serenity/*/serenity/client/struct.Client.html#method.builder
[`EventHandler::message`]: https://docs.rs/serenity/*/serenity/client/trait.EventHandler.html#method.message
[`Context`]: https://docs.rs/serenity/*/serenity/client/struct.Context.html
[`Event`]: https://docs.rs/serenity/*/serenity/model/event/enum.Event.html
Expand Down
2 changes: 1 addition & 1 deletion examples/e01_basic_ping_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,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::new(&token)
let mut client = Client::builder(&token)
.event_handler(Handler)
.await
.expect("Err creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/e02_transparent_guild_sharding/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ 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 mut client = Client::new(&token)
let mut client = Client::builder(&token)
.event_handler(Handler)
.await
.expect("Err creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/e03_struct_utilities/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ 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 mut client = Client::new(&token)
let mut client = Client::builder(&token)
.event_handler(Handler)
.await
.expect("Err creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/e04_message_builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ 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 mut client = Client::new(&token)
let mut client = Client::builder(&token)
.event_handler(Handler)
.await
.expect("Err creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/e05_command_framework/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ async fn main() {
.group(&MATH_GROUP)
.group(&OWNER_GROUP);

let mut client = Client::new(&token)
let mut client = Client::builder(&token)
.event_handler(Handler)
.framework(framework)
.await
Expand Down
2 changes: 1 addition & 1 deletion examples/e06_voice/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async fn main() {
.prefix("~"))
.group(&GENERAL_GROUP);

let mut client = Client::new(&token)
let mut client = Client::builder(&token)
.event_handler(Handler)
.framework(framework)
.await
Expand Down
2 changes: 1 addition & 1 deletion examples/e07_sample_bot_structure/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async fn main() {
.prefix("~"))
.group(&GENERAL_GROUP);

let mut client = Client::new(&token)
let mut client = Client::builder(&token)
.framework(framework)
.event_handler(Handler)
.await
Expand Down
2 changes: 1 addition & 1 deletion examples/e08_env_logging/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async fn main() {
.before(before)
.group(&GENERAL_GROUP);

let mut client = Client::new(&token)
let mut client = Client::builder(&token)
.event_handler(Handler)
.framework(framework)
.await
Expand Down
2 changes: 1 addition & 1 deletion examples/e09_shard_manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async fn main() {
let token = env::var("DISCORD_TOKEN")
.expect("Expected a token in the environment");

let mut client = Client::new(&token)
let mut client = Client::builder(&token)
.event_handler(Handler)
.await
.expect("Err creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/e10_voice_receive/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async fn main() {
.prefix("~"))
.group(&GENERAL_GROUP);

let mut client = Client::new(&token)
let mut client = Client::builder(&token)
.event_handler(Handler)
.framework(framework)
.await
Expand Down
2 changes: 1 addition & 1 deletion examples/e11_create_message_builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ 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 mut client = Client::new(&token)
let mut client = Client::builder(&token)
.event_handler(Handler)
.await
.expect("Err creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/e12_collectors/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async fn main() {
.help(&MY_HELP)
.group(&COLLECTOR_GROUP);

let mut client = Client::new(&token)
let mut client = Client::builder(&token)
.event_handler(Handler)
.framework(framework)
.await
Expand Down
2 changes: 1 addition & 1 deletion examples/e13_gateway_intents/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn main() {
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");

// Build our client.
let mut client = Client::new(token)
let mut client = Client::builder(token)
.event_handler(Handler)
.add_intent(GatewayIntents::GUILDS)
.add_intent(GatewayIntents::GUILD_MESSAGES)
Expand Down
4 changes: 2 additions & 2 deletions src/builder/create_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl CreateEmbed {
/// }
/// }
///
/// let mut client = Client::new("token").event_handler(Handler).await?;
/// let mut client = Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down Expand Up @@ -296,7 +296,7 @@ impl CreateEmbed {
/// }
/// }
///
/// let mut client =Client::new("token").event_handler(Handler).await?;
/// let mut client =Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/builder/create_invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use serde_json::Value;
/// }
/// }
///
/// let mut client =Client::new("token").event_handler(Handler).await?;
/// let mut client =Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down
15 changes: 8 additions & 7 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl Cache {
/// }
/// }
///
/// let mut client =Client::new("token").event_handler(Handler).await?;
/// let mut client =Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down Expand Up @@ -332,12 +332,13 @@ impl Cache {
/// [`Guild`]: ../model/guild/struct.Guild.html
/// [`Shard`]: ../gateway/struct.Shard.html
pub async fn guilds(&self) -> Vec<GuildId> {
let chain = self.unavailable_guilds.read().await.clone().into_iter();
self.guilds
.read()
.await
.keys()
.cloned()
.chain(self.unavailable_guilds.read().await.clone().into_iter())
.chain(chain)
.collect()
}

Expand Down Expand Up @@ -434,7 +435,7 @@ impl Cache {
async fn _guild_field<Ret, Fun>(&self, id: GuildId, field_accessor: Fun) -> Option<Ret>
where Fun: FnOnce(&Guild) -> Ret {
let guilds = self.guilds.read().await;
let guild = guilds.get(&id.into())?;
let guild = guilds.get(&id)?;

Some(field_accessor(guild))
}
Expand Down Expand Up @@ -481,7 +482,7 @@ impl Cache {
///
/// # #[cfg(feature = "client")]
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client =Client::new("token").event_handler(Handler).await?;
/// let mut client =Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down Expand Up @@ -530,7 +531,7 @@ impl Cache {
field_selector: Fun) -> Option<Ret>
where Fun: FnOnce(&GuildChannel) -> Ret {
let guild_channels = &self.channels.read().await;
let channel = guild_channels.get(&id.into())?;
let channel = guild_channels.get(&id)?;

Some(field_selector(channel))
}
Expand Down Expand Up @@ -678,7 +679,7 @@ impl Cache {
/// Returns the number of shards.
#[inline]
pub async fn shard_count(&self) -> u64 {
self.shard_count.read().await.clone()
*self.shard_count.read().await
}

/// Retrieves a [`Channel`]'s message from the cache based on the channel's and
Expand Down Expand Up @@ -923,7 +924,7 @@ impl Cache {
where Fun: FnOnce(&CurrentUser) -> Ret {
let user = self.user.read().await;

field_selector(&user).clone()
field_selector(&user)
}

/// Updates the cache with the update implementation for an event or other
Expand Down
2 changes: 1 addition & 1 deletion src/client/bridge/gateway/shard_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl ShardManager {
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let token = std::env::var("DISCORD_TOKEN")?;
/// let mut client = Client::new(&token).event_handler(Handler).await?;
/// let mut client = Client::builder(&token).event_handler(Handler).await?;
///
/// // restart shard ID 7
/// client.shard_manager.lock().await.restart(ShardId(7)).await;
Expand Down
12 changes: 6 additions & 6 deletions src/client/bridge/gateway/shard_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,24 +541,24 @@ impl ShardRunner {

match why {
Error::Gateway(GatewayError::InvalidAuthentication) => {
if let Err(_) = self.manager_tx.unbounded_send(
ShardManagerMessage::ShardInvalidAuthentication) {
if self.manager_tx.unbounded_send(
ShardManagerMessage::ShardInvalidAuthentication).is_err() {
panic!("Failed sending InvalidAuthentication error to the shard manager.");
}

return Err(why);
},
Error::Gateway(GatewayError::InvalidGatewayIntents) => {
if let Err(_) = self.manager_tx.unbounded_send(
ShardManagerMessage::ShardInvalidGatewayIntents) {
if self.manager_tx.unbounded_send(
ShardManagerMessage::ShardInvalidGatewayIntents).is_err() {
panic!("Failed sending InvalidGatewayIntents error to the shard manager.");
}

return Err(why);
},
Error::Gateway(GatewayError::DisallowedGatewayIntents) => {
if let Err(_) = self.manager_tx.unbounded_send(
ShardManagerMessage::ShardDisallowedGatewayIntents) {
if self.manager_tx.unbounded_send(
ShardManagerMessage::ShardDisallowedGatewayIntents).is_err() {
panic!("Failed sending DisallowedGatewayIntents error to the shard manager.");
}

Expand Down
16 changes: 8 additions & 8 deletions src/client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Context {
/// }
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client =Client::new("token").event_handler(Handler).await?;
/// let mut client =Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down Expand Up @@ -153,7 +153,7 @@ impl Context {
/// }
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client =Client::new("token").event_handler(Handler).await?;
/// let mut client =Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down Expand Up @@ -190,7 +190,7 @@ impl Context {
/// }
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client =Client::new("token").event_handler(Handler).await?;
/// let mut client =Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down Expand Up @@ -226,7 +226,7 @@ impl Context {
/// }
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client =Client::new("token").event_handler(Handler).await?;
/// let mut client =Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down Expand Up @@ -264,7 +264,7 @@ impl Context {
/// }
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client = Client::new("token").event_handler(Handler).await?;
/// let mut client = Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down Expand Up @@ -307,7 +307,7 @@ impl Context {
/// }
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client =Client::new("token").event_handler(Handler).await?;
/// let mut client =Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down Expand Up @@ -343,7 +343,7 @@ impl Context {
/// }
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client =Client::new("token").event_handler(Handler).await?;
/// let mut client =Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down Expand Up @@ -373,7 +373,7 @@ impl Context {
/// }
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client =Client::new("token").event_handler(Handler).await?;
/// let mut client =Client::builder("token").event_handler(Handler).await?;
///
/// client.start().await?;
/// # Ok(())
Expand Down
Loading

0 comments on commit ffc2997

Please sign in to comment.