Last active
February 21, 2025 01:05
-
-
Save notfood/b8d93250c706f1d5b5faee99c111126f to your computer and use it in GitHub Desktop.
Bash script to find the top AFK in a drawpile server and KICK them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Drawpile Admin API URL. Alternatively use pubsrvproxy for safety. | |
API=http://localhost:27780/api | |
# Minimun free slots to begin AFK KICK | |
MINSLOTS=3 | |
# Number of AFK users to KICK | |
NUM2KICK=5 | |
# Minutes to be considered AFK | |
MINAFK=60 | |
# Message to be given before KICK format(name, seconds afk) | |
MESSAGE="%s, you have been AFK kicked by the administrator for being %(%H:%M)T hours away." | |
# Should we log the kicks to console? | |
LOG=1 | |
# Get the session ids with almost no slots for only public boards | |
sessions=$(curl -s $API/sessions | jq -c '[.[] | |
| select(.hasPassword == false and .maxUserCount - .userCount < '$MINSLOTS') | |
| .id]') | |
# Exit if there is no sessions full | |
if [ "$sessions" = "[]" ]; then | |
exit | |
fi | |
# Don't overwhelm Admin | |
sleep .5 | |
# Pick top non mod non op AFK | |
users=$(curl -s $API/users | jq '. | |
| map(.afk = (.lastActive | split("+")[0] + "Z" | now - fromdate)) | |
| sort_by(-.afk) | |
| | |
[ .[] | |
| select(.session | IN('${sessions:1:-1}')) | |
| select(.mod == false and .op == false and .afk > 60*'$MINAFK') | |
| [.id, .session, .name, .afk] | |
| join("|") | |
] [:'$NUM2KICK'] []' ) | |
# Avoid any action if the above failed | |
if [ $? -ne 0 ]; then | |
exit 1; | |
fi | |
# If no users to kick, notify | |
if [ "$users" = "" ]; then | |
if [ "$LOG" -ne "0" ]; then | |
printf "FULL without AFK\n" | |
fi | |
exit | |
fi | |
# Notify and Kick | |
while IFS=$'\n' read line; do | |
IFS='|' read -r id session name afk <<<"${line:1:-1}" | |
afk=${afk%.*} | |
# Output for logging | |
if [ "$LOG" -ne "0" ]; then | |
printf "KICK %s/%s\t%s\t%(%HH:%MM:%SS)T\t%s\n" "$session" "$id" "$afk" "$afk" "$name" | |
fi | |
url="$API/sessions/$session/$id" | |
msg=$(printf "$MESSAGE" "$name" "$afk") | |
curl -s -H 'Content-Type: application/json' -X PUT -d '{"message":"'"$msg"'"}' "$url" -o /dev/null | |
sleep .5 | |
curl -s -X DELETE "$url" -o /dev/null | |
sleep 1 | |
done <<<"$users" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Drawpile afk users kick service | |
[Service] | |
Type=oneshot | |
ExecStart=drawpile-afk-kick.sh | |
[Install] | |
WantedBy=multi-user.target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Timer] | |
OnCalendar=*:0/15 | |
[Install] | |
WantedBy=timers.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment