dockerized successfully

This commit is contained in:
liyunze 2025-11-10 15:12:21 +11:00
parent cdffcd1b62
commit 71f9337794
6 changed files with 43 additions and 142 deletions

36
Dockerfile Normal file
View file

@ -0,0 +1,36 @@
FROM rust:trixie AS builder
WORKDIR /usr/src/dsec_bot
# Copy manifest files first to leverage Docker layer caching for dependencies
COPY Cargo.toml Cargo.lock ./
# Create a dummy main to cache dependency compilation
RUN mkdir -p src && echo 'fn main() {}' > src/main.rs
# Build dependencies (this layer will be cached)
RUN cargo build --release
# Remove dummy and copy real source
RUN rm -rf src
COPY . .
# Build the actual application
RUN cargo build --release
# Use a small image for the final build
FROM debian:bookworm-slim
# Set the working directory
WORKDIR /usr/local/bin
# Install CA certificates for TLS/HTTPS support
RUN apt-get update && \
apt-get install -y --no-install-recommends ca-certificates && \
rm -rf /var/lib/apt/lists/*
# Copy the application binary from the builder stage
COPY --from=builder /usr/src/dsec_bot/target/release/dsec_bot .
# Specify the command to run the application
CMD ["./dsec_bot"]

5
docker-compose.yml Normal file
View file

@ -0,0 +1,5 @@
services:
dsec_bot:
build: .
env_file:
- .env

View file

@ -1,3 +1,3 @@
// this file is to let main.rs know the existence of the "commands" folder // this file is to let main.rs know the existence of the "commands" folder
pub mod info; pub mod info;
pub mod weather; // pub mod weather;

View file

@ -1,58 +0,0 @@
use crate::{Context, Error};
use poise::CreateReply;
use serenity::{all::CreateEmbed, json::Value};
async fn get_weather(location: String) -> Result<String, Error> {
let weather_api_key = std::env::var("WEATHER_TOKEN").expect("missing WEATHER_TOKEN");
let request_url = format!(
"https://api.weatherapi.com/v1/current.json?key={key}&q={location}",
key = weather_api_key,
location = location
);
// retrieve weather data
let response = reqwest::get(request_url).await?.text().await?;
// process weather data
Ok(response)
}
/// Shows weather information
#[poise::command(slash_command)]
pub async fn weather(
ctx: Context<'_>,
#[description = "Location (City or Country)"] location: String,
) -> Result<(), Error> {
let weather_response = get_weather(location).await?;
let value: Value = serde_json::from_str(&weather_response)?;
let location_name = (&value["location"]["name"]).as_str().unwrap();
let location_region = (&value["location"]["region"]).as_str().unwrap();
let location_country = (&value["location"]["country"]).as_str().unwrap();
let weather_condition = (&value["current"]["condition"]["text"]).as_str().unwrap();
let weather_temp = &value["current"]["temp_c"];
let weather_feels_like = &value["current"]["feelslike_c"];
let weather_wind_kph = &value["current"]["wind_kph"];
let weather_humidity = &value["current"]["humidity"];
let weather_cloud = &value["current"]["cloud"];
let weather_icon = (&value["current"]["condition"]["icon"]).as_str().unwrap();
let embed = CreateEmbed::new()
.field("Name", format!("{}", location_name), true)
.field("Region", format!("{}", location_region), true)
.field("Country", format!("{}", location_country), true)
.field("Condition", format!("{}", weather_condition), true)
.field("Temperature", format!("{} °C", weather_temp), true)
.field("Feels like", format!("{} °C", weather_feels_like), true)
.field("wind", format!("{} kph", weather_wind_kph), true)
.field("humidity", format!("{}%", weather_humidity), true)
.field("cloud", format!("{}%", weather_cloud), true)
.thumbnail(format!("https:{}", weather_icon));
ctx.send(CreateReply::default().embed(embed)).await?;
Ok(())
}

View file

@ -22,7 +22,7 @@ async fn main() {
commands::info::ping(), commands::info::ping(),
commands::info::userinfo(), commands::info::userinfo(),
commands::info::serverinfo(), commands::info::serverinfo(),
commands::weather::weather(), // commands::weather::weather(),
], ],
..Default::default() ..Default::default()
}) })

View file

@ -1,82 +0,0 @@
[
{
"location": {
"name": "Melbourne",
"region": "Victoria",
"country": "Australia",
"lat": -37.8167,
"lon": 144.9667,
"tz_id": "Australia/Melbourne",
"localtime_epoch": 1762687144,
"localtime": "2025-11-09 22:19"
},
"current": {
"last_updated_epoch": 1762686900,
"last_updated": "2025-11-09 22:15",
"temp_c": 12.3,
"is_day": 0,
"condition": {
"text": "Partly cloudy",
"icon": "//cdn.weatherapi.com/weather/64x64/night/116.png",
"code": 1003
},
"wind_mph": 5.4,
"wind_kph": 8.6,
"wind_degree": 263,
"wind_dir": "W",
"pressure_mb": 1017.0,
"pressure_in": 30.03,
"precip_mm": 0.03,
"precip_in": 0.0,
"humidity": 71,
"cloud": 75,
"feelslike_c": 11.6,
"feelslike_f": 52.9,
"vis_km": 10.0,
"vis_miles": 6.0,
"uv": 0.0,
"gust_mph": 9.4,
"gust_kph": 15.1
}
},
{
"location": {
"name": "Canberra",
"region": "Australian Capital Territory",
"country": "Australia",
"lat": -35.283,
"lon": 149.217,
"tz_id": "Australia/Sydney",
"localtime_epoch": 1762687326,
"localtime": "2025-11-09 22:22"
},
"current": {
"last_updated_epoch": 1762686900,
"last_updated": "2025-11-09 22:15",
"temp_c": 12.3,
"is_day": 0,
"condition": {
"text": "Partly cloudy",
"icon": "//cdn.weatherapi.com/weather/64x64/night/116.png",
"code": 1003
},
"wind_mph": 4.9,
"wind_kph": 7.9,
"wind_degree": 74,
"wind_dir": "ENE",
"pressure_mb": 1015.0,
"pressure_in": 29.97,
"precip_mm": 0.0,
"precip_in": 0.0,
"humidity": 71,
"cloud": 50,
"feelslike_c": 11.7,
"feelslike_f": 53.1,
"vis_km": 10.0,
"vis_miles": 6.0,
"uv": 0.0,
"gust_mph": 7.5,
"gust_kph": 12.1
}
}
]