From 5dd1b88de4b447c57856498bd66dce58a2308cc1 Mon Sep 17 00:00:00 2001 From: mii443 Date: Tue, 8 Oct 2024 10:09:02 +0000 Subject: [PATCH] add poise client init --- src/main.rs | 60 +++++++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/src/main.rs b/src/main.rs index 98bbc20..7b6d80e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,9 @@ mod docker; mod event_handler; mod language; -use std::{env, fs::File, io::Read, sync::Arc}; +use std::{collections::HashSet, env, fs::File, io::Read, sync::Arc}; use config::Config; -use event_handler::Handler; use poise::{ serenity_prelude::{self as serenity, futures::lock::Mutex, UserId}, @@ -17,7 +16,7 @@ type Error = Box; type Context<'a> = poise::Context<'a, Data, Error>; pub struct Data { - pub context: Arc>, + pub config: Arc>, } fn load_config() -> Option { @@ -43,40 +42,33 @@ async fn main() -> Result<(), ()> { let config = load_config().unwrap(); - let token = &config.token; - let http = Http::new(token); + let intents = + serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT; - let bot_id = match http.get_current_user().await { - Ok(bot_id) => bot_id.id, - Err(why) => panic!("Could not access the bot id: {:?}", why), - }; + let framework = poise::Framework::builder() + .setup(move |_ctx, _ready, _framework| { + Box::pin(async move { + Ok(Data { + config: Arc::new(Mutex::new(config.clone())), + }) + }) + }) + .options(poise::FrameworkOptions { + commands: vec![], + prefix_options: PrefixFrameworkOptions { + prefix: Some(config.prefix), + ..Default::default() + }, + owners: HashSet::from([UserId::new(config.owner)]), + ..Default::default() + }) + .build(); - let framework = StandardFramework::new(); - framework.configure(|c| { - c.with_whitespace(true) - .on_mention(Some(bot_id)) - .prefix(config.prefix.clone()) - }); + let client = serenity::ClientBuilder::new(config.token, intents) + .framework(framework) + .await; - let mut client = Client::builder( - &token, - GatewayIntents::MESSAGE_CONTENT - | GatewayIntents::GUILD_MESSAGES - | GatewayIntents::DIRECT_MESSAGES, - ) - .framework(framework) - .event_handler(Handler) - .await - .expect("Err creating client"); - - { - let mut data = client.data.write().await; - data.insert::(Arc::new(Mutex::new(config))); - } - - if let Err(why) = client.start().await { - println!("Client error: {:?}", why); - } + client.unwrap().start().await.unwrap(); Ok(()) }