From 4d99adc22f74cc7c0b998b383f66eadf59649d70 Mon Sep 17 00:00:00 2001 From: jaketothepast Date: Fri, 17 Nov 2023 20:43:10 -0500 Subject: [PATCH 1/4] Started README.md --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e512cc5 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Vertex + +A Discord bot for the best Discord community out there, TriDev. Vertex comes jam packed with features that make your community come alive. Curl up by a nice Flame War Friday, and enjoy the ride + +## Getting Started + +1. Install the Rust toolchain for your system: https://www.rust-lang.org/learn/get-started +2. Open an issue for @jaketothepast to add you to the Discord developer team. +3. Compile and run the bot locally, with the correct Discord bot token. + +## Discord Environments + +We currently have two applications + +1. Vertex - the production app, used to power our prod bot user. +2. Vertex Test - our private app, used as a staging ground for new bot features. From ea030e8e5d236970effe5031a586d173ce67da05 Mon Sep 17 00:00:00 2001 From: jaketothepast Date: Fri, 17 Nov 2023 20:43:10 -0500 Subject: [PATCH 2/4] Started README.md Test server info, production info --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index e512cc5..f1b74e4 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,9 @@ We currently have two applications 1. Vertex - the production app, used to power our prod bot user. 2. Vertex Test - our private app, used as a staging ground for new bot features. + +We also have the test server: Vertex-Test. This server is used as our development environment. + +## Production + +Currently running in DigitalOcean. From 0efa03078766949afdf327d2fd5fdcd39fc4f5fa Mon Sep 17 00:00:00 2001 From: jaketothepast Date: Fri, 17 Nov 2023 21:43:23 -0500 Subject: [PATCH 3/4] Remove discord platform info --- README.md | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f1b74e4..b72b810 100644 --- a/README.md +++ b/README.md @@ -8,15 +8,12 @@ A Discord bot for the best Discord community out there, TriDev. Vertex comes jam 2. Open an issue for @jaketothepast to add you to the Discord developer team. 3. Compile and run the bot locally, with the correct Discord bot token. -## Discord Environments - -We currently have two applications - -1. Vertex - the production app, used to power our prod bot user. -2. Vertex Test - our private app, used as a staging ground for new bot features. - -We also have the test server: Vertex-Test. This server is used as our development environment. - -## Production - -Currently running in DigitalOcean. +## Running the Bot + +1. Go to Discord developer portal +2. Create a new application +3. Create a new bot user +4. Generate the invite link. +5. Add the bot to your server with the link. +6. Use the bot token as an environment variable `BOT_TOKEN` to start the app +7. Start the bot, profit! From 0570daa5acf6ec823122c37206893d0e7de66ae2 Mon Sep 17 00:00:00 2001 From: jaketothepast Date: Sat, 18 Nov 2023 07:10:12 -0500 Subject: [PATCH 4/4] Add configuration parsing, basic token validation and testing --- Cargo.lock | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/main.rs | 53 ++++++++++++++++++++++- 3 files changed, 173 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9bb362e..564427b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,54 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "antidote" version = "1.0.0" @@ -123,6 +171,52 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "clap" +version = "4.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2275f18819641850fa26c89acc84d465c1bf91ce57bc2748b28c420473352f64" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07cdf1b148b25c1e1f7a42225e30a0d99a615cd4637eae7365548dd4529b95bc" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "core-foundation" version = "0.9.3" @@ -170,6 +264,12 @@ dependencies = [ "websocket", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "ed25519" version = "1.5.3" @@ -237,6 +337,12 @@ dependencies = [ "wasi 0.9.0+wasi-snapshot-preview1", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.3" @@ -803,6 +909,12 @@ dependencies = [ "libsodium-sys", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "2.0.39" @@ -915,6 +1027,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "vcpkg" version = "0.2.15" @@ -937,7 +1055,9 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" name = "vertex" version = "0.1.0" dependencies = [ + "clap", "discord", + "dotenv", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 9a799e3..ee3bd2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = { version = "4.4.8", features = ["derive", "env"] } discord = { git = "https://github.com/SpaceManiac/discord-rs" } +dotenv = "0.15.0" diff --git a/src/main.rs b/src/main.rs index de5c462..08df917 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,57 @@ use discord::Discord; +use clap::Parser; +use dotenv::dotenv; extern crate discord; +// TODO: Better validation of Discord token. +const BOT_TOKEN_LENGTH: usize = 72; + +/// Tri-Cities Vertex Discord Bot +#[derive(Parser, Debug)] +#[command(author, version, about)] +pub struct Config { + /// Your bot token you got from Discord Developer portal. + #[arg(short, long, env("BOT_TOKEN"))] + pub bot_token: String +} + +impl Config { + pub fn from_env_and_args() -> Self { + dotenv().ok(); + Self::parse() + } +} + +fn get_discord(bot_token: &str) -> Discord { + if bot_token.len() < BOT_TOKEN_LENGTH { + panic!("invalid bot token"); + } + + return match Discord::from_bot_token(bot_token) { + Ok(discord) => discord, + Err(err) => panic!("error happened: {}", err) + } +} + fn main() { - println!("Hello, world!"); - let discord = Discord::from_bot_token("my_token"); + let cfg = Config::from_env_and_args(); + let discord = get_discord(cfg.bot_token.as_str()); +} + +#[cfg(test)] +mod tests { + use crate::get_discord; + + #[test] + #[should_panic] + fn invalid_bot_token_errors() { + get_discord("invalid"); + } + + #[test] + fn valid_bot_token_no_error() { + // Our only validation rule right now is length + get_discord("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + } }