Merge pull request #3 from victormignot/discord-connection

Init basic connection to Discord
This commit is contained in:
Victor Mignot 2022-10-17 13:37:20 +00:00 committed by GitHub
commit 3b49b951d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 1909 additions and 2 deletions

2
.env.example Normal file
View file

@ -0,0 +1,2 @@
# Discord Token
DISCORD_TOKEN="Enter your Discord secret token"

2
.gitignore vendored
View file

@ -1 +1,3 @@
/target /target
.env

1855
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,3 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
serenity = { version="0.11", default-features = false, features = ["client", "gateway", "rustls_backend", "model" ] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
mongodb = "2.3.0"

View file

@ -30,6 +30,12 @@
rust-analyzer rust-analyzer
rustfmt rustfmt
]; ];
shellHook = ''
set -a
source .env
set +a
'';
}; };
} }
); );

25
src/client.rs Normal file
View file

@ -0,0 +1,25 @@
use serenity::{prelude::GatewayIntents, Client as DiscordClient};
pub struct Client {
discord_client: DiscordClient,
}
pub struct ClientOptions<'a> {
pub discord_token: &'a String,
}
impl<'a> Client {
pub async fn new(options: ClientOptions<'a>) -> Client {
let discord_client = DiscordClient::builder(options.discord_token, GatewayIntents::empty())
.await
.expect("Could not create Discord Client");
Client { discord_client }
}
pub async fn connect_discord(&mut self) -> () {
if let Err(error) = self.discord_client.start().await {
println!("Could not connect to Discord: {:?}", error);
}
}
}

1
src/lib.rs Normal file
View file

@ -0,0 +1 @@
pub mod client;

View file

@ -1,3 +1,16 @@
fn main() { use std::env;
println!("Hello, world!");
use yorokobot::client::{Client, ClientOptions};
#[tokio::main]
async fn main() {
let discord_token = env::var("DISCORD_TOKEN").expect("Cannot fetch Discord token");
let options = ClientOptions {
discord_token: &discord_token,
};
let mut client = Client::new(options).await;
client.connect_discord().await;
} }