Skip to content

Commit 6f87d25

Browse files
authored
Add a method to ClientBuilder for retrieving the current token (#1550)
1 parent 9bfc1e7 commit 6f87d25

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

src/client/mod.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,8 @@ pub use crate::CacheAndHttp;
7676
/// A builder implementing [`Future`] building a [`Client`] to interact with Discord.
7777
#[cfg(feature = "gateway")]
7878
pub struct ClientBuilder<'a> {
79-
// FIXME: Remove this allow attribute once `application_id` is no longer feature-gated
80-
// under `unstable_discord_api`.
81-
#[allow(dead_code)]
82-
token: Option<String>,
8379
data: Option<TypeMap>,
84-
http: Option<Http>,
80+
http: Http,
8581
fut: Option<BoxFuture<'a, Result<Client>>>,
8682
intents: GatewayIntents,
8783
#[cfg(feature = "unstable_discord_api")]
@@ -100,11 +96,10 @@ pub struct ClientBuilder<'a> {
10096

10197
#[cfg(feature = "gateway")]
10298
impl<'a> ClientBuilder<'a> {
103-
fn _new() -> Self {
99+
fn _new(http: Http) -> Self {
104100
Self {
105-
token: None,
106101
data: Some(TypeMap::new()),
107-
http: None,
102+
http,
108103
fut: None,
109104
intents: GatewayIntents::non_privileged(),
110105
#[cfg(feature = "unstable_discord_api")]
@@ -130,7 +125,7 @@ impl<'a> ClientBuilder<'a> {
130125
/// a framework via the [`Self::framework`] or [`Self::framework_arc`] method,
131126
/// otherwise awaiting the builder will cause a panic.
132127
pub fn new(token: impl AsRef<str>) -> Self {
133-
Self::_new().token(token)
128+
Self::_new(Http::new_with_token(token.as_ref()))
134129
}
135130

136131
/// Construct a new builder with a [`Http`] instance to calls methods on
@@ -143,35 +138,30 @@ impl<'a> ClientBuilder<'a> {
143138
///
144139
/// [`Http`]: crate::http::Http
145140
pub fn new_with_http(http: Http) -> Self {
146-
let mut c = Self::_new();
147-
c.http = Some(http);
148-
c
141+
Self::_new(http)
149142
}
150143

151144
/// Sets a token for the bot. If the token is not prefixed "Bot ",
152145
/// this method will automatically do so.
153146
pub fn token(mut self, token: impl AsRef<str>) -> Self {
154-
let token = token.as_ref().trim();
155-
156-
let token =
157-
if token.starts_with("Bot ") { token.to_string() } else { format!("Bot {}", token) };
158-
159-
self.token = Some(token.clone());
160-
161-
self.http = Some(Http::new_with_token(&token));
147+
self.http = Http::new_with_token(token.as_ref());
162148

163149
self
164150
}
165151

152+
/// Gets the current token used for the [`Http`] client.
153+
///
154+
/// [`Http`]: crate::http::Http
155+
pub fn get_token(&self) -> &str {
156+
&self.http.token
157+
}
158+
166159
/// Sets the application id.
167160
#[cfg(feature = "unstable_discord_api")]
168161
pub fn application_id(mut self, application_id: u64) -> Self {
169162
self.application_id = Some(ApplicationId(application_id));
170163

171-
self.http = Some(Http::new_with_token_application_id(
172-
&self.token.clone().expect("no token"),
173-
application_id,
174-
));
164+
self.http = Http::new_with_token_application_id(self.get_token(), application_id);
175165

176166
self
177167
}
@@ -359,7 +349,7 @@ impl<'a> Future for ClientBuilder<'a> {
359349
let event_handler = self.event_handler.take();
360350
let raw_event_handler = self.raw_event_handler.take();
361351
let intents = self.intents;
362-
let http = Arc::new(self.http.take().unwrap());
352+
let http = Arc::new(std::mem::take(&mut self.http));
363353

364354
#[cfg(feature = "unstable_discord_api")]
365355
if http.application_id == 0 {

0 commit comments

Comments
 (0)