feat: extract the start bot part and init work on the framework
This commit is contained in:
parent
4b4691690e
commit
a54d14783a
34
Cargo.lock
generated
34
Cargo.lock
generated
@ -472,6 +472,17 @@ dependencies = [
|
|||||||
"windows-targets 0.48.5",
|
"windows-targets 0.48.5",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "command_attr"
|
||||||
|
version = "0.5.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "32f08c85a02e066b7b4f7dcb60eee6ae0793ef7d6452a3547d1f19665df070a9"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 1.0.109",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "convert_case"
|
name = "convert_case"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
@ -1073,6 +1084,12 @@ version = "1.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "levenshtein"
|
||||||
|
version = "1.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "db13adb97ab515a3691f56e4dbab09283d0b86cb45abd991d8634a9d6f501760"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.152"
|
version = "0.2.152"
|
||||||
@ -1840,20 +1857,25 @@ dependencies = [
|
|||||||
"base64",
|
"base64",
|
||||||
"bitflags 2.4.1",
|
"bitflags 2.4.1",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"command_attr",
|
||||||
"flate2",
|
"flate2",
|
||||||
"futures",
|
"futures",
|
||||||
|
"levenshtein",
|
||||||
"mime_guess",
|
"mime_guess",
|
||||||
|
"parking_lot",
|
||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"secrecy",
|
"secrecy",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"static_assertions",
|
||||||
"time",
|
"time",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-tungstenite",
|
"tokio-tungstenite",
|
||||||
"tracing",
|
"tracing",
|
||||||
"typemap_rev",
|
"typemap_rev",
|
||||||
"url",
|
"url",
|
||||||
|
"uwl",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1958,6 +1980,12 @@ version = "0.9.8"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "static_assertions"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "stringprep"
|
name = "stringprep"
|
||||||
version = "0.1.4"
|
version = "0.1.4"
|
||||||
@ -2428,6 +2456,12 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "uwl"
|
||||||
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f4bf03e0ca70d626ecc4ba6b0763b934b6f2976e8c744088bb3c1d646fbb1ad0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "vcpkg"
|
name = "vcpkg"
|
||||||
version = "0.2.15"
|
version = "0.2.15"
|
||||||
|
@ -35,6 +35,8 @@ serenity = { version = "0.12", default-features = false, features = [
|
|||||||
"gateway",
|
"gateway",
|
||||||
"rustls_backend",
|
"rustls_backend",
|
||||||
"model",
|
"model",
|
||||||
|
"framework",
|
||||||
|
"standard_framework",
|
||||||
] }
|
] }
|
||||||
tokio = { version = "1.35", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1.35", features = ["macros", "rt-multi-thread"] }
|
||||||
|
|
||||||
|
26
src/bot/handler.rs
Normal file
26
src/bot/handler.rs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
use serenity::{
|
||||||
|
all::{Message, Ready},
|
||||||
|
async_trait,
|
||||||
|
client::{Context, EventHandler},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Handler;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl EventHandler for Handler {
|
||||||
|
async fn message(&self, ctx: Context, msg: Message) {
|
||||||
|
if msg.author.bot {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if msg.content == "!ping" {
|
||||||
|
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
|
||||||
|
println!("Error sending message: {:?}", why);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn ready(&self, _: Context, ready: Ready) {
|
||||||
|
println!("{} is connected!", ready.user.name);
|
||||||
|
}
|
||||||
|
}
|
49
src/bot/init.rs
Normal file
49
src/bot/init.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
use super::handler::Handler;
|
||||||
|
use crate::config::Config;
|
||||||
|
use serenity::{all::GatewayIntents, http::Http, Client};
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
|
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 intents = GatewayIntents::GUILD_MESSAGES
|
||||||
|
| GatewayIntents::DIRECT_MESSAGES
|
||||||
|
| GatewayIntents::MESSAGE_CONTENT
|
||||||
|
| GatewayIntents::GUILD_VOICE_STATES
|
||||||
|
| GatewayIntents::GUILDS
|
||||||
|
| GatewayIntents::GUILD_MEMBERS;
|
||||||
|
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 mut client = Client::builder(&config.token, intents)
|
||||||
|
.event_handler(Handler)
|
||||||
|
.await
|
||||||
|
.expect("Err creating client");
|
||||||
|
if let Err(why) = client.start().await {
|
||||||
|
println!("Client error: {why:?}");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
2
src/bot/mod.rs
Normal file
2
src/bot/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod handler;
|
||||||
|
pub mod init;
|
@ -1 +1,2 @@
|
|||||||
pub mod config;
|
pub mod config;
|
||||||
|
pub mod bot;
|
88
src/main.rs
88
src/main.rs
@ -1,88 +1,10 @@
|
|||||||
use std::collections::HashSet;
|
mod bot;
|
||||||
|
mod config;
|
||||||
|
|
||||||
use actix_cors::Cors;
|
use actix_cors::Cors;
|
||||||
use actix_web::{rt::task::spawn_blocking, App, HttpServer};
|
use actix_web::{App, HttpServer};
|
||||||
use botdiscord::config::{parse_local_config, Config};
|
use bot::init::start_bot;
|
||||||
use serenity::{
|
use config::parse_local_config;
|
||||||
async_trait,
|
|
||||||
client::{Context, EventHandler},
|
|
||||||
http::Http,
|
|
||||||
model::{
|
|
||||||
channel::Message,
|
|
||||||
gateway::{GatewayIntents, Ready},
|
|
||||||
},
|
|
||||||
Client,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Handler;
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl EventHandler for Handler {
|
|
||||||
async fn message(&self, ctx: Context, msg: Message) {
|
|
||||||
if msg.author.bot {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if msg.content == "!ping" {
|
|
||||||
// Sending a message can fail, due to a network error, an
|
|
||||||
// authentication error, or lack of permissions to post in the
|
|
||||||
// channel, so log to stdout when some error happens, with a
|
|
||||||
// description of it.
|
|
||||||
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
|
|
||||||
println!("Error sending message: {:?}", why);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn ready(&self, _: Context, ready: Ready) {
|
|
||||||
println!("{} is connected!", ready.user.name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn start_bot(config: Config) {
|
|
||||||
if config.token != "" {
|
|
||||||
spawn_blocking(move || {
|
|
||||||
let rt = tokio::runtime::Handle::current();
|
|
||||||
rt.block_on(async {
|
|
||||||
let local = tokio::task::LocalSet::new();
|
|
||||||
let _ = local
|
|
||||||
.run_until(async move {
|
|
||||||
let intents = GatewayIntents::GUILD_MESSAGES
|
|
||||||
| GatewayIntents::DIRECT_MESSAGES
|
|
||||||
| GatewayIntents::MESSAGE_CONTENT
|
|
||||||
| GatewayIntents::GUILD_VOICE_STATES
|
|
||||||
| GatewayIntents::GUILDS
|
|
||||||
| GatewayIntents::GUILD_MEMBERS;
|
|
||||||
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 mut client = Client::builder(&config.token, intents)
|
|
||||||
.event_handler(Handler)
|
|
||||||
.await
|
|
||||||
.expect("Err creating client");
|
|
||||||
if let Err(why) = client.start().await {
|
|
||||||
println!("Client error: {why:?}");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.await;
|
|
||||||
})
|
|
||||||
});
|
|
||||||
println!("JOJO 8");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_web::main] // or #[tokio::main]
|
#[actix_web::main] // or #[tokio::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
|
Loading…
Reference in New Issue
Block a user