aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorJokler <jokler@protonmail.com>2020-07-27 01:17:30 +0200
committerJokler <jokler@protonmail.com>2020-09-01 18:52:46 +0200
commitbbe3e1fffc94e7e87237a331de7b09253b0aa3fb (patch)
tree6092b3db497ee0a795f70db695ff2adb3c16e5ee /src/main.rs
parent130cde033795382b70a312846a8f2704a15d11e3 (diff)
downloadpokebot-bbe3e1fffc94e7e87237a331de7b09253b0aa3fb.tar.gz
pokebot-bbe3e1fffc94e7e87237a331de7b09253b0aa3fb.zip
Upgrade dependencies & use tokio 0.2 exclusively
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs99
1 files changed, 43 insertions, 56 deletions
diff --git a/src/main.rs b/src/main.rs
index c8c93a4..f755db0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,10 +2,8 @@ use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;
use std::thread;
-use std::time::{Duration, Instant};
+use std::time::Duration;
-use futures::compat::Future01CompatExt;
-use futures::future::{FutureExt, TryFutureExt};
use log::{debug, error, info};
use structopt::clap::AppSettings;
use structopt::StructOpt;
@@ -22,7 +20,7 @@ mod youtube_dl;
use bot::{MasterArgs, MasterBot, MusicBot, MusicBotArgs};
#[derive(StructOpt, Debug)]
-#[structopt(raw(global_settings = "&[AppSettings::ColoredHelp]"))]
+#[structopt(global_settings = &[AppSettings::ColoredHelp])]
pub struct Args {
#[structopt(short = "l", long = "local", help = "Run locally in text mode")]
local: bool,
@@ -66,13 +64,14 @@ pub struct Args {
// 3. Print udp packets
}
-fn main() {
- if let Err(e) = run() {
+#[tokio::main]
+async fn main() {
+ if let Err(e) = run().await {
println!("Error: {}", e);
}
}
-fn run() -> Result<(), Box<dyn std::error::Error>> {
+async fn run() -> Result<(), Box<dyn std::error::Error>> {
log4rs::init_file("log4rs.yml", Default::default()).unwrap();
// Parse command line options
@@ -137,56 +136,44 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
info!("Starting PokeBot!");
debug!("Received CLI arguments: {:?}", std::env::args());
- tokio::runtime::Runtime::new()?
- .block_on(
- async {
- if bot_args.local {
- let name = bot_args.names[0].clone();
- let id = bot_args.ids.expect("identies should exists")[0].clone();
-
- let disconnect_cb = Box::new(move |_, _, _| {});
-
- let bot_args = MusicBotArgs {
- name,
- name_index: 0,
- id_index: 0,
- local: true,
- address: bot_args.address.clone(),
- id,
- channel: String::from("local"),
- verbose: bot_args.verbose,
- disconnect_cb,
- };
- MusicBot::new(bot_args).await.1.await;
- } else {
- let domain = bot_args.domain.clone();
- let bind_address = bot_args.bind_address.clone();
- let (bot, fut) = MasterBot::new(bot_args).await;
-
- thread::spawn(|| {
- let web_args = web_server::WebServerArgs {
- domain,
- bind_address,
- bot,
- };
- if let Err(e) = web_server::start(web_args) {
- error!("Error in web server: {}", e);
- }
- });
-
- fut.await;
- // Keep tokio running while the bot disconnects
- tokio::timer::Delay::new(Instant::now() + Duration::from_secs(1))
- .compat()
- .await
- .expect("Failed to wait for delay");
- }
+ if bot_args.local {
+ let name = bot_args.names[0].clone();
+ let id = bot_args.ids.expect("identies should exists")[0].clone();
+
+ let disconnect_cb = Box::new(move |_, _, _| {});
+
+ let bot_args = MusicBotArgs {
+ name,
+ name_index: 0,
+ id_index: 0,
+ local: true,
+ address: bot_args.address.clone(),
+ id,
+ channel: String::from("local"),
+ verbose: bot_args.verbose,
+ disconnect_cb,
+ };
+ MusicBot::new(bot_args).await.1.await;
+ } else {
+ let domain = bot_args.domain.clone();
+ let bind_address = bot_args.bind_address.clone();
+ let (bot, fut) = MasterBot::new(bot_args).await;
+
+ thread::spawn(|| {
+ let web_args = web_server::WebServerArgs {
+ domain,
+ bind_address,
+ bot,
+ };
+ if let Err(e) = web_server::start(web_args) {
+ error!("Error in web server: {}", e);
}
- .unit_error()
- .boxed()
- .compat(),
- )
- .expect("Runtime exited on an error");
+ });
+
+ fut.await;
+ // Keep tokio running while the bot disconnects
+ tokio::time::delay_for(Duration::from_secs(1)).await;
+ }
Ok(())
}