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"
futures = "0.3.30"
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"}

View file

@ -1,7 +1,7 @@
use serenity::{
async_trait,
model::{channel::Message, gateway::Ready},
prelude::*,
async_trait,
model::{channel::Message, gateway::Ready},
prelude::*,
};
mod token;
@ -13,35 +13,47 @@ struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == HELP_COMMAND {
if let Err(why) = msg.channel_id.say(&ctx.http, HELP_MESSAGE).await {
println!("Error sending message: {:?}", why);
}
}
}
async fn message(&self, ctx: Context, msg: Message) {
println!("Message received : '{}' from {}", msg.content, msg.author);
if msg.content == HELP_COMMAND {
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) {
println!("{} is connected!", ready.user.name);
}
async fn ready(&self, context: Context, ready: Ready) {
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() {
println!("Creation process started");
let maybe_client = Client::builder(token::TOKEN)
.event_handler(Handler)
.await
.map_err(|why| format!("Client error: {:?}", why));
println!("Bot connection process started...");
let maybe_client = Client::builder(token::TOKEN)
.event_handler(Handler)
.await
.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 {
println!("Client error: {:?}", why);
return;
}
} else {
println!("No Client");
return;
}
if let Ok(mut client) = maybe_client {
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
return;
}
} else {
println!("No Client");
return;
}
}

View file

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