Interaction logging

master
Tymon 1 year ago
parent d975537ddd
commit 8add137b19

@ -11,6 +11,7 @@ panic = "abort"
[dependencies]
eframe = "0.19.0"
egui-toast = "0.4.0"
serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.86"
tungstenite = "0.17.3"

@ -1,16 +1,19 @@
use std::sync::mpsc::Receiver;
use std::time::Duration;
use eframe::{
egui::{self, RichText, ScrollArea, Window},
emath::Align,
epaint::Color32,
};
use serde::{Deserialize, Serialize};
use egui_toast::Toasts;
use serde::Deserialize;
use tungstenite::Message;
pub struct App {
rx: Receiver<Message>,
history: Vec<Log>,
history: Vec<MessageLog>,
autoscroll: bool,
description: String,
selected_channel: String,
@ -18,14 +21,29 @@ pub struct App {
description_shown: bool,
}
#[derive(Serialize, Deserialize)]
struct Log {
#[derive(Deserialize)]
struct MessageLog {
name: String,
description: String,
message: String,
channel: String,
}
#[derive(Deserialize)]
struct InteractionLog {
name: String,
command: String,
arguments: String,
channel: String,
}
#[derive(Deserialize)]
#[serde(tag = "type")]
enum Loggers {
InteractionLog(InteractionLog),
MessageLog(MessageLog),
}
impl App {
pub fn new(rx: Receiver<Message>) -> Self {
Self {
@ -42,15 +60,38 @@ impl App {
impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
let mut toasts = Toasts::new()
.anchor((10.0, 10.0))
.direction(egui::Direction::TopDown)
.align_to_end(false);
let message = self.rx.try_recv();
// TODO: Add toasts for things like debug information and status changes (https://github.com/urholaukkarinen/egui-toast)
if let Ok(message) = message {
let text = message.to_string();
let json = serde_json::from_str(&text);
if let Ok(json) = json {
self.history.push(json);
let json: Loggers = json;
match json {
Loggers::MessageLog(msg) => {
self.history.push(msg);
}
Loggers::InteractionLog(msg) => {
if self.selected_channel == msg.channel {
toasts.info(
format!(
"User {} called command /{} with arguments {}",
msg.name, msg.command, msg.arguments
),
Duration::from_secs(5),
);
}
}
}
} else if let Err(json) = json {
println!("{}", json);
}
}
egui::CentralPanel::default().show(ctx, |ui| {
@ -117,5 +158,6 @@ impl eframe::App for App {
});
ctx.request_repaint();
toasts.show(ctx);
}
}

@ -21,10 +21,10 @@ fn main() {
socket
.write_message(Message::Text("__PONG__".to_string()))
.expect("Failed to respond to ping! Did the WebSocket server die?");
} else {
tx.send(message)
.expect("Failed to send message to channel! (This shouldn't be possible)");
}
tx.send(message)
.expect("Failed to send message to channel! (This shouldn't be possible)");
}
});

Loading…
Cancel
Save