mirror of
https://github.com/mii443/rs-docker-bot.git
synced 2025-08-23 00:25:30 +00:00
add poise client init
This commit is contained in:
60
src/main.rs
60
src/main.rs
@ -3,10 +3,9 @@ mod docker;
|
|||||||
mod event_handler;
|
mod event_handler;
|
||||||
mod language;
|
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 config::Config;
|
||||||
use event_handler::Handler;
|
|
||||||
|
|
||||||
use poise::{
|
use poise::{
|
||||||
serenity_prelude::{self as serenity, futures::lock::Mutex, UserId},
|
serenity_prelude::{self as serenity, futures::lock::Mutex, UserId},
|
||||||
@ -17,7 +16,7 @@ type Error = Box<dyn std::error::Error + Send + Sync>;
|
|||||||
type Context<'a> = poise::Context<'a, Data, Error>;
|
type Context<'a> = poise::Context<'a, Data, Error>;
|
||||||
|
|
||||||
pub struct Data {
|
pub struct Data {
|
||||||
pub context: Arc<Mutex<Config>>,
|
pub config: Arc<Mutex<Config>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_config() -> Option<Config> {
|
fn load_config() -> Option<Config> {
|
||||||
@ -43,40 +42,33 @@ async fn main() -> Result<(), ()> {
|
|||||||
|
|
||||||
let config = load_config().unwrap();
|
let config = load_config().unwrap();
|
||||||
|
|
||||||
let token = &config.token;
|
let intents =
|
||||||
let http = Http::new(token);
|
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
|
||||||
|
|
||||||
let bot_id = match http.get_current_user().await {
|
let framework = poise::Framework::builder()
|
||||||
Ok(bot_id) => bot_id.id,
|
.setup(move |_ctx, _ready, _framework| {
|
||||||
Err(why) => panic!("Could not access the bot id: {:?}", why),
|
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();
|
let client = serenity::ClientBuilder::new(config.token, intents)
|
||||||
framework.configure(|c| {
|
.framework(framework)
|
||||||
c.with_whitespace(true)
|
.await;
|
||||||
.on_mention(Some(bot_id))
|
|
||||||
.prefix(config.prefix.clone())
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut client = Client::builder(
|
client.unwrap().start().await.unwrap();
|
||||||
&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::<ConfigStorage>(Arc::new(Mutex::new(config)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(why) = client.start().await {
|
|
||||||
println!("Client error: {:?}", why);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user