use super::cmd::{help::HELP, ping::PING_COMMAND}; use super::handler::Handler; use crate::config::{Config, ConfigGlobal}; use crate::img::config_file::{ConfigFile, ConfigImgGlobal}; use serenity::framework::standard::Configuration; use serenity::{ all::GatewayIntents, framework::{standard::macros::group, StandardFramework}, http::Http, Client, }; use std::collections::HashSet; use std::fs; use tokio::task::spawn_blocking; #[group] #[commands(ping)] struct General; pub fn start_bot(config: Config) { if config.token != "" { spawn_blocking(move || { let rt = tokio::runtime::Handle::current(); rt.block_on(async move { let local = tokio::task::LocalSet::new(); let _ = local .run_until(async move { let config_img = match fs::read_to_string(format!( "{}/config.yaml", config.image.path )) { Ok(content) => content, Err(err) => { println!("Error while opening config.yaml : {:?}", err); return; } }; let config_parsed = match ConfigFile::parse_config(config_img) { Ok(config) => config, Err(err) => { println!("Error while parsing config.yaml : {:?}", err); return; } }; let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::DIRECT_MESSAGES | GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_VOICE_STATES | GatewayIntents::GUILDS | GatewayIntents::GUILD_MEMBERS | GatewayIntents::GUILD_PRESENCES | GatewayIntents::GUILD_MESSAGE_REACTIONS; let http = Http::new(&config.token); let (owners, _bot_id) = match http.get_current_application_info().await { Ok(info) => { let mut owners = HashSet::new(); match info.owner { Some(user) => { owners.insert(user.id); println!("{}", user.global_name.unwrap()) } None => {} } (owners, info.id) } Err(why) => panic!("Could not access application info: {:?}", why), }; let framework = StandardFramework::new().help(&HELP).group(&GENERAL_GROUP); framework.configure( Configuration::new() .with_whitespace(true) .prefix(config.prefix.clone()) .owners(owners), ); let mut client = Client::builder(&config.token, intents) .event_handler(Handler) .framework(framework) .await .expect("Err creating client"); { // Open the data lock in write mode, so keys can be inserted to it. let mut data = client.data.write().await; data.insert::(config_parsed.clone()); data.insert::(config.clone()); } if let Err(why) = client.start().await { println!("Client error: {why:?}"); } }) .await; }) }); } }