better message history

This commit is contained in:
WanderingPenwing 2024-07-11 15:39:14 +02:00
parent 391c284b59
commit a66e462937
2 changed files with 86 additions and 36 deletions

View file

@ -127,40 +127,8 @@ async fn check_packets(context: &Context) {
}
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);
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
match channel_id_str.parse::<u64>() {
Ok(channel_id_u64) => {
let channel_id = ChannelId::from(channel_id_u64);
let maybe_messages = channel_id.messages(&context.http, |retriever| {
retriever.after(MessageId::from(158339864557912064)).limit(25)
}).await;
match maybe_messages {
Ok(messages) => {
println!("bot : got messages");
for message in messages {
let discord_message = discord_structure::Message::new(message.id.to_string(), channel_id_str.clone(), guild_id_str.clone(), message.author.to_string(), message.content, message.timestamp.to_string());
sender.send(postman::Packet::Message(discord_message)).expect("Failed to send packet");
}
}
Err(why) => {
eprintln!("bot : Failed to fetch messages : {}", why);
}
}
}
Err(why) => {
eprintln!("bot : Failed to parse channel ID string to u64: {}", why);
}
}
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
} else {
println!("bot : failed to retrieve sender");
}
let _result = get_messages(context, guild_id_str, channel_id_str, first_message_id_str).await;
}
_ => {
println!("bot : unhandled packet");
@ -169,6 +137,40 @@ async fn check_packets(context: &Context) {
}
}
async fn get_messages(context: &Context, guild_id_str: String, channel_id_str: String, first_message_id_str: String) -> Result<(), String> {
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
let channel_id_u64 = channel_id_str.parse::<u64>().map_err(|e| e.to_string())?;
let channel_id = ChannelId::from(channel_id_u64);
let messages = if first_message_id_str == "" {
channel_id.messages(&context.http, |retriever| {
retriever.limit(25)
}).await.map_err(|e| e.to_string())?
} else {
let first_message_id_u64 = first_message_id_str.parse::<u64>().map_err(|e| e.to_string())?;
channel_id.messages(&context.http, |retriever| {
retriever.before(MessageId::from(first_message_id_u64)).limit(25)
}).await.map_err(|e| e.to_string())?
};
println!("bot : got messages");
for message in &messages {
let author_name = message.author.name.clone();
let discord_message = discord_structure::Message::new(message.id.to_string(), channel_id_str.clone(), guild_id_str.clone(), author_name, message.content.clone(), message.timestamp.to_string());
sender.send(postman::Packet::Message(discord_message)).map_err(|e| e.to_string())?;
}
if messages.len() == 25 {
let discord_fetch_message = discord_structure::Message::new("".to_string(), channel_id_str.clone(), guild_id_str.clone(), "+".to_string(), "".to_string(), "".to_string());
sender.send(postman::Packet::Message(discord_fetch_message)).map_err(|e| e.to_string())?;
}
sender.send(postman::Packet::FinishedRequest).map_err(|e| e.to_string())?;
}
Ok(())
}
async fn get_guilds(context: &Context) {
let guilds = context.cache.guilds().await;

View file

@ -48,6 +48,7 @@ struct Jiji {
selected_channel: Option<usize>,
time_watch: f32,
pending_bot_requests: usize,
current_message: String,
}
impl Jiji {
@ -61,6 +62,7 @@ impl Jiji {
selected_channel: None,
time_watch: 0.0,
pending_bot_requests: 0,
current_message: "".into(),
}
}
}
@ -105,6 +107,10 @@ impl eframe::App for Jiji {
if self.guilds[guild_index].channels[i].id != message.channel_id {
continue
}
if self.guilds[guild_index].channels[i].messages[0].author_name == "+" {
self.guilds[guild_index].channels[i].messages[0] = message.clone();
continue
}
self.guilds[guild_index].channels[i].messages.insert(0, message.clone());
}
}
@ -157,6 +163,7 @@ impl Jiji {
ui.set_min_width(60.0);
if ui.add(egui::SelectableLabel::new(self.selected_guild == None, "None")).clicked() {
self.selected_guild = None;
self.selected_channel = None;
}
for i in 0..self.guilds.len() {
if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(i), self.guilds[i].name.clone())).clicked() {
@ -164,6 +171,7 @@ impl Jiji {
if self.guilds[i].channels.len() == 0 {
let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[i].id.clone()));
self.selected_channel = None;
self.pending_bot_requests += 1;
}
}
@ -213,6 +221,20 @@ impl Jiji {
egui::TopBottomPanel::bottom("infobar")
.resizable(false)
.show(ctx, |ui| {
if self.selected_channel != None {
ui.label("");
ui.horizontal(|ui| {
if ui.button(">").clicked() {
println!("gui : sent message");
}
egui::ScrollArea::vertical()
.show(ui, |ui| {
let _response = ui.add(egui::TextEdit::multiline(&mut self.current_message)
.desired_width(f32::INFINITY)
.lock_focus(true));
});
});
}
ui.horizontal(|ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
ui.label(&format!("time per frame : {:.1} ms", self.time_watch));
@ -234,13 +256,35 @@ impl Jiji {
.show(ui, |ui| {
if let Some(selected_guild_index) = &self.selected_guild {
if let Some(selected_channel_index) = &self.selected_channel {
let mut last_author = "".to_string();
for message in &self.guilds[*selected_guild_index].channels[*selected_channel_index].messages {
if message.author_name == "-" {
continue
}
ui.separator();
if message.author_name == "+" {
if ui.button("+").clicked() {
if let Some(selected_guild_index) = &self.selected_guild {
if let Some(selected_channel_index) = &self.selected_channel {
let _ = self.sender.send(postman::Packet::FetchMessages(
self.guilds[*selected_guild_index].id.clone(),
self.guilds[*selected_guild_index].channels[*selected_channel_index].id.clone(),
self.guilds[*selected_guild_index].channels[*selected_channel_index].messages[1].id.clone(),
));
self.pending_bot_requests += 1;
}
}
}
continue
}
if message.author_name != last_author {
ui.separator();
ui.colored_label(hex_str_to_color("#3399ff"), &message.author_name);
} else {
ui.label("");
}
ui.label(&message.content);
ui.label(&message.author_name);
last_author = message.author_name.clone();
}
}
}
@ -264,3 +308,7 @@ pub fn load_icon() -> Result<egui::IconData, Box<dyn Error>> {
height: icon_height,
})
}
pub fn hex_str_to_color(hex_str: &str) -> egui::Color32 {
egui::Color32::from_hex(hex_str).unwrap_or_else(|_| egui::Color32::WHITE)
}