Skip to content

Commit

Permalink
feat: use shuttle runtime to run web server and discord bot
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Apr 1, 2024
1 parent 543ecb8 commit b892dc7
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 127 deletions.
1 change: 0 additions & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ jobs:
no-test: "true"
secrets: |
DISCORD_TOKEN = '${{ secrets.DISCORD_TOKEN }}'
HOST = '${{ secrets.HOST}}'
GUILD_ID = '${{ secrets.GUILD_ID }}'
119 changes: 78 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ readme = "README.md"

[dependencies]
anyhow = "1.0.66"
shuttle-serenity = "0.42.0"
shuttle-runtime = "0.42.0"
serenity = { version = "=0.12.0", default-features = true, features = [
"cache",
Expand Down Expand Up @@ -44,7 +43,7 @@ symphonia = { version = "0.5.3", default-features = false, features = [
"ogg",
"wav",
] }
tiny_http = "0.12.0"
axum = "0.7.5"

[dependencies.parking_lot]
version = "0.12"
Expand Down
6 changes: 1 addition & 5 deletions src/config/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ pub async fn setup(
return Err(anyhow!("'GUILD_ID' was not found"));
};

let Some(host) = secret_store.get("HOST") else {
return Err(anyhow!("'HOST' was not found"));
};

let handler = Handler::new(guild_id.parse().unwrap(), host);
let handler = Handler::new(guild_id.parse().unwrap());

let client = Client::builder(token, intents)
.event_handler(handler)
Expand Down
58 changes: 2 additions & 56 deletions src/config/slash_command_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use serenity::{
model::prelude::{GuildId, Interaction, Member, Ready},
prelude::{Context, EventHandler},
};
use tiny_http::Method;
use tracing::{error, log::info};

use crate::slash_commands::ping;
Expand All @@ -23,17 +22,11 @@ use slash_commands::sugerencia;

pub struct Handler {
guild_id: u64,
host: String,
http_running: AtomicBool,
}

impl Handler {
pub fn new(guild_id: u64, host: String) -> Self {
Self {
host,
guild_id,
http_running: AtomicBool::new(false),
}
pub fn new(guild_id: u64) -> Self {
Self { guild_id }
}
}

Expand Down Expand Up @@ -104,53 +97,6 @@ impl EventHandler for Handler {
)
.await;

let ctx = Arc::new(ctx);

if !self.http_running.load(Ordering::Relaxed) {
let ctx1 = Arc::clone(&ctx);
let host = self.host.clone();

// start http server
tokio::spawn(async move {
let server = tiny_http::Server::http(host).unwrap();

tracing::debug!("Listening on {:?}", server.server_addr());

while let Ok(mut req) = server.recv() {
println!("Request: {req:?}");
// Ejecuta el servidor
match (req.method(), req.url()) {
(Method::Post, "/daily_challenge") => {
let reader = req.as_reader();
let Ok(data) = serde_json::from_reader(reader) else {
tracing::error!("Failed load json from reader");
continue;
};
match run_daily_challenge(&ctx1, &data).await {
Ok(()) => {
tracing::debug!("Success send daily");
let r = req.respond(tiny_http::Response::empty(200));
tracing::debug!("Response sended: {r:?}");
}
Err(e) => {
tracing::error!("Cannot send daily: {e:?}");
let r = req.respond(
tiny_http::Response::from_string(e.to_string())
.with_status_code(400),
);
tracing::debug!("Response sended: {r:?}");
}
}
}
_ => {}
}
}
});

// Now that the loop is running, we set the bool to true
self.http_running.swap(true, Ordering::Relaxed);
}

// info!("I created the following global slash command: {:#?}", guild_command);
}
}
Loading

0 comments on commit b892dc7

Please sign in to comment.