119 lines
3.2 KiB
Rust
119 lines
3.2 KiB
Rust
#[macro_use] extern crate rocket;
|
|
|
|
use std::{sync::{Arc, Mutex}};
|
|
|
|
use clap::Parser;
|
|
use dotenvy::dotenv;
|
|
use poise::serenity_prelude as serenity;
|
|
use squad_quest::config::Config;
|
|
|
|
use crate::{commands::{error_handler, print_error_recursively}, config::{ConfigImpl, DiscordConfig}, error::Error, strings::Strings};
|
|
|
|
mod api;
|
|
mod commands;
|
|
mod cli;
|
|
mod config;
|
|
mod account;
|
|
mod error;
|
|
mod strings;
|
|
mod timer;
|
|
|
|
const CONFIG_PATH: &str = "cfg/config.toml";
|
|
const DISCORD_TOKEN: &str = "DISCORD_TOKEN";
|
|
|
|
#[derive(Debug)]
|
|
struct InnerBool {
|
|
pub value: bool,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct Data {
|
|
pub config: Config,
|
|
pub discord: Arc<Mutex<DiscordConfig>>,
|
|
pub strings: Strings,
|
|
pub timer_set: Arc<Mutex<InnerBool>>,
|
|
}
|
|
|
|
impl Data {
|
|
pub fn timer(&self) {
|
|
let tm = self.timer_set.clone();
|
|
{
|
|
let mut guard = tm.lock().unwrap();
|
|
guard.value = true;
|
|
}
|
|
}
|
|
|
|
pub fn has_timer(&self) -> bool {
|
|
let tm = self.timer_set.clone();
|
|
{
|
|
let guard = tm.lock().unwrap();
|
|
guard.value
|
|
}
|
|
}
|
|
}
|
|
|
|
type Context<'a> = poise::Context<'a, Data, Error>;
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
dotenv().unwrap();
|
|
|
|
let cli = cli::Cli::parse();
|
|
let config = Config::load(cli.config.clone().unwrap_or(CONFIG_PATH.into()));
|
|
let (discord, strings) = config.discord_impl().unwrap_or_else(|_| {
|
|
config.init_impl().unwrap();
|
|
config.discord_impl().unwrap()
|
|
});
|
|
|
|
let token = std::env::var(DISCORD_TOKEN).expect("missing DISCORD_TOKEN");
|
|
let intents = serenity::GatewayIntents::non_privileged();
|
|
|
|
let conf1 = config.clone();
|
|
tokio::spawn(async {
|
|
if let Err(error) = api::rocket(conf1).launch().await {
|
|
eprintln!("ERROR ON API LAUNCH");
|
|
print_error_recursively(&error);
|
|
}
|
|
});
|
|
|
|
let framework = poise::Framework::builder()
|
|
.options(poise::FrameworkOptions {
|
|
on_error: |err| Box::pin(error_handler(err)),
|
|
commands: vec![
|
|
commands::register(),
|
|
commands::quest::quest(),
|
|
commands::info(),
|
|
commands::init::init(),
|
|
commands::init::timer(),
|
|
commands::answer::answer(),
|
|
commands::social::social(),
|
|
commands::account::scoreboard(),
|
|
commands::account::balance(),
|
|
commands::account::reset(),
|
|
commands::map::unlock(),
|
|
commands::map::r#move(),
|
|
commands::map::avatar(),
|
|
],
|
|
..Default::default()
|
|
})
|
|
.setup(|_ctx, _ready, _framework| {
|
|
Box::pin(async move {
|
|
//poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
|
|
|
|
|
Ok(Data {
|
|
config,
|
|
discord: Arc::new(Mutex::new(discord)),
|
|
timer_set: Arc::new(Mutex::new(InnerBool { value: false })),
|
|
strings,
|
|
})
|
|
})
|
|
})
|
|
.build();
|
|
|
|
let client = serenity::ClientBuilder::new(token, intents)
|
|
.framework(framework)
|
|
.await;
|
|
client.unwrap().start().await.unwrap();
|
|
}
|
|
|