-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- used serenity lib to build discord bot - made http requests with reqwest - updated .gitignore and pre-commit for rust integration
- Loading branch information
Showing
14 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,4 @@ repos: | |
- id: rust-check | ||
name: Check Rust Code | ||
entry: ./scripts/check_rust.sh | ||
files: discord/ | ||
language: rust |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "discord" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
clap = "4.5.18" | ||
dotenv = "0.15.0" | ||
once_cell = "1.19.0" | ||
reqwest = "0.12.7" | ||
serenity = "0.12.2" | ||
tokio = "1.40.0" |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// discord/src/config/mod.rs | ||
|
||
pub mod settings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// discord/src/config/settings.rs | ||
|
||
use dotenv::dotenv; | ||
use once_cell::sync::OnceCell; | ||
use std::env; | ||
|
||
|
||
#[derive(Debug, Clone)] | ||
pub struct Config { | ||
pub api_url: String, | ||
pub discord_token: String, | ||
} | ||
|
||
// Initialize a static instance of the configuration | ||
static CONFIG: OnceCell<Config> = OnceCell::new(); | ||
|
||
pub fn load_config() { | ||
// Load the configuration from the environment | ||
dotenv().ok(); | ||
|
||
let api_url = env::var("API_URL").expect("API_URL must be set"); | ||
let discord_token = env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN must be set"); | ||
|
||
let config = Config { | ||
api_url, | ||
discord_token, | ||
}; | ||
|
||
// Set the configuration | ||
CONFIG.set(config).expect("Failed to set configuration"); | ||
} | ||
|
||
pub fn get_config() -> &'static Config { | ||
CONFIG.get().expect("Configuration not loaded") | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
mod config; | ||
mod services; | ||
|
||
use config::settings; | ||
use services::api_client; | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() { | ||
// Load the configuration | ||
settings::load_config(); | ||
|
||
// Get the configuration | ||
let config = settings::get_config(); | ||
|
||
// Print the configuration | ||
println!("API URL: {}", config.api_url); | ||
println!("Discord Token: {}", config.discord_token); | ||
|
||
// Get data from the API | ||
match api_client::get("").await { | ||
Ok(response) => println!("Response: {}", response), | ||
Err(err) => eprintln!("Error: {}", err), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// discord/src/services/api_client.rs | ||
|
||
use reqwest::Client; | ||
use crate::config::settings::get_config; | ||
use std::error::Error; | ||
|
||
|
||
pub async fn get(endpoint: &str) -> Result<String, Box<dyn Error>> { | ||
// Get the configuration | ||
let config = get_config(); | ||
|
||
// Construct the URL | ||
let url = format!("{}/{}", config.api_url, endpoint); | ||
|
||
// Create a new reqwest client | ||
let client = Client::new(); | ||
|
||
// Send a GET request to the API | ||
let response = client.get(&url) | ||
.header("Authorization", &config.discord_token) | ||
.send() | ||
.await?; | ||
|
||
if response.status().is_success() { | ||
let body = response.text().await?; | ||
Ok(body) | ||
} else { | ||
Err("Failed to get data".into()) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// discord/src/services/mod.rs | ||
|
||
pub mod api_client; |
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters