I recently migrated away from t2bot.io's discord<->matrix bridging (to a self-hosted one), which after figuring out all the nice synapse and systemd related pecularities left me with one last cleanup of all the @_discord_ID:t2bot.io
ghosts haunting my previously bridged matrix rooms.
In order to automate the process I wrote two scripts that I want to share; the main one is purge_t2bot_ghosts.sh
which you provide with a room ID (the internal ones starting with a !
and looking like a random-generated password), and an access token with admin priviledges; also note that due to the _synapse
API these scripts are using, they must be executed on localhost
(this might be rewritten to be used from anywhere, idk). You can optionally specify a delay in seconds between kicks to not trigger the built-in rate limit, but I've found 4 seconds (the default) to be exactly right.
Tip: Do this before setting up any replacement bridges to avoid status spams on the Discord site.
purge_t2bot_ghosts.sh
(requires jq):
#!/bin/bash
ROOM_ID=$1
TOKEN=$2
DELAY=${3:-4}
curl 'http://localhost:8008/_synapse/admin/v1/rooms/'$ROOM_ID'/members?access_token='$TOKEN -X GET \
| jq -r '.members[]' | grep 't2bot\.io$' | \
xargs -I{} ./kick_usr.sh $ROOM_ID {} $TOKEN $DELAY
kick_usr.sh
:
#!/bin/bash
ROOMID=$1
USERID=$2
TOKEN=$3
DELAY=$4
echo "Kicking ${USERID}"
curl 'http://localhost:8008/_matrix/client/v3/rooms/'$ROOMID'/kick?access_token='$TOKEN \
-X POST --data '{"user_id": "'$USERID'"}'
sleep $DELAY