prepared bot startup

This commit is contained in:
WanderingPenwing 2024-03-07 13:28:57 +01:00
parent 09f81bd03d
commit 5fb197390d
2 changed files with 93 additions and 98 deletions

39
src/bot.rs Normal file
View file

@ -0,0 +1,39 @@
use serenity::{
async_trait,
model::{channel::Message, gateway::Ready},
prelude::*,
};
mod token;
const HELP_MESSAGE: &str = "Hello there, Human! I am a messenger for the wandering penwing.";
const HELP_COMMAND: &str = "!jiji";
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 ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
async fn start_discord_bot() -> Result<Client, String> {
let mut client = Client::new(token::TOKEN)
.event_handler(Handler)
.await
.map_err(|why| format!("Client error: {:?}", why))?;
if let Err(why) = client.start().await {
return Err(format!("Client error: {:?}", why));
}
Ok(client)
}

View file

@ -1,58 +1,45 @@
use eframe::egui; use eframe::egui;
use image::GenericImageView; use image::GenericImageView;
use std::{ use serenity::prelude::*;
thread, use std::{error::Error, sync::Arc, thread, time};
time,
sync::Arc,
error::Error,
env,
};
use serenity::{
async_trait,
model::{channel::Message, gateway::Ready},
prelude::*,
};
mod token; mod bot;
const MAX_FPS: f32 = 30.0;
const MAX_FPS : f32 = 30.0; fn main() -> Result<(), eframe::Error> {
const HELP_MESSAGE: &str = "Hello there, Human! I am a messenger for the wandering penwing."; let icon_data = load_icon().unwrap_or_default();
const HELP_COMMAND: &str = "!jiji";
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([1200.0, 800.0])
.with_icon(Arc::new(icon_data)),
..Default::default()
};
//fn main() -> Result<(), eframe::Error> { eframe::run_native(
// let icon_data = load_icon().unwrap_or_default(); "Jiji",
// options,
// let options = eframe::NativeOptions { Box::new(move |_cc| Box::from(Jiji::default())),
// viewport: egui::ViewportBuilder::default() )
// .with_inner_size([1200.0, 800.0]) }
// .with_icon(Arc::new(icon_data)),
// ..Default::default()
// };
//
// eframe::run_native(
// "Jiji",
// options,
// Box::new(move |_cc| Box::from(Jiji::default())),
// )
//}
struct Jiji { struct Jiji {
next_frame: time::Instant, next_frame: time::Instant,
bot: Option<Client>,
} }
impl Default for Jiji { impl Default for Jiji {
fn default() -> Self { fn default() -> Self {
// should start the bot
Self { Self {
next_frame: time::Instant::now(), next_frame: time::Instant::now(),
bot: None,
//bot_process_reference
} }
} }
} }
impl eframe::App for Jiji { impl eframe::App for Jiji {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
thread::sleep(time::Duration::from_secs_f32( thread::sleep(time::Duration::from_secs_f32(
@ -60,6 +47,8 @@ impl eframe::App for Jiji {
)); ));
self.next_frame = time::Instant::now(); self.next_frame = time::Instant::now();
//here if bot started put its reference in self.bot
self.draw_feed(ctx); self.draw_feed(ctx);
} }
} }
@ -87,36 +76,3 @@ pub fn load_icon() -> Result<egui::IconData, Box<dyn Error>> {
height: icon_height, height: icon_height,
}) })
} }
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 ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
#[tokio::main]
async fn main() {
//let token = env::var(token::TOKEN)
//.expect("Expected a token in the environment");
let mut client = Client::new(&token::TOKEN)
.event_handler(Handler)
.await
.expect("Err creating client");
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
}
}