Skip to content

Commit

Permalink
Merge pull request #7 from tricities-dev/feature/vertex-bot-environments
Browse files Browse the repository at this point in the history
Info about bot environments, added configuration parsing, tests for bot startup
  • Loading branch information
RyannosaurusRex authored Nov 18, 2023
2 parents 9d9ec6d + 6fc966a commit 6119e21
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 2 deletions.
120 changes: 120 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.

## 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!
53 changes: 51 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 6119e21

Please sign in to comment.