diff options
| author | Nathan Perry <avaglir@gmail.com> | 2018-02-13 19:35:45 -0500 |
|---|---|---|
| committer | Nathan Perry <avaglir@gmail.com> | 2018-02-13 19:35:45 -0500 |
| commit | ef6e70691aa719bb7166619795aa15bbd9734f65 (patch) | |
| tree | c83c6892288b9baccff3c8b19c5c0a9149ee4214 /src | |
| parent | 85f52b253776ff4d9ce66839320c00fdabcda44d (diff) | |
first pass on initial functionality
Diffstat (limited to 'src')
| -rw-r--r-- | src/commands.rs | 6 | ||||
| -rw-r--r-- | src/main.rs | 126 | ||||
| -rw-r--r-- | src/util.rs | 20 |
3 files changed, 151 insertions, 1 deletions
diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..e802bde --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,6 @@ +//use serenity::prelude::*; +//use serenity::framework::StandardFramework; +// +//pub fn register_commands(mut f: StandardFramework) -> StandardFramework { +// f +//} diff --git a/src/main.rs b/src/main.rs index e7a11a9..0f7b22b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,127 @@ +#[macro_use] extern crate serenity; +#[macro_use] extern crate log; +#[macro_use] extern crate error_chain; +#[macro_use] extern crate dotenv_codegen; + +extern crate dotenv; +extern crate simple_logger; +extern crate typemap; +extern crate url; + +mod commands; +mod util; + +mod errors { + error_chain!(); +} + +use errors::*; + +pub use util::*; + +use std::env; +use std::collections::HashSet; +use std::thread; +use std::time::{Duration, Instant}; + +use serenity::prelude::*; +use serenity::framework::StandardFramework; +use serenity::framework::standard::help_commands; +use serenity::model::gateway::Ready; + +use dotenv::dotenv; + +struct Handler; +impl EventHandler for Handler { + fn ready(&self, _c: Context, r: Ready) { + r.guilds.iter().find(|g| { + g.id().0 == 0 + }).or_else(|| { + info!("bot isn't in configured guild. let it join here: {}", OAUTH_URL); + }); + } +} + +fn run() -> Result<()> { + let token = &env::var("DISCORD_TOKEN")?; + let mut client = Client::new(token, Handler)?; + let framework = StandardFramework::new() + .configure(|c| c + .allow_dm(false) + .allow_whitespace(true) + .prefixes(vec!["!thulani ", "!thulan ", "!thulando madando ", "!thulando "]) + .ignore_bots(true) + .on_mention(false) + .owners(HashSet::new()) + .case_insensitivity(true) + ) + .before(|_ctx, message, cmd| { + debug!("got command {} from user '{}' ({})", cmd, message.author.name, message.author.id); + + true + }) + .after(|_ctx, _msg, cmd, err| { + match err { + Ok(()) => {}, + Err(e) => { + + + } + } + }) + .bucket("std", 1, 10, 3) + .customised_help(help_commands::with_embeds, |c| { + c + }); + + client.with_framework(framework); + client.start()?; + + Ok(()) +} + fn main() { - println!("Hello, world!"); + const BACKOFF_FACTOR: f64 = 2.0; + const MAX_BACKOFFS: usize = 3; + const BACKOFF_INIT: f64 = 100.0; + + const MIN_RUN_DURATION: Duration = Duration::from_secs(120); + + dotenv().ok(); + simple_logger::init().unwrap(); + + let mut backoff_count: usize = 0; + + loop { + let start = Instant::now(); + + info!("starting bot"); + match run() { + Err(e) => { + error!("error encountered running client: {}", e); + e.iter().skip(1).for_each(|e| { + + }); + + }, + _ => { + warn!("somehow `run` completed without an error. should probably take a look at this."); + } + } + + if Instant::now() - start >= MIN_RUN_DURATION { + backoff_count = 0; + continue; + } + + backoff_count += 1; + if backoff_count >= 3 { + panic!("restarted bot too many times"); + } + + let backoff_millis = (BACKOFF_INIT*BACKOFF_FACTOR.powi(backoff_count as i32)) as u64; + info!("bot died too quickly. backing off, retrying in {}ms.", backoff_millis); + + thread::sleep(Duration::from_millis(backoff_millis)); + } } diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..7903771 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,20 @@ +use serenity::model::permissions::Permissions; +use url::Url; + +const REQUIRED_PERMS: Permissions = Permissions::EMBED_LINKS | + Permissions::READ_MESSAGES | + Permissions::ADD_REACTIONS | + Permissions::SEND_MESSAGES | + Permissions::SEND_TTS_MESSAGES | + Permissions::MENTION_EVERYONE | + Permissions::USE_EXTERNAL_EMOJIS | + Permissions::CONNECT | + Permissions::SPEAK | + Permissions::CHANGE_NICKNAME | + Permissions::USE_VAD | + Permissions::ATTACH_FILES; + +pub const OAUTH_URL: Url = Url::parse( + concat!("https://discordapp.com/api/oauth2/authorize?scope=bot", + "&permissions=", stringify!(REQUIRED_PERMS.bits()), + "&client_id=", dotenv!("THULANI_CLIENT_ID"))).unwrap(); |
