ability for the bot to send data to the gui
This commit is contained in:
parent
2d9ea54292
commit
d11305b039
24
src/bot.rs
24
src/bot.rs
|
@ -3,6 +3,8 @@ use serenity::{
|
|||
model::{channel::Message, gateway::Ready},
|
||||
prelude::*,
|
||||
};
|
||||
use std::sync::mpsc;
|
||||
use crate::postman;
|
||||
|
||||
mod token;
|
||||
|
||||
|
@ -29,21 +31,33 @@ impl EventHandler for Handler {
|
|||
println!("{} is connected!", ready.user.name);
|
||||
let guilds = context.cache.guilds().await;
|
||||
|
||||
let mut guild_names : Vec<String> = vec![];
|
||||
|
||||
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()
|
||||
};
|
||||
guild.name.clone()
|
||||
} else {
|
||||
"not found".to_string()
|
||||
};
|
||||
guild_names.push(guild_name.clone());
|
||||
println!("Guild : '{}' ({})", guild_id, guild_name);
|
||||
}
|
||||
|
||||
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
|
||||
let message = postman::Message::new(postman::MessageType::GuildName, guild_names[0].clone());
|
||||
sender.send(message).unwrap();
|
||||
println!("Message from bot to gui, sent");
|
||||
} else {
|
||||
println!("Failed to retrieve sender");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_discord_bot() {
|
||||
pub async fn start_discord_bot(sender: mpsc::Sender<postman::Message>) {
|
||||
println!("Bot connection process started...");
|
||||
let maybe_client = Client::builder(token::TOKEN)
|
||||
.event_handler(Handler)
|
||||
.type_map_insert::<postman::Sender>(sender)
|
||||
.await
|
||||
.map_err(|why| format!("Client error: {:?}", why));
|
||||
|
||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -1,24 +1,27 @@
|
|||
use eframe::egui;
|
||||
use image::GenericImageView;
|
||||
use std::{error::Error, sync::Arc, thread, time};
|
||||
use std::{error::Error, sync::Arc, sync::mpsc, thread, time};
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
mod bot;
|
||||
mod postman;
|
||||
|
||||
const MAX_FPS: f32 = 30.0;
|
||||
|
||||
fn main() {
|
||||
let _handle = thread::spawn(|| {
|
||||
let (tx, rx) = mpsc::channel::<postman::Message>(); //tx transmiter
|
||||
|
||||
let _handle = thread::spawn(move || {
|
||||
println!("Bot thread spawned");
|
||||
let mut rt = Runtime::new().unwrap();
|
||||
rt.block_on(bot::start_discord_bot());
|
||||
rt.block_on(bot::start_discord_bot(tx));
|
||||
});
|
||||
|
||||
// Run the GUI on the main thread
|
||||
gui();
|
||||
gui(rx);
|
||||
}
|
||||
|
||||
fn gui() {
|
||||
fn gui(receiver: mpsc::Receiver<postman::Message>) {
|
||||
let icon_data = load_icon().unwrap_or_default();
|
||||
|
||||
let options = eframe::NativeOptions {
|
||||
|
@ -28,17 +31,19 @@ fn gui() {
|
|||
..Default::default()
|
||||
};
|
||||
|
||||
let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new())));
|
||||
let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new(receiver))));
|
||||
}
|
||||
|
||||
struct Jiji {
|
||||
next_frame: time::Instant,
|
||||
receiver: mpsc::Receiver<postman::Message>,
|
||||
}
|
||||
|
||||
impl Jiji {
|
||||
fn new() -> Self {
|
||||
fn new(receiver: mpsc::Receiver<postman::Message>) -> Self {
|
||||
Self {
|
||||
next_frame: time::Instant::now(),
|
||||
receiver,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
27
src/postman.rs
Normal file
27
src/postman.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
use std::sync::mpsc;
|
||||
use serenity::prelude::TypeMapKey;
|
||||
|
||||
pub struct Message {
|
||||
kind: MessageType,
|
||||
content: String,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn new(kind : MessageType, content: String) -> Self {
|
||||
Self {
|
||||
kind,
|
||||
content,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub enum MessageType {
|
||||
GuildName,
|
||||
}
|
||||
|
||||
pub struct Sender;
|
||||
|
||||
impl TypeMapKey for Sender {
|
||||
type Value = mpsc::Sender<Message>;
|
||||
}
|
Loading…
Reference in a new issue