snippets/bash/frequented.sh
2024-08-30 15:24:30 +03:00

79 lines
2.2 KiB
Bash
Executable file

#!/usr/bin/env bash
# Usage: ./frequented.sh [number=5]
# Lists your top `number` most frequented channels
# Single threaded, still does 1.22 milliseconds per channel
# (on a 5600X with 2289 channels and 385952 messages)
# set this to your id to not print your id in dms/group chats
SELF_ID="${SELF_ID:-276363003270791168}"
n=$((${1:-5}))
repeat() {
local n=${2?}
for ((i=0; i<n; i++)); do echo "${1?}"; done
}
# shellcheck disable=SC2207
counts=($(repeat '0' $n))
# shellcheck disable=SC2207
channels=($(repeat 'none' $n))
# insert $1 and $2 into index $3 and drop last element
insert() {
local value1=${1?}
local value2=${2?}
local idx=${3:-0}
counts=("${counts[@]:0:$idx}" "$value1" "${counts[@]:$idx:$((n - idx - 1))}")
channels=("${channels[@]:0:$idx}" "$value2" "${channels[@]:$idx:$((n - idx - 1))}")
}
total_messages=0
total_channels=0
word_count() {
local total=0
while read -r; do
total=$((total + 1))
done
echo $total
}
time for dir in c*; do
channel=${dir#c}
count=$(("$(word_count < "$dir"/messages.csv)" - 1))
total_messages=$((total_messages + count))
total_channels=$((total_channels + 1))
for ((i=0; i<n; i++)); do
if ((count > ${counts[$i]})); then
insert $count "$channel" $i
break
fi
done
done
echo "total channels: $total_channels"
echo "total messages: $total_messages"
echo "most frequented $n channels:"
for ((i=0; i<n; i++)); do
# 1 and 3 are DM and GROUP_DM respectively
# https://discord.com/developers/docs/resources/channel#channel-object-channel-types
# if you want to understand the following `jq` filter,
# start reading here: https://jqlang.github.io/jq/manual/#basic-filters
channel_text=$(jq 'if .type == 1 // .type == 3 then
.id + " (" + if (.recipients | length) > 2 then
"GC with "
else
"DM with "
end + (.recipients | map(select(. != "'"$SELF_ID"'")) | join(", ")) + ")"
else
if has("name") then
"#" + .name + " (" + .guild.name + ")"
else
.id + " (unknown guild)"
end
end' c"${channels[$i]}"/channel.json)
echo "$((i + 1)). messages: ${counts[$i]}, channel: $channel_text"
done