From 0e3c286eaf59922537573babf04d7df86e428453 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Mon, 16 May 2022 18:55:32 +0200 Subject: [PATCH] feat: discord bridging See https://git.coopcloud.tech/coop-cloud/matrix-synapse/issues/31 --- .env.sample | 9 +++ README.md | 19 ++++++ abra.sh | 3 +- compose.discord.yml | 66 +++++++++++++++++++++ compose.telegram.yml | 6 +- discord_bridge.yaml.tmpl | 123 +++++++++++++++++++++++++++++++++++++++ homeserver.yaml.tmpl | 5 +- 7 files changed, 224 insertions(+), 7 deletions(-) create mode 100644 compose.discord.yml create mode 100644 discord_bridge.yaml.tmpl diff --git a/.env.sample b/.env.sample index 26d4375..66b5b6a 100644 --- a/.env.sample +++ b/.env.sample @@ -65,6 +65,9 @@ ENCRYPTED_BY_DEFAULT=all #SMTP_USER= #SECRET_SMTP_PASSWORD_VERSION=v1 +#APP_SERVICES_ENABLED=1 +#APP_SERVICE_CONFIGS="[\"...\"]" + #COMPOSE_FILE="$COMPOSE_FILE:compose.telegram.yml" #APP_SERVICE_BOT_USERNAME=telegrambot #APP_SERVICE_DISPLAY_NAME="Telegram bridge bot" @@ -81,3 +84,9 @@ ENCRYPTED_BY_DEFAULT=all #SECRET_TELEGRAM_BOT_TOKEN_VERSION=v1 #SECRET_TELEGRAM_AS_TOKEN_VERSION=v1 #SECRET_TELEGRAM_HS_TOKEN_VERSION=v1 + +COMPOSE_FILE="$COMPOSE_FILE:compose.discord.yml" +#DISCORD_CLIENT_ID= +#DISCORD_BRIDGE_ADMIN= +#SECRET_DISCORD_BOT_TOKEN_VERSION=v1 +#SECRET_DISCORD_DB_PASSWORD_VERSION=v1 diff --git a/README.md b/README.md index a05bc4f..3c6e07f 100644 --- a/README.md +++ b/README.md @@ -118,3 +118,22 @@ Some helpful documentation: - [`docs.mau.fi`](https://docs.mau.fi/bridges/python/setup/docker.html?bridge=telegram) - [`example-config.yaml`](https://mau.dev/mautrix/telegram/-/blob/master/mautrix_telegram/example-config.yaml) + +### Discord bridging + +> WIP + +Just as messy as the Telegram bridging above! Rough guide: + +- get a local copy of [`config.yaml`](https://github.com/matrix-org/matrix-appservice-discord/blob/develop/config/config.sample.yaml) +- fill it out with the values you need, all the discord token stuff, etc. +- run `mkdir -p data && cp config.yaml data/` then `docker run --rm -v data:/data halfshot/matrix-appservice-discord:v1.0.0 sh -c "cd /data && node /build/src/discordas.js -r -u "http://discordbridge:9005" -c config.yaml"` +- this generates the app service registration configuration you need to feed to the homeserver +- run secret generation for the `discord_db_password`, insert your `discord_bot_token` +- run `abra app cp discord-registration.yaml app:/discord-data` (it has to be called `discord-registration.yaml`) +- deploy the bridge & happy hacking + +Some helpful documentation: + +- [`matrix-org/matrix-appservice-discord` docs](https://github.com/matrix-org/matrix-appservice-discord#bridging-a-room) +- [`t2bot.io/discord`](https://t2bot.io/discord/) diff --git a/abra.sh b/abra.sh index eab1085..0a6f4a7 100644 --- a/abra.sh +++ b/abra.sh @@ -1,4 +1,5 @@ export ENTRYPOINT_CONF_VERSION=v1 -export HOMESERVER_YAML_VERSION=v9 +export HOMESERVER_YAML_VERSION=v10 export LOG_CONFIG_VERSION=v2 export TELEGRAM_BRIDGE_YAML_VERSION=v2 +export DISCORD_BRIDGE_YAML_VERSION=v1 diff --git a/compose.discord.yml b/compose.discord.yml new file mode 100644 index 0000000..8cefa64 --- /dev/null +++ b/compose.discord.yml @@ -0,0 +1,66 @@ +--- +version: "3.8" + +services: + app: + environment: + - APP_SERVICES_ENABLED + - APP_SERVICE_CONFIGS + volumes: + - discord-data:/discord-data + + discordbridge: + image: halfshot/matrix-appservice-discord:v1.0.0 + depends_on: + - discorddb + configs: + - source: discord_bridge_yaml + target: /data/config.yaml + environment: + - DISCORD_CLIENT_ID + - DISCORD_BRIDGE_ADMIN + - HOMESERVER_DOMAIN + - HOMESERVER_URL + secrets: + - discord_bot_token + - discord_db_password + volumes: + - discord-data:/data + networks: + - internal + + discorddb: + image: postgres:13-alpine + secrets: + - discord_db_password + environment: + - LC_COLLATE=C + - LC_CTYPE=C + - POSTGRES_DB=discordbridge + - POSTGRES_INITDB_ARGS="-E \"UTF8\"" + - POSTGRES_PASSWORD_FILE=/run/secrets/discord_db_password + - POSTGRES_USER=discordbridge + networks: + - internal + healthcheck: + test: ["CMD", "pg_isready", "-U", "synapse"] + volumes: + - discord-postgres:/var/lib/postgresql/data + +configs: + discord_bridge_yaml: + name: ${STACK_NAME}_discord_bridge_yaml_${DISCORD_BRIDGE_YAML_VERSION} + file: discord_bridge.yaml.tmpl + template_driver: golang + +volumes: + discord-data: + discord-postgres: + +secrets: + discord_db_password: + external: true + name: ${STACK_NAME}_discord_db_password_${SECRET_DISCORD_DB_PASSWORD_VERSION} + discord_bot_token: + external: true + name: ${STACK_NAME}_discord_bot_token_${SECRET_DISCORD_BOT_TOKEN_VERSION} diff --git a/compose.telegram.yml b/compose.telegram.yml index 28aba53..95acf38 100644 --- a/compose.telegram.yml +++ b/compose.telegram.yml @@ -4,15 +4,15 @@ version: "3.8" services: app: environment: - - TELEGRAM_APP_SERVICE_ENABLED=1 - - TELEGRAM_APP_SERVICE_CONFIG=/telegram-data/registration.yaml + - APP_SERVICES_ENABLED + - APP_SERVICE_CONFIGS volumes: - telegram-data:/telegram-data telegrambridge: image: dock.mau.dev/mautrix/telegram:1f5b91cbec7866663d8dd8f4c9a62115e2905552-amd64 depends_on: - - telegram_db + - telegramdb configs: - source: telegram_bridge_yaml target: /data/config.yaml diff --git a/discord_bridge.yaml.tmpl b/discord_bridge.yaml.tmpl new file mode 100644 index 0000000..a2707a0 --- /dev/null +++ b/discord_bridge.yaml.tmpl @@ -0,0 +1,123 @@ +bridge: + # Domain part of the bridge, e.g. matrix.org + domain: "{{ env "HOMESERVER_DOMAIN" }}" + # This should be your publicly-facing URL because Discord may use it to + # fetch media from the media store. + homeserverUrl: "{{ env "HOMESERVER_URL" }}" + # The TCP port on which the appservice runs on. + port: 9005 + # Interval at which to process users in the 'presence queue'. If you have + # 5 users, one user will be processed every 500 milliseconds according to the + # value below. This has a minimum value of 250. + # WARNING: This has a high chance of spamming the homeserver with presence + # updates since it will send one each time somebody changes state or is online. + presenceInterval: 500 + # Disable setting presence for 'ghost users' which means Discord users on Matrix + # will not be shown as away or online. + disablePresence: false + # Disable sending typing notifications when somebody on Discord types. + disableTypingNotifications: false + # Disable deleting messages on Discord if a message is redacted on Matrix. + disableDeletionForwarding: false + # Disable portal bridging, where Matrix users can search for unbridged Discord + # rooms on their Matrix server. + disablePortalBridging: false + # Enable users to bridge rooms using !discord commands. See + # https://t2bot.io/discord for instructions. + enableSelfServiceBridging: true + # Disable sending of read receipts for Matrix events which have been + # successfully bridged to Discord. + disableReadReceipts: false + # Disable Join Leave echos from matrix + disableJoinLeaveNotifications: false + # Disable Invite echos from matrix + disableInviteNotifications: false + # Auto-determine the language of code blocks (this can be CPU-intensive) + determineCodeLanguage: false + # MXID of an admin user that will be PMd if the bridge experiences problems. Optional + adminMxid: '{{ env "DISCORD_BRIDGE_ADMIN" }}' + # The message to send to the bridge admin if the Discord token is not valid + invalidTokenMessage: 'Your Discord bot token seems to be invalid, and the bridge cannot function. Please update it in your bridge settings and restart the bridge' + +# Authentication configuration for the discord bot. +auth: + # This MUST be a string (wrapped in quotes) + clientID: "{{ env "DISCORD_CLIENT_ID" }}" + botToken: "{{ secret "discord_bot_token" }}" + # You must enable "Privileged Gateway Intents" in your bot settings on discord.com (e.g. https://discord.com/developers/applications/12345/bot) + # for this to work + usePrivilegedIntents: false + +logging: + # What level should the logger output to the console at. + console: "warn" #silly, verbose, info, http, warn, error, silent + lineDateFormat: "MMM-D HH:mm:ss.SSS" # This is in moment.js format + files: + - file: "debug.log" + disable: + - "PresenceHandler" # Will not capture presence logging + - file: "warn.log" # Will capture warnings + level: "warn" + - file: "botlogs.log" # Will capture logs from DiscordBot + level: "info" + enable: + - "DiscordBot" + +database: + # You may either use SQLite or Postgresql for the bridge database, which contains + # important mappings for events and user puppeting configurations. + # Use the filename option for SQLite, or connString for Postgresql. + # If you are migrating, see https://github.com/Half-Shot/matrix-appservice-discord/blob/master/docs/howto.md#migrate-to-postgres-from-sqlite + # WARNING: You will almost certainly be fine with sqlite unless your bridge + # is in heavy demand and you suffer from IO slowness. + connString: "postgres://discordbridge:{{ secret "discord_db_password" }}@discorddb/discordbridge" + +room: + # Set the default visibility of alias rooms, defaults to "public". + # One of: "public", "private" + defaultVisibility: "public" + +channel: + # Pattern of the name given to bridged rooms. + # Can use :guild for the guild name and :name for the channel name. + namePattern: "[Discord] :guild :name" + # Changes made to rooms when a channel is deleted. + deleteOptions: + # Prefix the room name with a string. + #namePrefix: "[Deleted]" + # Prefix the room topic with a string. + #topicPrefix: "This room has been deleted" + # Disable people from talking in the room by raising the event PL to 50 + disableMessaging: false + # Remove the discord alias from the room. + unsetRoomAlias: true + # Remove the room from the directory. + unlistFromDirectory: true + # Set the room to be unavailable for joining without an invite. + setInviteOnly: true + # Make all the discord users leave the room. + ghostsLeave: true + +limits: + # Delay in milliseconds between discord users joining a room. + roomGhostJoinDelay: 6000 + # Lock timeout in milliseconds before sending messages to discord to avoid + # echos. Default is rather high as the lock will most likely time out + # before anyways. + # echos = (Copies of a sent message may arrive from discord before we've + # fininished handling it, causing us to echo it back to the room) + discordSendDelay: 1500 + # Set a maximum of rooms to be bridged. + # roomCount: 20 + +ghosts: + # Pattern for the ghosts nick, available is :nick, :username, :tag and :id + nickPattern: ":nick" + # Pattern for the ghosts username, available is :username, :tag and :id + usernamePattern: ":username#:tag" + +# Prometheus-compatible metrics endpoint +metrics: + enable: false + port: 9001 + host: "127.0.0.1" diff --git a/homeserver.yaml.tmpl b/homeserver.yaml.tmpl index 0578de0..8fc0408 100644 --- a/homeserver.yaml.tmpl +++ b/homeserver.yaml.tmpl @@ -1442,9 +1442,8 @@ room_prejoin_state: # A list of application service config files to use # -{{ if eq (env "TELEGRAM_APP_SERVICE_ENABLED") "1" }} -app_service_config_files: - - "{{ env "TELEGRAM_APP_SERVICE_CONFIG" }}" +{{ if eq (env "APP_SERVICES_ENABLED") "1" }} +app_service_config_files: {{ env "APP_SERVICE_CONFIGS" }} {{ end }} # Uncomment to enable tracking of application service IP addresses. Implicitly