two way communications

This commit is contained in:
WanderingPenwing 2024-03-11 17:45:10 +01:00
parent ce2992f524
commit 454047661c

View file

@ -3,8 +3,14 @@ use serenity::{
model::{channel::Message, gateway::Ready}, model::{channel::Message, gateway::Ready},
prelude::*, prelude::*,
}; };
use serenity::model::prelude::GuildId;
use std::sync::mpsc; use std::sync::mpsc;
use std::sync::Mutex; use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use std::thread;
use std::sync::Arc;
use crate::postman; use crate::postman;
use crate::discord_structure; use crate::discord_structure;
@ -12,8 +18,11 @@ mod token;
const HELP_MESSAGE: &str = "Hello there, Human! I am a messenger for the wandering penwing."; const HELP_MESSAGE: &str = "Hello there, Human! I am a messenger for the wandering penwing.";
const HELP_COMMAND: &str = "!jiji"; const HELP_COMMAND: &str = "!jiji";
const PACKET_REFRESH : u64 = 500;
struct Handler; struct Handler {
is_loop_running: AtomicBool,
}
#[async_trait] #[async_trait]
@ -30,31 +39,84 @@ impl EventHandler for Handler {
} }
async fn ready(&self, context: Context, ready: Ready) { async fn ready(&self, context: Context, ready: Ready) {
println!("{} is connected!", ready.user.name); println!("bot : {} is connected!", ready.user.name);
let guilds = context.cache.guilds().await; }
async fn cache_ready(&self, context: Context, _guilds: Vec<GuildId>) {
println!("bot : cache built successfully!");
if let Some(sender) = context.data.read().await.get::<postman::Sender>() { let context = Arc::new(context);
for guild_id in guilds {
let guild_name : String = if let Some(guild) = context.cache.guild(guild_id).await { if !self.is_loop_running.load(Ordering::Relaxed) {
guild.name.clone() // We have to clone the Arc, as it gets moved into the new thread.
} else { let context1 = Arc::clone(&context);
"not found".to_string() // tokio::spawn creates a new green thread that can run in parallel with the rest of
}; // the application.
println!("bot : found guild : '{}' ({})", guild_name.clone(), guild_id.clone()); tokio::spawn(async move {
get_guilds(&context1).await;
let guild = discord_structure::Guild::new(guild_name, guild_id.to_string()); });
sender.send(postman::Packet::Guild(guild)).expect("Failed to send packet");
let context2 = Arc::clone(&context);
tokio::spawn(async move {
loop {
check_packets(&context2).await;
thread::sleep(Duration::from_millis(PACKET_REFRESH));
}
});
// Now that the loop is running, we set the bool to true
self.is_loop_running.swap(true, Ordering::Relaxed);
}
}
}
async fn check_packets(context: &Context) {
if let Some(receiver_mutex) = context.data.read().await.get::<postman::Receiver>() {
if let Ok(receiver) = receiver_mutex.lock() {
while let Ok(packet) = receiver.try_recv() {
match packet {
postman::Packet::FetchChannels(guild_id) => {
println!("bot : fetch channels request received : '{}'", guild_id);
}
_ => {
println!("bot : unhandled packet");
}
}
} }
} else { } else {
println!("bot : failed to retrieve sender"); println!("bot : failed to lock receiver");
} }
} else {
println!("bot : failed to retrieve receiver");
}
}
async fn get_guilds(context: &Context) {
let guilds = context.cache.guilds().await;
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
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!("bot : found guild : '{}' ({})", guild_name.clone(), guild_id.clone());
let guild = discord_structure::Guild::new(guild_name, guild_id.to_string());
sender.send(postman::Packet::Guild(guild)).expect("Failed to send packet");
}
} else {
println!("bot : failed to retrieve sender");
} }
} }
pub async fn start_discord_bot(sender: mpsc::Sender<postman::Packet>, receiver: Mutex<mpsc::Receiver<postman::Packet>>) { pub async fn start_discord_bot(sender: mpsc::Sender<postman::Packet>, receiver: Mutex<mpsc::Receiver<postman::Packet>>) {
println!("bot : connection 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 {
is_loop_running: AtomicBool::new(false),
})
.type_map_insert::<postman::Sender>(sender) .type_map_insert::<postman::Sender>(sender)
.type_map_insert::<postman::Receiver>(receiver) .type_map_insert::<postman::Receiver>(receiver)
.await .await