From 8cfa9568d0db9c488cb0b677733302692178c12c Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Sun, 14 Jul 2024 11:34:38 +0200 Subject: [PATCH] notificatioonnsss ! --- jiji.project | 2 +- src/app.rs | 9 ++++++++- src/discord_structure.rs | 18 ++++++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/jiji.project b/jiji.project index 851be80..2d8d1f0 100644 --- a/jiji.project +++ b/jiji.project @@ -1 +1 @@ -{"categories":[{"name":"to do","content":[{"name":"ability to change token","description":"use a config file to store token so that \n\n1- it is away from github\n\n2- it is configurable if need be","id":1},{"name":"clean up bot code","description":"try to remove unnecessary code\n\nunindent\n\ngive sender to function ?","id":2},{"name":"trayable ?","description":"// Hello there","id":5},{"name":"new message marker","description":"perma if too complicated to detect read","id":2},{"name":"proper links","description":"when there is a link, ability to click it","id":3}]},{"name":"in progress","content":[]},{"name":"done","content":[{"name":"run discord bot","description":"make it so the bot is running","id":1},{"name":"fixed token in github","description":"// Hello there","id":1},{"name":"fetch previous messages","description":"// Hello there","id":4},{"name":"ability to write messages","description":"// Hello there","id":5},{"name":"get incoming messages","description":"read","id":2},{"name":"unread system","description":"add a * when a channel just received a message","id":1},{"name":"remember channel id for dm","description":"and put in config file\n\nmaybe load message ? dm first ?","id":3},{"name":"bug : does not save notification state for dm","description":"// Hello there","id":1},{"name":"handle unknown channel better","description":"when receiving a message from a not yet scanned guild, create the channel and put the message\n\nallow scanning if guild selected\n\ndo not add duplicate channel","id":1},{"name":"guild unread ?","description":"// Hello there","id":2},{"name":"timestamps","description":"// Hello there","id":1},{"name":"better ui error display","description":"handle the error packet for better display","id":1}]},{"name":"bugs","content":[]},{"name":"v1.0","content":[{"name":"notifications !!!","description":"// Hello there","id":4}]},{"name":"+","content":[]}]} \ No newline at end of file +{"categories":[{"name":"to do","content":[{"name":"ability to change token","description":"use a config file to store token so that \n\n1- it is away from github\n\n2- it is configurable if need be","id":1},{"name":"clean up bot code","description":"try to remove unnecessary code\n\nunindent\n\ngive sender to function ?","id":2},{"name":"trayable ?","description":"// Hello there","id":5},{"name":"new message marker","description":"perma if too complicated to detect read","id":2},{"name":"proper links","description":"when there is a link, ability to click it","id":3}]},{"name":"in progress","content":[]},{"name":"done","content":[{"name":"run discord bot","description":"make it so the bot is running","id":1},{"name":"fixed token in github","description":"// Hello there","id":1},{"name":"fetch previous messages","description":"// Hello there","id":4},{"name":"ability to write messages","description":"// Hello there","id":5},{"name":"get incoming messages","description":"read","id":2},{"name":"unread system","description":"add a * when a channel just received a message","id":1},{"name":"remember channel id for dm","description":"and put in config file\n\nmaybe load message ? dm first ?","id":3},{"name":"bug : does not save notification state for dm","description":"// Hello there","id":1},{"name":"handle unknown channel better","description":"when receiving a message from a not yet scanned guild, create the channel and put the message\n\nallow scanning if guild selected\n\ndo not add duplicate channel","id":1},{"name":"guild unread ?","description":"// Hello there","id":2},{"name":"timestamps","description":"// Hello there","id":1},{"name":"better ui error display","description":"handle the error packet for better display","id":1},{"name":"notifications !!!","description":"// Hello there","id":4}]},{"name":"bugs","content":[]},{"name":"v1.0","content":[]},{"name":"+","content":[]}]} \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index e337a09..471aabe 100644 --- a/src/app.rs +++ b/src/app.rs @@ -63,6 +63,7 @@ impl Jiji { } else { println!("app: unknown channel"); self.guilds[guild_index].channels.push(discord_structure::Channel::create(message.channel_id.clone(), message.channel_id.clone(), message.guild_id.clone())); + let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[guild_index].id.clone())); self.guilds[guild_index].channels.len() - 1 }; @@ -71,9 +72,15 @@ impl Jiji { } if self.guilds[guild_index].channels[channel_index].notify && message.new != "" { + let mut body_text = format!("{} - {}", self.guilds[guild_index].name, self.guilds[guild_index].channels[channel_index].name); + + if self.guilds[guild_index].id == "dm" { + body_text = format!("{}", self.guilds[guild_index].name); + } + let _ = Notification::new() .summary(&message.author_name) - .body(&format!("{} - {}", self.guilds[guild_index].name, self.guilds[guild_index].channels[channel_index].name)) + .body(&body_text) .timeout(0) .show(); } diff --git a/src/discord_structure.rs b/src/discord_structure.rs index 7ae699a..d373d9e 100644 --- a/src/discord_structure.rs +++ b/src/discord_structure.rs @@ -1,4 +1,5 @@ use chrono::{DateTime, ParseError}; +use notify_rust::Notification; #[derive(PartialEq, Clone)] pub struct Guild { @@ -21,11 +22,24 @@ impl Guild { pub fn add_channel(&mut self, channel: Channel) { let mut already_exist = false; - for existing_channel in self.channels.clone() { - if existing_channel.id != channel.id { + for i in 0..self.channels.len() { + if self.channels[i].id != channel.id { continue } already_exist = true; + + self.channels[i].name = channel.name.clone(); + + if channel.notify && !self.channels[i].notify { + self.channels[i].notify = true; + if self.channels[i].messages.len() > 0 { + let _ = Notification::new() + .summary(&self.channels[i].messages[self.channels[i].messages.len() - 1].author_name) + .body(&format!("{} - {}", self.name, self.channels[i].name)) + .timeout(0) + .show(); + } + } } if !already_exist {