fix: only announce departures for users who joined monitored channels
Amolith
created
Track users who join monitored channels and only announce their
departure when they disconnect. This prevents announcing departures
for users who never joined channels we care about.
@@ -19,6 +19,9 @@ MONITORED_CHANNELS=(
"Room 5"
)
+# Array to track users who have joined monitored channels
+TRACKED_USERS=()
+
# Function to print debug messages if DEBUG is true
log() {
if ((DEBUG)); then
@@ -146,6 +149,13 @@ parse_move_event() {
# Check if this channel should be monitored
if is_monitored_channel "$channel"; then
log "*herald perks up* This movement requires my attention!"
+
+ # Add user to tracked users array if not already present
+ if [[ ! " ${TRACKED_USERS[*]} " =~ \ $username\ ]]; then
+ TRACKED_USERS+=("$username")
+ log "*herald scribbles name in ledger* '$username' is now on my scroll of monitored souls"
+ fi
+
local join_msg
join_msg=$(get_random_message JOIN_MESSAGES)
local message="${join_msg//\{USERNAME\}/$username}"
@@ -170,11 +180,28 @@ parse_disconnect_event() {
local username="${BASH_REMATCH[1]}"
log "*herald nods knowingly* '$username' has departed the realm entirely"
- local leave_msg- leave_msg=$(get_random_message LEAVE_MESSAGES)- local message="${leave_msg//\{USERNAME\}/$username}"- echo "*herald notices someone's absence and sighs*"- send_notification "$message"
+ # Only announce departure if we were tracking this user
+ if [[ " ${TRACKED_USERS[*]} " =~ \ $username\ ]]; then
+ log "*herald checks ledger* Ah yes, '$username' was on my scroll. Time for a farewell..."
+
+ # Remove user from tracked users array
+ local temp_array=()
+ for user in "${TRACKED_USERS[@]}"; do
+ if [[ "$user" != "$username" ]]; then
+ temp_array+=("$user")
+ fi
+ done
+ TRACKED_USERS=("${temp_array[@]}")
+ log "*herald crosses out name* '$username' removed from my scroll of monitored souls"
+
+ local leave_msg
+ leave_msg=$(get_random_message LEAVE_MESSAGES)
+ local message="${leave_msg//\{USERNAME\}/$username}"
+ echo "*herald notices someone's absence and sighs*"
+ send_notification "$message"
+ else
+ log "*herald glances up briefly* '$username' departed, but they weren't on my sacred list anyway. *returns to scrolls*"
+ fi
else
log "*herald frowns* Disconnect event format puzzling. *grumbles about log formats* Line: $log_line"
fi