bot read message and get servers

This commit is contained in:
WanderingPenwing 2024-03-08 12:59:26 +01:00
parent f8ec903a2c
commit 2d9ea54292
3 changed files with 48 additions and 38 deletions

View file

@ -9,5 +9,5 @@ edition = "2021"
eframe = "0.26.2" eframe = "0.26.2"
futures = "0.3.30" futures = "0.3.30"
image = "0.24.9" image = "0.24.9"
serenity = { default-features = false, features = ["client", "gateway", "model", "rustls_backend"], version = "0.9.0-rc.2"} serenity = { default-features = false, features = ["client", "gateway", "model", "rustls_backend", "cache", "http"], version = "0.9.0-rc.2"}
tokio = {features = ["macros"], version = "0.2"} tokio = {features = ["macros"], version = "0.2"}

View file

@ -1,7 +1,7 @@
use serenity::{ use serenity::{
async_trait, async_trait,
model::{channel::Message, gateway::Ready}, model::{channel::Message, gateway::Ready},
prelude::*, prelude::*,
}; };
mod token; mod token;
@ -13,35 +13,47 @@ struct Handler;
#[async_trait] #[async_trait]
impl EventHandler for Handler { impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) { async fn message(&self, ctx: Context, msg: Message) {
if msg.content == HELP_COMMAND { println!("Message received : '{}' from {}", msg.content, msg.author);
if let Err(why) = msg.channel_id.say(&ctx.http, HELP_MESSAGE).await { if msg.content == HELP_COMMAND {
println!("Error sending message: {:?}", why); println!("Message is command");
} if let Err(why) = msg.channel_id.say(&ctx.http, HELP_MESSAGE).await {
} println!("Error sending message: {:?}", why);
} return
}
println!("Successfuly sent message");
}
}
async fn ready(&self, _: Context, ready: Ready) { async fn ready(&self, context: Context, ready: Ready) {
println!("{} is connected!", ready.user.name); println!("{} is connected!", ready.user.name);
} let guilds = context.cache.guilds().await;
for guild_id in guilds {
let guild_name : String = if let Some(guild) = context.cache.guild(guild_id).await {
guild.name.clone()
} else {
"not found".to_string()
};
println!("Guild : '{}' ({})", guild_id, guild_name);
}
}
} }
pub async fn start_discord_bot() { pub async fn start_discord_bot() {
println!("Creation process started"); println!("Bot connection process started...");
let maybe_client = Client::builder(token::TOKEN) let maybe_client = Client::builder(token::TOKEN)
.event_handler(Handler) .event_handler(Handler)
.await .await
.map_err(|why| format!("Client error: {:?}", why)); .map_err(|why| format!("Client error: {:?}", why));
println!("Got a result"); if let Ok(mut client) = maybe_client {
if let Err(why) = client.start().await {
if let Ok(mut client) = maybe_client { println!("Client error: {:?}", why);
if let Err(why) = client.start().await { return;
println!("Client error: {:?}", why); }
return; } else {
} println!("No Client");
} else { return;
println!("No Client"); }
return;
}
} }

View file

@ -8,13 +8,11 @@ mod bot;
const MAX_FPS: f32 = 30.0; const MAX_FPS: f32 = 30.0;
fn main() { fn main() {
println!("hello there"); let _handle = thread::spawn(|| {
println!("Bot thread spawned");
let handle = thread::spawn(|| { let mut rt = Runtime::new().unwrap();
println!("general kenobi"); rt.block_on(bot::start_discord_bot());
let mut rt = Runtime::new().unwrap(); });
rt.block_on(bot::start_discord_bot());
});
// Run the GUI on the main thread // Run the GUI on the main thread
gui(); gui();
@ -25,7 +23,7 @@ fn gui() {
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default() viewport: egui::ViewportBuilder::default()
.with_inner_size([1200.0, 800.0]) .with_inner_size([400.0, 300.0])
.with_icon(Arc::new(icon_data)), .with_icon(Arc::new(icon_data)),
..Default::default() ..Default::default()
}; };