Skip to content

Instantly share code, notes, and snippets.

@Ancurio
Created September 30, 2023 14:27
Show Gist options
  • Select an option

  • Save Ancurio/5f33a7ed3c84b580d1d748b900a211f6 to your computer and use it in GitHub Desktop.

Select an option

Save Ancurio/5f33a7ed3c84b580d1d748b900a211f6 to your computer and use it in GitHub Desktop.

Revisions

  1. Ancurio created this gist Sep 30, 2023.
    33 changes: 33 additions & 0 deletions purge_t2bot_ghosts.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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):
    ```bash
    #!/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`:
    ```bash
    #!/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
    ```