Skip to content

Commit

Permalink
Start Discord bot
Browse files Browse the repository at this point in the history
  • Loading branch information
Celeo committed Jul 7, 2024
1 parent cbfb8ab commit d96ad4a
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions vzdv-bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ vzdv = { path = "../vzdv" }

anyhow = "1.0.86"
base64 = "0.22.1"
clap = { version = "4.5.1", features = ["derive"] }
log = "0.4.22"
pretty_env_logger = "0.5.0"
rand = "0.8.5"
sqlx = { version = "0.7.4", features = ["runtime-tokio", "sqlite", "chrono", "uuid"] }
tokio = { version = "1.38.0", features = ["full"] }
twilight-gateway = "0.15.4"
twilight-http = "0.15.4"
Expand Down
128 changes: 127 additions & 1 deletion vzdv-bot/src/main.rs
Original file line number Diff line number Diff line change
@@ -1 +1,127 @@
fn main() {}
//! vZDV Discord bot.
#![deny(clippy::all)]
#![deny(unsafe_code)]

use anyhow::Result;
use base64::{engine::general_purpose, Engine as _};
use clap::Parser;
use log::{debug, error, info, warn};
use sqlx::{Pool, Sqlite};
use std::{
env,
path::{Path, PathBuf},
sync::Arc,
};
use twilight_gateway::{Event, Intents, Shard, ShardId};
use twilight_http::Client as HttpClient;
use vzdv::{
config::{self, Config},
db::load_db,
};

/// vZDV Discord bot.
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// Load the config from a specific file.
///
/// [default: vzdv.toml]
#[arg(long)]
config: Option<PathBuf>,

/// Enable debug logging
#[arg(short, long)]
debug: bool,
}

/// Parse a bot ID from the token.
///
/// This function panics instead of returning a Result, as the token
/// must confirm to this layout in order to be valid for Discord.
fn bot_id_from_token(token: &str) -> u64 {
std::str::from_utf8(
&general_purpose::STANDARD_NO_PAD
.decode(token.split('.').next().unwrap())
.unwrap(),
)
.unwrap()
.parse()
.unwrap()
}

/// Entrypoint.
#[tokio::main]
async fn main() {
let cli = Cli::parse();
if cli.debug {
env::set_var("RUST_LOG", "info,vzdv_bot=debug");
} else if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "info");
}
pretty_env_logger::init();
debug!("Logging configured");

debug!("Loading");
let config_location = match cli.config {
Some(path) => path,
None => Path::new(config::DEFAULT_CONFIG_FILE_NAME).to_owned(),
};
debug!("Loading from config file");
let config = match Config::load_from_disk(&config_location) {
Ok(c) => Arc::new(c),
Err(e) => {
error!("Could not load config: {e}");
std::process::exit(1);
}
};
debug!("Creating DB connection");
let db = match load_db(&config).await {
Ok(db) => db,
Err(e) => {
error!("Could not load DB: {e}");
std::process::exit(1);
}
};

let token = &config.discord.bot_token;
let bot_id = bot_id_from_token(token);
let intents = Intents::GUILD_MESSAGES | Intents::MESSAGE_CONTENT;
let mut shard = Shard::new(ShardId::ONE, token.clone(), intents);
let http = Arc::new(HttpClient::new(token.clone()));

// TODO start background tasks

info!("Waiting for events");
loop {
let event = match shard.next_event().await {
Ok(event) => event,
Err(source) => {
warn!("Error receiving event: {:?}", source);
if source.is_fatal() {
break;
}
continue;
}
};
let http = http.clone();
let config = config.clone();
let db: Pool<Sqlite> = db.clone();
tokio::spawn(async move {
if let Err(e) = handle_event(event, http, bot_id, &config, &db).await {
error!("Error in future: {e}");
}
});
}
}

async fn handle_event(
event: Event,
http: Arc<HttpClient>,
bot_id: u64,
config: &Config,
db: &Pool<Sqlite>,
) -> Result<()> {
// TODO
Ok(())
}
1 change: 1 addition & 0 deletions vzdv/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub struct ConfigDiscordRoles {
pub home_controller: String,
pub visiting_controller: String,
pub neighboring_controller: String,
pub event_controller: String,

// staff
pub sr_staff: String,
Expand Down

0 comments on commit d96ad4a

Please sign in to comment.