ability to send messages

This commit is contained in:
WanderingPenwing 2024-07-11 21:48:36 +02:00
parent d2a0603bbf
commit 6f5bfdfd82
3 changed files with 99 additions and 65 deletions

View file

@ -47,7 +47,6 @@ impl EventHandler for Handler {
} else { } else {
"dm".to_string() "dm".to_string()
}; };
let discord_message = discord_structure::Message::new(msg.id.to_string(), msg.channel_id.to_string(), guild_id, author_name, msg.content.clone(), msg.timestamp.to_rfc2822()); let discord_message = discord_structure::Message::new(msg.id.to_string(), msg.channel_id.to_string(), guild_id, author_name, msg.content.clone(), msg.timestamp.to_rfc2822());
sender.send(postman::Packet::Message(discord_message)).expect("failed to send packet"); sender.send(postman::Packet::Message(discord_message)).expect("failed to send packet");
} else { } else {
@ -104,6 +103,8 @@ async fn check_packets(context: &Context) {
println!("bot : failed to retrieve receiver"); println!("bot : failed to retrieve receiver");
} }
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
for packet in packets_received { for packet in packets_received {
match packet { match packet {
postman::Packet::FetchChannels(guild_id_str) => { postman::Packet::FetchChannels(guild_id_str) => {
@ -115,6 +116,8 @@ async fn check_packets(context: &Context) {
} }
Err(why) => { Err(why) => {
println!("bot : error getting channels : {}", why); println!("bot : error getting channels : {}", why);
sender.send(postman::Packet::Error(why)).expect("Failed to send packet");
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
} }
} }
@ -122,21 +125,45 @@ async fn check_packets(context: &Context) {
postman::Packet::FetchMessages(guild_id_str, channel_id_str, first_message_id_str) => { postman::Packet::FetchMessages(guild_id_str, channel_id_str, first_message_id_str) => {
println!("bot : received FetchMessages packet, channel : '{}', first message : {}", channel_id_str, first_message_id_str); println!("bot : received FetchMessages packet, channel : '{}', first message : {}", channel_id_str, first_message_id_str);
match get_messages(context, guild_id_str, channel_id_str, first_message_id_str).await { match get_messages(context, guild_id_str.clone(), channel_id_str.clone(), first_message_id_str).await {
Ok(_) => { Ok(_) => {
println!("bot : successfuly got messages"); println!("bot : successfuly got messages");
} }
Err(why) => { Err(why) => {
println!("bot : error getting messages : {}", why); println!("bot : error getting messages : {}", why);
sender.send(postman::Packet::Error(why)).expect("Failed to send packet");
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
} }
} }
} }
postman::Packet::SendMessage(channel_id_str, content) => {
println!("bot : received SendMessage packet, channel : '{}', content : {}", channel_id_str, content);
match send_message(context, channel_id_str, content).await {
Ok(_) => {
println!("bot : successfuly sent message");
}
Err(why) => {
println!("bot : error sending message : {}", why);
sender.send(postman::Packet::Error(why)).expect("Failed to send packet");
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
}
}
}
_ => { _ => {
println!("bot : unhandled packet"); println!("bot : unhandled packet");
} }
} }
} }
} }
}
async fn send_message(context: &Context, channel_id_str: String, content: String) -> Result<(), String> {
let channel_id_u64 = channel_id_str.parse::<u64>().map_err(|e| e.to_string())?;
ChannelId(channel_id_u64).say(&context.http, content).await.map_err(|e| e.to_string())?;
Ok(())
}
async fn get_channels(context: &Context, guild_id_str: String) -> Result<(), String> { async fn get_channels(context: &Context, guild_id_str: String) -> Result<(), String> {
if let Some(sender) = context.data.read().await.get::<postman::Sender>() { if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
@ -149,9 +176,9 @@ async fn get_channels(context: &Context, guild_id_str: String) -> Result<(), Str
continue continue
} }
let discord_channel = discord_structure::Channel::new(channel.name, format!("{}",channel_id), guild_id_str.to_string()); let discord_channel = discord_structure::Channel::new(channel.name, format!("{}",channel_id), guild_id_str.to_string());
sender.send(postman::Packet::Channel(discord_channel)).expect("Failed to send packet"); sender.send(postman::Packet::Channel(discord_channel)).map_err(|e| e.to_string())?;
} }
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet"); sender.send(postman::Packet::FinishedRequest).map_err(|e| e.to_string())?;
} else { } else {
return Err("guild not found".to_string()) return Err("guild not found".to_string())

View file

@ -146,11 +146,14 @@ impl eframe::App for Jiji {
} }
} }
postman::Packet::Error(reason) => {
println!("gui : error received {}", reason);
}
postman::Packet::FinishedRequest => { postman::Packet::FinishedRequest => {
self.pending_bot_requests = self.pending_bot_requests.checked_sub(1).unwrap_or(0); self.pending_bot_requests = self.pending_bot_requests.checked_sub(1).unwrap_or(0);
} }
_ => { _ => {
println!("unhandled packet"); println!("gui : unhandled packet");
} }
} }
} }
@ -252,11 +255,13 @@ impl Jiji {
egui::TopBottomPanel::bottom("infobar") egui::TopBottomPanel::bottom("infobar")
.resizable(false) .resizable(false)
.show(ctx, |ui| { .show(ctx, |ui| {
if self.selected_channel != None { if let Some(guild_index) = self.selected_guild {
if let Some(channel_index) = self.selected_channel {
ui.label(""); ui.label("");
ui.horizontal(|ui| { ui.horizontal(|ui| {
if ui.button(">").clicked() { if ui.button(">").clicked() {
println!("gui : sent message"); let _ = self.sender.send(postman::Packet::SendMessage(self.guilds[guild_index].channels[channel_index].id.clone(), self.current_message.clone()));
self.current_message = "".to_string();
} }
egui::ScrollArea::vertical() egui::ScrollArea::vertical()
.show(ui, |ui| { .show(ui, |ui| {
@ -266,6 +271,7 @@ impl Jiji {
}); });
}); });
} }
}
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| { ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
ui.label(&format!("time per frame : {:.1} ms", self.time_watch)); ui.label(&format!("time per frame : {:.1} ms", self.time_watch));

View file

@ -7,11 +7,12 @@ pub enum Packet {
Guild(discord_structure::Guild), Guild(discord_structure::Guild),
Channel(discord_structure::Channel), Channel(discord_structure::Channel),
Message(discord_structure::Message), Message(discord_structure::Message),
SendMessage(String, String),
ChannelEnd(String, String), ChannelEnd(String, String),
FetchChannels(String), FetchChannels(String),
FetchMessages(String, String, String), FetchMessages(String, String, String),
FinishedRequest, FinishedRequest,
Error(String),
} }
pub struct Sender; pub struct Sender;