pub struct Credentials { /* private fields */ }Expand description
The application’s public key (required, verifies Interactions), and optionally the bot token
(for Ctx::bot) and the application id (for a Bot Client outside
Interactions).
Build it in code, from the conventional variables with from_lookup, or
deserialize it: fields public_key, bot_token (or token), application_id.
use composure::Credentials;
let key = "a".repeat(64);
let credentials = Credentials::new(&key)?.with_bot_token("token");
assert_eq!(credentials.bot_token(), Some("token"));
// `config`: `settings.get::<Credentials>("discord")`, `envy`:
// `envy::prefixed("DISCORD_").from_env::<Credentials>()`, or any serde format
let credentials: Credentials = serde_json::from_str(&format!(
r#"{{ "public_key": "{key}", "token": "token", "application_id": 123 }}"#
)).unwrap();
assert_eq!(credentials.application_id(), Some(123u64.into()));Debug never shows the bot token.
Implementations§
Source§impl Credentials
impl Credentials
Sourcepub fn new(public_key: &str) -> Result<Self, CredentialsError>
pub fn new(public_key: &str) -> Result<Self, CredentialsError>
With the application’s public key (64 hex characters), no bot token or application id
Sourcepub fn from_lookup(
lookup: impl FnMut(&str) -> Option<String>,
) -> Result<Self, CredentialsError>
pub fn from_lookup( lookup: impl FnMut(&str) -> Option<String>, ) -> Result<Self, CredentialsError>
From the conventional variables, lookup reading one by name: DISCORD_PUBLIC_KEY
(required), DISCORD_TOKEN and DISCORD_APPLICATION_ID (optional). An empty value is
unset.
let credentials = Credentials::from_lookup(|name| std::env::var(name).ok());Sourcepub fn with_bot_token(self, token: impl Into<String>) -> Self
pub fn with_bot_token(self, token: impl Into<String>) -> Self
With the bot token, without the Bot prefix
Sourcepub fn with_application_id(self, id: Snowflake) -> Self
pub fn with_application_id(self, id: Snowflake) -> Self
With the application id
Sourcepub fn public_key(&self) -> &str
pub fn public_key(&self) -> &str
The application’s public key, as hex
Sourcepub fn application_id(&self) -> Option<Snowflake>
pub fn application_id(&self) -> Option<Snowflake>
The application id
Sourcepub fn bot_client<T: Transport>(&self, transport: T) -> Option<BotClient<T>>
Available on crate feature rest only.
pub fn bot_client<T: Transport>(&self, transport: T) -> Option<BotClient<T>>
rest only.A Bot Client over transport, for REST outside Interactions; None without both a bot
token and an application id